Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy3
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
50f5b5cf
authored
Dec 19, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
77f8b56e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
16 additions
and
36 deletions
snake.py
snake.py
View file @
50f5b5cf
import
pygame
import
random
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
661
,
481
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPSCLOCK
=
pygame
.
time
.
Clock
()
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
food
=
pygame
.
image
.
load
(
'apple.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'
)
# 头 朝下
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.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'
)
x2
,
x3
,
x4
,
x5
,
x6
,
x7
,
x8
,
x9
,
x10
,
x11
=
330
y2
,
y3
,
y4
,
y5
,
y6
,
y7
,
y8
,
y9
,
y10
,
y11
=
330
x2
=
330
y2
=
330
x
,
y
=
240
,
120
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
...
...
@@ -31,7 +27,7 @@ snake_head = right
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
"left"
:
...
...
@@ -47,7 +43,6 @@ while True:
setheading
=
'down'
snake_head
=
down
# 设置贪吃蛇的头部坐标
if
setheading
==
"right"
:
x
+=
30
elif
setheading
==
"left"
:
...
...
@@ -56,38 +51,23 @@ while True:
y
-=
30
else
:
y
+=
30
position
.
append
((
x
,
y
))
if
x2
or
x3
or
x4
or
x5
or
x6
or
x7
or
x8
or
x9
or
x10
or
x11
==
x
and
y2
or
y3
or
y4
or
y5
or
y6
or
y7
or
y8
or
y9
or
y10
or
y11
==
y
:
if
x2
==
x
and
y2
==
y
:
x2
=
random
.
randint
(
0
,
21
)
y2
=
random
.
randint
(
0
,
15
)
x2
=
x2
*
30
y2
=
y2
*
30
else
:
position
.
pop
(
0
)
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
x2
,
y2
))
screen
.
blit
(
food
,
(
x3
,
y3
))
screen
.
blit
(
food
,
(
x4
,
y4
))
screen
.
blit
(
food
,
(
x5
,
y5
))
screen
.
blit
(
food
,
(
x6
,
y6
))
screen
.
blit
(
food
,
(
x7
,
y7
))
screen
.
blit
(
food
,
(
x8
,
y8
))
screen
.
blit
(
food
,
(
x9
,
y9
))
screen
.
blit
(
food
,
(
x10
,
y10
))
screen
.
blit
(
food
,
(
x11
,
y11
))
# 刷新画面
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
4
)
\ No newline at end of file
FPSCLOCK
.
tick
(
0.01
)
\ 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