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
92b5b8d7
authored
Dec 17, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
9276b9d7
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
7 deletions
snake.py
snake.py
View file @
92b5b8d7
import
pygame
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
pygame
.
init
()
...
@@ -10,30 +9,54 @@ screen = pygame.display.set_mode((660, 480))
...
@@ -10,30 +9,54 @@ screen = pygame.display.set_mode((660, 480))
# 背景
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
x
=
240
x
=
240
y
=
120
y
=
120
f
=
pygame
.
time
.
Clock
()
f
=
pygame
.
time
.
Clock
()
eva
=
"right"
ave
=
right
l
=
[(
210
,
120
),(
180
,
120
),(
180
,
90
),(
x
,
y
)]
l
=
[(
210
,
120
),(
180
,
120
),(
180
,
90
),(
x
,
y
)]
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
pygame
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
# 将背景图画上去
if
event
.
type
==
pygame
.
KEYDOWN
:
screen
.
blit
(
background
,
(
0
,
0
))
if
event
.
type
==
pygame
.
K_RIGHT
and
eva
!=
"left"
:
eva
=
"right"
ave
=
right
if
event
.
type
==
pygame
.
K_LEFT
and
eva
!=
"right"
:
eva
=
"left"
ave
=
left
if
event
.
type
==
pygame
.
K_UP
and
eva
!=
"down"
:
eva
=
"up"
ave
=
up
if
event
.
type
==
pygame
.
K_DOWN
and
eva
!=
"up"
:
eva
=
"down"
ave
=
down
# 将贪吃蛇画上去
# 将贪吃蛇画上去
if
eva
==
"left"
:
x
=
x
-
30
elif
eva
==
"right"
:
x
=
x
+
30
elif
eva
==
"up"
:
y
-=
30
else
:
y
+=
30
l
.
append
((
x
,
y
))
l
.
append
((
x
,
y
))
l
.
pop
(
0
)
l
.
pop
(
0
)
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
right
,
l
[
-
1
])
screen
.
blit
(
right
,
l
[
-
1
])
x
+=
30
y
+=
30
for
i
in
range
(
len
(
l
)
-
1
):
for
i
in
range
(
len
(
l
)
-
1
):
screen
.
blit
(
body
,
l
[
i
])
screen
.
blit
(
body
,
l
[
i
])
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
food
,
(
360
,
300
))
f
.
tick
(
5
)
f
.
tick
(
4
)
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
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