Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_4
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
a51c60ce
authored
Mar 11, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
e4bbe901
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
0 deletions
snake.py
snake.py
View file @
a51c60ce
import
pygame
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
#载入图片
beijing
=
pygame
.
image
.
load
(
'bg.png'
)
apple
=
pygame
.
image
.
load
(
'apple.png'
)
juese
=
pygame
.
image
.
load
(
'right.png'
)
shenti
=
pygame
.
image
.
load
(
'body.png'
)
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPS
=
pygame
.
time
.
Clock
()
#pygame时钟,设置游戏速度(帧数)
x
,
y
=
240
,
120
zuobiao
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#身体和头的坐标库
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
==
locals
.
K_RIGHT
and
setheading
!=
'left'
:
setheading
=
'right'
if
event
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
setheading
=
'left'
if
event
==
locals
.
K_UP
and
setheading
!=
'down'
:
setheading
=
'up'
if
event
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
setheading
=
'down'
#向右走
x
+=
30
zuobiao
.
append
((
x
,
y
))
#增加一个头的坐标进入列表
zuobiao
.
pop
(
0
)
#删掉第一个坐标
#将背景画上去
screen
.
blit
(
beijing
,(
0
,
0
))
#把头画上去
screen
.
blit
(
juese
,
zuobiao
[
-
1
])
#把身体画上去
for
i
in
range
(
len
(
zuobiao
)
-
1
):
screen
.
blit
(
shenti
,
zuobiao
[
i
])
#把苹果画上去
screen
.
blit
(
apple
,(
0
,
60
))
#刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
2
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment