Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy3
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
7fa0c593
authored
Jan 15, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
51709d8e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
15 deletions
snake.py
snake.py
View file @
7fa0c593
...
...
@@ -17,6 +17,7 @@ body = pygame.image.load('body.png') # 身体
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
x
,
y
=
240
,
120
p
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
...
...
@@ -24,7 +25,7 @@ ax=360
ay
=
300
sd
=
"right"
sh
=
right
fsh
=
0
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
...
...
@@ -45,30 +46,28 @@ while True:
sh
=
down
# 设置贪吃蛇的头部坐标
if
sd
==
"right"
:
x
+=
30
elif
sd
==
"left"
:
x
-=
30
elif
sd
==
"up"
:
y
-=
30
if
sd
==
"right"
:
x
+=
30
elif
sd
==
"left"
:
x
-=
30
elif
sd
==
"up"
:
y
-=
30
else
:
y
+=
30
y
+=
30
p
.
append
((
x
,
y
))
if
x
==
ax
and
y
==
ay
:
ax
=
random
.
randint
(
1
,
22
)
*
30
-
30
ay
=
random
.
randint
(
1
,
16
)
*
30
-
30
fsh
+=
1
else
:
p
.
pop
(
0
)
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
screen
.
blit
(
sh
,
p
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
p
)
-
1
):
screen
.
blit
(
body
,
p
[
i
])
# 将果实画上去
io
=
'score:'
+
str
(
fsh
)
text
=
my_font
.
render
(
io
,
True
,
(
0
,
0
,
0
))
screen
.
blit
(
text
,
(
10
,
10
))
screen
.
blit
(
food
,
(
ax
,
ay
))
# 刷新画面
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
6
)
\ 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