Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_2
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
ae43067d
authored
Dec 07, 2020
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
7bc38f28
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
4 deletions
snake3.py
snake3.py
View file @
ae43067d
...
@@ -13,22 +13,28 @@ up = pygame.image.load('up.png') #头朝上
...
@@ -13,22 +13,28 @@ up = pygame.image.load('up.png') #头朝上
down
=
pygame
.
image
.
load
(
'down.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'
)
#创建字体资源
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#设置控制速度时钟模块
#设置控制速度时钟模块
FPS
=
pygame
.
time
.
Clock
()
FPS
=
pygame
.
time
.
Clock
()
#设置贪吃蛇的初始朝向
#设置贪吃蛇的初始朝向
setheading
=
"right"
setheading
=
"right"
#设置贪吃蛇头部的朝向
#设置贪吃蛇头部的朝向
snake_head
=
right
snake_head
=
right
#贪吃蛇的头部
#贪吃蛇的头部
x
,
y
=
240
,
120
x
,
y
=
240
,
120
#创建苹果的初始坐标
#创建score变量
apple_x
=
360
score
=
0
apple_y
=
300
#贪吃蛇的移动:
#贪吃蛇的移动:
#设置贪吃蛇的初始位置,按照从尾到头的顺序
#设置贪吃蛇的初始位置,按照从尾到头的顺序
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#创建苹果的初始坐标
apple_x
=
360
apple_y
=
300
while
True
:
while
True
:
...
@@ -65,14 +71,18 @@ while True:
...
@@ -65,14 +71,18 @@ while True:
#时刻记录头部坐标
#时刻记录头部坐标
pos
.
append
((
x
,
y
))
pos
.
append
((
x
,
y
))
#贪吃蛇吃到了苹果,这段代码要放在贪吃蛇头部更新的后面
#贪吃蛇吃到了苹果,这段代码要放在贪吃蛇头部更新的后面
if
x
==
apple_
y
and
y
==
apple_y
:
if
x
==
apple_
x
and
y
==
apple_y
:
num1
=
random
.
randint
(
1
,
22
)
#横排格子数
num1
=
random
.
randint
(
1
,
22
)
#横排格子数
num2
=
random
.
randint
(
1
,
16
)
#纵排格子数
num2
=
random
.
randint
(
1
,
16
)
#纵排格子数
apple_x
=
num1
*
30
-
30
apple_x
=
num1
*
30
-
30
apple_y
=
num2
*
30
-
30
apple_y
=
num2
*
30
-
30
score
+=
10
#最尾部往前移动了,所以删除掉,吃了苹果就不删除
#最尾部往前移动了,所以删除掉,吃了苹果就不删除
else
:
else
:
pos
.
pop
(
0
)
pos
.
pop
(
0
)
#撞墙检测:
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
exit
()
screen
.
blit
(
backgrade
,(
0
,
0
))
screen
.
blit
(
backgrade
,(
0
,
0
))
#用列表中的元素表示坐标
#用列表中的元素表示坐标
screen
.
blit
(
snake_head
,
pos
[
-
1
])
screen
.
blit
(
snake_head
,
pos
[
-
1
])
...
@@ -84,6 +94,11 @@ while True:
...
@@ -84,6 +94,11 @@ while True:
# screen.blit(body,(180,120))
# screen.blit(body,(180,120))
# screen.blit(body,(180,90))
# screen.blit(body,(180,90))
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
#绘制分数
info
=
"Score"
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
pygame
.
display
.
update
()
pygame
.
display
.
update
()
#在一秒内刷新不超过3次
#在一秒内刷新不超过3次
...
...
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