Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy1
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
f8ddb4ff
authored
4 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
0390d315
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
25 additions
and
5 deletions
snake.py
snake.py
View file @
f8ddb4ff
...
@@ -22,7 +22,7 @@ position = [(180, 90), (180, 120), (210, 120), (x, y)]
...
@@ -22,7 +22,7 @@ position = [(180, 90), (180, 120), (210, 120), (x, y)]
apple_x
,
apple_y
=
360
,
300
apple_x
,
apple_y
=
360
,
300
setheading
=
"right"
setheading
=
"right"
snake_head
=
right
snake_head
=
right
score
=
0
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
...
@@ -51,12 +51,23 @@ while True:
...
@@ -51,12 +51,23 @@ while True:
y
-=
30
y
-=
30
else
:
else
:
y
+=
30
y
+=
30
# 吃掉水果身体变长
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
num_x
num_x
=
random
.
randint
(
1
,
22
)
apple_x
=
random
.
randint
(
30
,
1800
)
%
30
num_y
=
random
.
randint
(
1
,
16
)
apple_y
=
random
.
randint
(
30
,
14400
)
%
30
apple_x
=
num_x
*
30
apple_y
=
num_y
*
30
score
+=
1
else
:
position
.
pop
(
0
)
#贪吃蛇撞墙停止
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
font1
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
25
)
text1
=
font
.
render
(
"游戏失败"
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
200
,
200
))
break
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
# 将贪吃蛇的头画上去
...
@@ -67,6 +78,14 @@ while True:
...
@@ -67,6 +78,14 @@ while True:
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
# 绘制分数
# 1.载入字体
font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
# 2.设置字体文字
text
=
font
.
render
(
"Score:"
+
str
(
score
),
True
,(
0
,
0
,
0
))
# 3.渲染
screen
.
blit
(
text
,(
400
,
0
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
3
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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