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
3798024c
authored
Jan 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
d5739bd2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
111 additions
and
2 deletions
less2.py
neuropol.ttf
snake.py
less2.py
0 → 100644
View file @
3798024c
import
pygame
from
pygame
import
locals
import
random
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
backgrade
=
pygame
.
image
.
load
(
'bg.png'
)
#背景
#头部的渲染
right
=
pygame
.
image
.
load
(
'right.png'
)
#头朝右
left
=
pygame
.
image
.
load
(
'left.png'
)
#头朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
#头朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
#头朝下
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
#创建字体资源
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#设置控制速度时钟模块
FPS
=
pygame
.
time
.
Clock
()
#设置贪吃蛇的初始朝向
setheading
=
"right"
#设置贪吃蛇头部的朝向
snake_head
=
right
#贪吃蛇的头部
x
,
y
=
240
,
120
#创建score变量
score
=
0
#贪吃蛇的移动:
#设置贪吃蛇的初始位置,按照从尾到头的顺序
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#创建苹果的初始坐标
apple_x
=
360
apple_y
=
300
while
True
:
#获取事件
for
event
in
pygame
.
event
.
get
():
# print(event)
#获取对应的事件id,与之匹配,然后执行相应的事件
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
#如果按下右键,并且当前朝向不是朝向左方
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"
:
x
+=
30
elif
setheading
==
"left"
:
x
-=
30
elif
setheading
==
"up"
:
y
-=
30
else
:
y
+=
30
#贪吃蛇头部向右移动
#x+=30
#时刻记录头部坐标
pos
.
append
((
x
,
y
))
#贪吃蛇吃到了苹果,这段代码要放在贪吃蛇头部更新的后面
if
x
==
apple_x
and
y
==
apple_y
:
num1
=
random
.
randint
(
1
,
22
)
#横排格子数
num2
=
random
.
randint
(
1
,
16
)
#纵排格子数
apple_x
=
num1
*
30
-
30
apple_y
=
num2
*
30
-
30
score
+=
10
#最尾部往前移动了,所以删除掉,吃了苹果就不删除
else
:
pos
.
pop
(
0
)
#撞墙检测:
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
exit
()
screen
.
blit
(
backgrade
,(
0
,
0
))
#用列表中的元素表示坐标
screen
.
blit
(
snake_head
,
pos
[
-
1
])
#获取贪吃蛇身体元素
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,
pos
[
i
])
# screen.blit(body,(210,120))
# screen.blit(body,(180,120))
# screen.blit(body,(180,90))
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
#绘制分数
fenshu
=
"Score"
+
str
(
score
)
text
=
my_font
.
render
(
fenshu
,
True
,(
0
,
250
,
200
))
screen
.
blit
(
text
,(
540
,
10
))
pygame
.
display
.
update
()
#在一秒内刷新不超过3次
FPS
.
tick
(
3
)
\ No newline at end of file
neuropol.ttf
deleted
100644 → 0
View file @
d5739bd2
File deleted
snake.py
View file @
3798024c
...
@@ -19,7 +19,8 @@ down = pygame.image.load('down.png') # 头 朝下
...
@@ -19,7 +19,8 @@ down = pygame.image.load('down.png') # 头 朝下
x
,
y
=
240
,
120
x
,
y
=
240
,
120
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
apple_x
=
360
apple_y
=
300
setheading
=
"right"
setheading
=
"right"
snake_head
=
right
snake_head
=
right
...
@@ -62,7 +63,8 @@ while True:
...
@@ -62,7 +63,8 @@ while True:
screen
.
blit
(
body
,
position
[
i
])
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
3
)
\ 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