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
b33132f2
authored
Oct 22, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
54347764
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
3 deletions
snake.py
snake.py
View file @
b33132f2
...
@@ -10,27 +10,58 @@ FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(
...
@@ -10,27 +10,58 @@ FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(
# 背景
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
right
=
pygame
.
image
.
load
(
'right.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'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
# 向左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 向上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 向下
x
,
y
=
240
,
120
# 初始化贪吃蛇头的坐标设置xy为240,120
x
,
y
=
240
,
120
# 初始化贪吃蛇头的坐标设置xy为240,120
# 创建列表存储贪吃蛇的初始坐标三段身体以及蛇头的坐标
# 创建列表存储贪吃蛇的初始坐标三段身体以及蛇头的坐标
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
setheading
=
"right"
# 新建变量setheading记录贪吃蛇朝向并设为初始朝右"right"
snake_head
=
right
# 新建变量snake_head用来设置贪吃蛇朝向造型,初始造型设为朝右
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
()
x
+=
30
# 每次循环都将x坐标增加一个格子的长度30
if
event
.
type
==
locals
.
KEYDOWN
:
# if判断键盘是否被按下
# if判断4个方向键是否被按下以及判断贪吃蛇朝向是否相反
# 就将方向设为按下方向键朝向
# 将造型设置为朝向造型
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
setheading
=
'right'
snake_head
=
right
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
"right"
:
setheading
=
'left'
snake_head
=
left
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
"down"
:
setheading
=
'up'
snake_head
=
up
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
setheading
=
'down'
snake_head
=
down
# 设置贪吃蛇头的坐标
if
setheading
==
"right"
:
# if判断朝向向右
x
+=
30
# x坐标增加一格
elif
setheading
==
"left"
:
# 继续判断朝向向左
x
-=
30
# x坐标减少一格
elif
setheading
==
"up"
:
# 继续判断朝向向上
y
-=
30
# y坐标减少一格
else
:
# 否则 朝向向下
y
+=
30
# y坐标增加一格
# 将贪吃蛇头的新坐标追加进列表
# 将贪吃蛇头的新坐标追加进列表
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
position
.
pop
(
0
)
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇画上去
# 将贪吃蛇画上去
screen
.
blit
(
right
,
position
[
-
1
])
# 将列表的第一个元素删除作为蛇头的坐标
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将列表的第一个元素删除作为蛇头的坐标
# 将贪吃蛇的身体画上去
# 将贪吃蛇的身体画上去
# 用for循环取出身体坐标len()获取列表长度-1,得到的值通过range()函数生成列表下标
# 用for循环取出身体坐标len()获取列表长度-1,得到的值通过range()函数生成列表下标
for
i
in
range
(
len
(
position
)
-
1
):
for
i
in
range
(
len
(
position
)
-
1
):
...
...
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