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
29211ae5
authored
Dec 05, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
82dc80c6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
8 additions
and
1 deletions
snake.py
snake.py
View file @
29211ae5
import
pygame
import
random
from
random
import
randint
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
...
...
@@ -17,7 +18,9 @@ body = pygame.image.load('body.png') # 身体
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
50
)
score
=
0
r
,
g
,
b
=
0
,
0
,
0
x
,
y
=
240
,
120
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
a2x
=
random
.
randint
(
1
,
21
)
...
...
@@ -28,6 +31,7 @@ setheading = "right"
snake_head
=
right
while
True
:
r
,
g
,
b
=
randint
(
0
,
255
),
randint
(
0
,
255
),
randint
(
0
,
255
)
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
...
...
@@ -64,6 +68,7 @@ while True:
apple_x
=
30
*
a2x
apple_y
=
30
*
a2y
position
.
append
((
x
,
y
))
score
+=
10
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
...
...
@@ -72,6 +77,8 @@ while True:
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
text
=
font
.
render
(
"Score:"
+
str
(
score
),
True
,(
r
,
g
,
b
))
screen
.
blit
(
text
,(
randint
(
0
,
6
),
randint
(
0
,
5
)))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
# 刷新画面
...
...
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