Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy4
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
28fe25dc
authored
Sep 26, 2020
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
bfd27ca3
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
14 additions
and
18 deletions
snake.py
snake.py
View file @
28fe25dc
import
pygame
import
time
from
random
import
randint
from
pygame
import
locals
...
...
@@ -20,7 +19,6 @@ left = pygame.image.load('left.png') # 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
txt
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
20
)
over
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
75
)
x
,
y
=
240
,
120
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
...
...
@@ -49,35 +47,33 @@ while True:
snake_head
=
down
# 设置贪吃蛇的头部坐标
if
setheading
==
"right"
:
x
+=
30
elif
setheading
==
"left"
:
x
-=
30
elif
setheading
==
"up"
:
y
-=
30
else
:
y
+=
30
position
.
append
((
x
,
y
))
if
position
[
-
1
]
==
(
fx
,
fy
):
fx
=
randint
(
0
,
20
)
*
30
fy
=
randint
(
0
,
15
)
*
30
score
+=
1
else
:
position
.
pop
(
0
)
# 将背景图画上去
text
=
txt
.
render
(
"SCORE:"
+
str
(
score
),
True
,(
0
,
0
,
0
))
if
0
>
x
or
x
>
630
or
0
>
y
or
y
>
450
:
exit
()
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
if
0
>
x
or
630
<
x
or
0
>
y
or
y
>
450
:
a
=
over
.
render
(
"GAME OVER"
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
a
,
(
0
,
75
))
else
:
if
setheading
==
"right"
:
x
+=
30
elif
setheading
==
"left"
:
x
-=
30
elif
setheading
==
"up"
:
y
-=
30
else
:
y
+=
30
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去1
screen
.
blit
(
food
,
(
fx
,
fy
))
screen
.
blit
(
text
,(
10
,
10
))
...
...
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