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
d7994ef5
authored
Sep 04, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
16f0d60e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
5 deletions
snake.py
snake.py
View file @
d7994ef5
import
pygame
import
pygame
import
random
from
pygame
import
locals
from
pygame
import
locals
head_x
=
240
head_y
=
120
position
=
[(
210
,
120
),(
180
,
120
),(
90
,
120
),(
head_x
,
head_y
)]
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
pygame
.
init
()
...
@@ -10,23 +14,64 @@ screen = pygame.display.set_mode((660, 480))
...
@@ -10,23 +14,64 @@ 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'
)
left
=
pygame
.
image
.
load
(
"left.png"
)
up
=
pygame
.
image
.
load
(
"up.png"
)
down
=
pygame
.
image
.
load
(
"down.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'
)
FPSCOLOK
=
pygame
.
time
.
Clock
()
heading
=
"right"
snake_head
=
right
apple_x
=
360
apple_y
=
300
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
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_RIGHT
and
heading
!=
"left"
:
#K_W
heading
=
"right"
if
event
.
key
==
pygame
.
K_LEFT
and
heading
!=
"right"
:
#K_W
heading
=
"left"
if
event
.
key
==
pygame
.
K_UP
and
heading
!=
"down"
:
#K_W
heading
=
"up"
if
event
.
key
==
pygame
.
K_DOWN
and
heading
!=
"up"
:
#K_W
heading
=
"down"
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
# 将贪吃蛇画上去
# 将贪吃蛇画上去
screen
.
blit
(
right
,
(
240
,
120
))
if
heading
==
"right"
:
head_x
+=
30
snake_head
=
right
elif
heading
==
"left"
:
head_x
-=
30
snake_head
=
left
elif
heading
==
"up"
:
head_y
-=
30
snake_head
=
up
elif
heading
==
"down"
:
head_y
+=
30
snake_head
=
down
position
.
append
((
head_x
,
head_y
))
#判断是否吃到
if
head_x
==
apple_x
and
head_y
==
apple_y
:
apple_x
=
random
.
randint
(
0
,
21
)
*
30
apple_y
=
random
.
randint
(
0
,
15
)
*
30
else
:
position
.
pop
(
0
)
screen
.
blit
(
snake_head
,
(
head_x
,
head_y
))
# 将贪吃蛇的身体画上去
# 将贪吃蛇的身体画上去
screen
.
blit
(
body
,
(
210
,
120
))
for
index
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
(
180
,
120
)
)
screen
.
blit
(
body
,
position
[
index
]
)
screen
.
blit
(
body
,
(
180
,
90
))
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
# 刷新画面
# 刷新画面
FPSCOLOK
.
tick
(
10
)
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