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
c7babce0
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
8c6de8a9
c173352l1354p256a13982/wx235701
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
94 additions
and
60 deletions
ffg.py
snake.py
ffg.py
0 → 100644
View file @
c7babce0
#贪吃蛇是否撞墙
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
#如果蛇超出四个边界其中之一
exit
()
#退出
\ No newline at end of file
This diff is collapsed.
Click to expand it.
snake.py
View file @
c7babce0
import
pygame
#初始化
from
pygame
import
locals
import
pygame
#导入pygame模块
from
pygame
import
locals
#导入locals模块
import
random
#导入随机数模块
pygame
.
init
()
#初始化pygame,为使用pygame做准备
#
初始化pygame,为使用硬件做准备
#
开"时钟"
pygame
.
init
()
FPSCLOCK
=
pygame
.
time
.
Clock
()
#把时钟模块赋值给变量
# 创建一个窗口
#建窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#创建660x480的游戏窗口
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
# 背景
#预加载
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
#加载背景素材
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
right
=
pygame
.
image
.
load
(
'right.png'
)
#加载蛇头(右)素材
food
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
left
=
pygame
.
image
.
load
(
'left.png'
)
#加载蛇头(左)素材
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
down
=
pygame
.
image
.
load
(
'down.png'
)
#加载蛇头(下)素材
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
#加载蛇头(上)素材
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
body
=
pygame
.
image
.
load
(
'body.png'
)
#加载蛇身素材
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
apple
=
pygame
.
image
.
load
(
'apple.png'
)
#加载苹果素材
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#加载分数字体
#变量名 = pygame.image.load('图片名.png')
x
,
y
=
240
,
120
#设变量
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
snake_head
=
right
#蛇头图片变量,设为向右
setheading
=
'right'
#蛇头方向变量,设为向右
x
,
y
=
240
,
120
#蛇头坐标变量
apple_x
,
apple_y
=
300
,
360
#苹果坐标变量
snake
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
#蛇身和蛇头位置的列表
score
=
0
#分数
'''
x,y如同两个装货卡车,现在用x车和y车上的物品填充列表,将列表赋值给snake,
在随后xy车上的货物改变后,列表里对应位置的货物不会改变的,只有再次赋值的时
候才会从xy车上取更新的货物。
'''
setheading
=
"right"
#判断事件
snake_head
=
right
while
True
:
#重复执行以下代码
for
event
in
pygame
.
event
.
get
():
#将所有玩家所做的事件赋值给event
if
event
.
type
==
locals
.
QUIT
:
#如果事件的编号=locals.QUIT的编号(locals.QUIT也就是点击退出发生)
exit
()
#退出程序
if
event
.
type
==
locals
.
KEYDOWN
:
#如果出现键盘事件
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
setheading
=
'down'
#按下“下”键且方向不冲上,方向就改为下
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
'down'
:
setheading
=
'up'
#按下“上”键且方向不冲下,方向就改为上
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
'left'
:
setheading
=
'right'
#按下“右”键且方向不冲左,方向就改为右
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
setheading
=
'left'
#按下“左”键且方向不冲右,方向就改为左
#检测方向,移动
if
setheading
==
'up'
:
#如果方向为上
y
-=
30
#y坐标减三十(向上挪30)
snake_head
=
up
#蛇头图案为“上”
if
setheading
==
'down'
:
#如果方向为下
y
+=
30
#y坐标加三十(向下挪30)
snake_head
=
down
#蛇头图案为“下”
if
setheading
==
'left'
:
#如果方向为左
x
-=
30
#x坐标减三十(向左挪30)
snake_head
=
left
#蛇头图案为“左”
if
setheading
==
'right'
:
#如果方向为右
x
+=
30
#x坐标加三十(向右挪30)
snake_head
=
right
#蛇头图案为“右”
while
True
:
snake
.
append
((
x
,
y
))
#加尾不去头,因为蛇碰上苹果后会变长一格
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"
:
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"
:
if
apple_x
==
x
and
apple_y
==
y
:
#如果苹果和蛇头坐标一样(碰到了)
x
+=
30
num_x
=
random
.
randint
(
1
,
22
)
#从1-21之间随机选一个横格数(格的坐标为左上角)
elif
setheading
==
"left"
:
num_y
=
random
.
randint
(
1
,
16
)
#从1-16之间随机选一个竖格数
x
-=
30
apple_x
=
30
*
num_x
-
30
#横格数乘三十再减三十得到x坐标(格的坐标为左上角,不是右上角)
elif
setheading
==
"up"
:
apple_y
=
30
*
num_y
-
30
#竖格数乘三十再减三十得到y坐标
y
-=
30
score
+=
1
else
:
else
:
y
+=
30
snake
.
pop
(
0
)
#如果没碰上苹果,就不会变长,去头
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
#贪吃蛇是否撞墙
# 将背景图画上去
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
#如果蛇超出四个边界其中之一
screen
.
blit
(
background
,
(
0
,
0
))
exit
()
#退出
# 将贪吃蛇的头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
background
,
(
0
,
0
))
#渲染背景
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
apple
,(
apple_x
,
apple_y
))
#渲染苹果
# 刷新画面
screen
.
blit
(
snake_head
,
snake
[
-
1
])
#按方向渲染头
pygame
.
display
.
update
()
#screen.blit(代表图片的变量名, (从x, y开始渲染))
FPSCLOCK
.
tick
(
3
)
\ No newline at end of file
#渲染蛇身
for
i
in
range
(
len
(
snake
)
-
1
):
#len(snake)指取蛇身位置列表的长度,-1是减一
screen
.
blit
(
body
,
snake
[
i
])
#通过减1来遍历列表中从第1项到倒数第2项所有坐标
#绘制分数
info
=
'Score'
+
str
(
score
)
#将要打印的内容
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
#以纯黑色渲染,赋值给变量
screen
.
blit
(
text
,(
540
,
10
))
#在540,10渲染分数
#(0,0,0)是色盘,由三个0-255的数表示颜色,(0,0,0)指黑色
pygame
.
display
.
update
()
#刷新屏幕,保证看到最新画面
FPSCLOCK
.
tick
(
3
)
#设置帧数(设置运行速度)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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