Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_4
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
d0264f15
authored
Jul 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
701f32ab
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
69 additions
and
11 deletions
bad_food.png
snake.py
bad_food.png
0 → 100644
View file @
d0264f15
174 Bytes
snake.py
View file @
d0264f15
import
pygame
,
random
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 帧率初始化
FPSCLICK
=
pygame
.
time
.
Clock
()
...
...
@@ -19,8 +23,12 @@ up = pygame.image.load("up.png")
down
=
pygame
.
image
.
load
(
"down.png"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
#屁股
bad_food
=
pygame
.
image
.
load
(
"bad_food.png"
)
# 坏食物
body
=
pygame
.
image
.
load
(
"body.png"
)
#身体
#字体
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
# 头部初始化坐标
...
...
@@ -30,6 +38,10 @@ x,y = 240,120
apple_x
=
360
apple_y
=
300
#坏屁股初始化坐标
bad_apple_x
=
330
bad_apple_y
=
240
# 吃屁股
eat_apple
=
0
...
...
@@ -39,7 +51,25 @@ position = [(180,120),(180,270),(210,120),(x,y)]
setheading
=
'right'
snake_head
=
right
# 亿些封装
def
move
():
global
apple_y
,
apple_x
apple_x
=
30
*
random
.
randint
(
0
,
21
)
apple_y
=
30
*
random
.
randint
(
0
,
15
)
def
bad_apple_move
():
global
bad_apple_y
,
bad_apple_x
bad_apple_x
=
30
*
random
.
randint
(
0
,
21
)
bad_apple_y
=
30
*
random
.
randint
(
0
,
15
)
def
cantKeyDonw
():
global
x
,
y
if
x
>
630
or
x
<
0
or
y
>
480
or
y
<
0
:
return
False
else
:
return
True
while
True
:
# global eat_apple
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
12
:
# 接收到退出事件后退出程序
...
...
@@ -48,19 +78,19 @@ while True:
# 按键设置
if
event
.
type
==
locals
.
KEYDOWN
:
# w键
if
event
.
key
==
locals
.
K_w
and
setheading
!=
'down'
:
if
event
.
key
==
locals
.
K_w
and
setheading
!=
'down'
and
cantKeyDonw
()
:
setheading
=
'up'
snake_head
=
up
# a键
if
event
.
key
==
locals
.
K_a
and
setheading
!=
'right'
:
if
event
.
key
==
locals
.
K_a
and
setheading
!=
'right'
and
cantKeyDonw
()
:
setheading
=
'left'
snake_head
=
left
# s键
if
event
.
key
==
locals
.
K_s
and
setheading
!=
'up'
:
if
event
.
key
==
locals
.
K_s
and
setheading
!=
'up'
and
cantKeyDonw
()
:
setheading
=
'down'
snake_head
=
down
# d键
if
event
.
key
==
locals
.
K_d
and
setheading
!=
'left'
:
if
event
.
key
==
locals
.
K_d
and
setheading
!=
'left'
and
cantKeyDonw
()
:
setheading
=
'right'
snake_head
=
right
...
...
@@ -79,20 +109,40 @@ while True:
# 恰
苹果
# 恰
屁股 和 恰坏屁股
if
(
x
,
y
)
==
(
apple_x
,
apple_y
):
apple_x
=
30
*
random
.
randint
(
0
,
16
)
apple_x
=
30
*
random
.
randint
(
0
,
20
)
move
()
eat_apple
+=
1
elif
(
apple_x
,
apple_y
)
in
position
[
0
:
-
1
]:
move
()
elif
(
x
,
y
)
==
(
bad_apple_x
,
bad_apple_y
):
bad_apple_move
()
eat_apple
-=
1
for
i
in
range
(
2
):
position
.
pop
(
0
)
elif
(
apple_x
,
apple_y
)
in
position
[
0
:
-
1
]:
bad_apple_move
()
else
:
position
.
pop
(
0
)
# 碰墙退出
if
x
>
660
or
y
>
480
or
x
<
0
or
y
<
0
:
exit
()
# if x > 660 or y > 480 or x < 0 or y < 0:
# exit()
# 穿墙
if
x
<
0
:
x
=
630
if
x
>
630
:
x
=
-
30
if
y
<
0
:
y
=
480
if
y
>
480
:
y
=
-
30
# 分数退出
if
eat_apple
>
150
:
if
eat_apple
>
15
:
exit
()
elif
eat_apple
<
0
:
exit
()
# 自杀退出
...
...
@@ -108,11 +158,19 @@ while True:
# 屁股图片添加于屏幕
screen
.
blit
(
apple
,(
apple_x
,
apple_y
))
# 坏屁股图片添加于屏幕
screen
.
blit
(
bad_food
,(
bad_apple_x
,
bad_apple_y
))
# 身体图片添加于屏幕
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 绘制分数
info
=
'Score'
+
str
(
eat_apple
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
# 刷新画面
pygame
.
display
.
update
()
# 帧率
FPSCLICK
.
tick
(
5
)
FPSCLICK
.
tick
(
6
)
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