Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_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
298528d5
authored
Aug 28, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
4dab22c9
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
13 deletions
snake.py
snake.py
View file @
298528d5
...
...
@@ -9,40 +9,53 @@ screen = pygame.display.set_mode((660, 480))
FPSCLOCK
=
pygame
.
time
.
Clock
()
#'''pygame时钟,控制游戏速度''' and
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
right
=
pygame
.
image
.
load
(
'right
.png'
)
right
=
pygame
.
image
.
load
(
'right
.png'
)
right
=
pygame
.
image
.
load
(
'righ
t.png'
)
up
=
pygame
.
image
.
load
(
'up
.png'
)
down
=
pygame
.
image
.
load
(
'down
.png'
)
left
=
pygame
.
image
.
load
(
'lef
t.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
x
=
240
y
=
120
fangxiang
=
"left"
a
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
),]
head
=
left
a
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
and
fangxiang
!=
"down"
:
fangxiang
=
"up"
if
event
.
key
==
locals
.
K_
UP
and
fangxiang
!=
"up"
:
if
event
.
key
==
locals
.
K_
DOWN
and
fangxiang
!=
"up"
:
fangxiang
=
"down"
if
event
.
key
==
locals
.
K_
UP
and
fangxiang
!=
"left"
:
if
event
.
key
==
locals
.
K_
RIGHT
and
fangxiang
!=
"left"
:
fangxiang
=
"right"
if
event
.
key
==
locals
.
K_
UP
and
fangxiang
!=
"right"
:
if
event
.
key
==
locals
.
K_
LEFT
and
fangxiang
!=
"right"
:
fangxiang
=
"left"
if
fangxiang
==
"left"
:
x
-=
30
head
=
left
if
fangxiang
==
"right"
:
x
+=
30
head
=
right
if
fangxiang
==
"up"
:
y
-=
30
head
=
up
if
fangxiang
==
"down"
:
y
+=
30
head
=
down
a
.
append
((
x
,
y
))
a
.
pop
(
0
)
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇画上去
screen
.
blit
(
right
,
(
x
,
y
))
screen
.
blit
(
head
,
(
x
,
y
))
for
i
in
range
(
len
(
a
)
-
1
):
# 将贪吃蛇的身体画上去
screen
.
blit
(
body
,
a
[
i
])
screen
.
blit
(
food
,
(
150
,
))
screen
.
blit
(
food
,
(
150
,
180
))
FPSCLOCK
.
tick
(
3
)
x
+=
30
a
.
append
((
x
,
y
))
a
.
pop
(
0
)
pygame
.
display
.
update
()
\ 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