Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_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
cb0ae6a3
authored
Feb 25, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
d5739bd2
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
38 additions
and
2 deletions
apple.png → aa/apple.png
bg.png → aa/bg.png
body.png → aa/body.png
down.png → aa/down.png
left.png → aa/left.png
neuropol.ttf → aa/neuropol.ttf
right.png → aa/right.png
snake.py → aa/snake.py
up.png → aa/up.png
apple.png
→
a
a/a
pple.png
View file @
cb0ae6a3
File moved
bg.png
→
aa/
bg.png
View file @
cb0ae6a3
File moved
body.png
→
aa/
body.png
View file @
cb0ae6a3
File moved
down.png
→
aa/
down.png
View file @
cb0ae6a3
File moved
left.png
→
aa/
left.png
View file @
cb0ae6a3
File moved
neuropol.ttf
→
aa/
neuropol.ttf
View file @
cb0ae6a3
File moved
right.png
→
aa/
right.png
View file @
cb0ae6a3
File moved
snake.py
→
aa/
snake.py
View file @
cb0ae6a3
import
pygame
import
pygame
import
random
from
pygame
import
locals
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
...
@@ -15,9 +16,14 @@ food = pygame.image.load('apple.png') # 食物 苹果
...
@@ -15,9 +16,14 @@ food = pygame.image.load('apple.png') # 食物 苹果
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
18
)
#字体
fenshu
=
0
x
,
y
=
240
,
120
x
,
y
=
240
,
120
ax
,
ay
=
360
,
300
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
setheading
=
"right"
setheading
=
"right"
...
@@ -42,6 +48,8 @@ while True:
...
@@ -42,6 +48,8 @@ while True:
setheading
=
'down'
setheading
=
'down'
snake_head
=
down
snake_head
=
down
# 设置贪吃蛇的头部坐标
# 设置贪吃蛇的头部坐标
if
setheading
==
"right"
:
if
setheading
==
"right"
:
x
+=
30
x
+=
30
...
@@ -51,7 +59,27 @@ while True:
...
@@ -51,7 +59,27 @@ while True:
y
-=
30
y
-=
30
else
:
else
:
y
+=
30
y
+=
30
#撞墙退出游戏
if
x
<
0
or
y
<
0
or
x
>
630
or
y
>
450
:
exit
()
#撞到身体退出游戏
if
(
x
,
y
)
in
position
:
exit
()
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
if
x
==
ax
and
y
==
ay
:
#设置苹果的坐标
ax
=
random
.
randint
(
0
,
21
)
*
30
ay
=
random
.
randint
(
0
,
15
)
*
30
#苹果不随机到身体上重新随机坐标
while
(
ax
,
ay
)
in
position
:
ax
=
random
.
randint
(
0
,
21
)
*
30
ay
=
random
.
randint
(
0
,
15
)
*
30
fenshu
=
fenshu
+
1
else
:
position
.
pop
(
0
)
position
.
pop
(
0
)
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
...
@@ -61,8 +89,15 @@ while True:
...
@@ -61,8 +89,15 @@ while True:
for
i
in
range
(
len
(
position
)
-
1
):
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
screen
.
blit
(
body
,
position
[
i
])
#写入内容
text
=
"fenshu "
+
str
(
fenshu
)
info
=
font
.
render
(
text
,
True
,(
255
,
0
,
0
))
#分数渲染到游戏里
screen
.
blit
(
info
,
(
540
,
10
))
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
food
,
(
ax
,
ay
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
3
)
\ No newline at end of file
up.png
→
aa/
up.png
View file @
cb0ae6a3
File moved
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