Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_diy3
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
7b7776b9
authored
Jul 19, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
e934cfd0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
76 deletions
snake.py
snake.py
View file @
7b7776b9
#初始化
import
pygame
#导入pygame模块
from
pygame
import
locals
#导入locals模块
pygame
.
init
()
#初始化pygame,为使用pygame做准备
#开"时钟"
FPSCLOCK
=
pygame
.
time
.
Clock
()
#把时钟模块赋值给变量
#建窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#创建660x480的游戏窗口
#设变量
snake_head
=
'right'
#蛇头图片变量,设为向右
setheading
=
'right'
#蛇头方向变量,设为向右
#预加载
background
=
pygame
.
image
.
load
(
'bg.png'
)
#加载背景素材
right
=
pygame
.
image
.
load
(
'right.png'
)
#加载蛇头(右)素材
right
=
pygame
.
image
.
load
(
'left.png'
)
#加载蛇头(左)素材
right
=
pygame
.
image
.
load
(
'down.png'
)
#加载蛇头(下)素材
right
=
pygame
.
image
.
load
(
'up.png'
)
#加载蛇头(上)素材
body
=
pygame
.
image
.
load
(
'body.png'
)
#加载蛇身素材
#body = pygame.image.load('图片名.png')
x
,
y
=
240
,
120
#蛇头坐标变量
snake
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
#蛇身和蛇头位置的列表
'''
x,y如同两个装货卡车,现在用x车和y车上的物品填充列表,将列表赋值给snake,
在随后xy车上的货物改变后,列表里对应位置的货物不会改变的,只有再次赋值的时
候才会从xy车上取更新的货物。
'''
#判断事件
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'
#蛇头图案为“右”
# 随着x,y的增加,在列表加尾去头,实现整体移动
snake
.
append
((
x
,
y
))
#加尾
snake
.
pop
(
0
)
#去头
screen
.
blit
(
background
,
(
0
,
0
))
#渲染背景
screen
.
blit
(
snake_head
,
snake
[
-
1
])
#按方向渲染头
#screen.blit(代表图片的变量名, (从x, y开始渲染))
#渲染蛇身
for
i
in
range
(
len
(
snake
)
-
1
):
#len()指取蛇身位置列表的长度
screen
.
blit
(
body
,
snake
[
i
])
#通过-1来遍历列表中从第1项到倒数第2项所有坐标
pygame
.
display
.
update
()
#刷新屏幕,保证看到最新画面
FPSCLOCK
.
tick
(
3
)
#设置帧数(设置运行速度)
\ No newline at end of file
import
pygame
from
pygame
import
locals
#引用locals模块
x
,
y
=
240
,
120
#事先设置蛇头的位置
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
# pygame.display.set_mode((660, 480))
#pygame时钟,设定游戏快慢(帧数)
FPSCLOCK
=
pygame
.
time
.
Clock
()
# 加载好背景,用变量存起来
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
while
True
:
for
event
in
pygame
.
event
.
get
():
#pygame.event.get()玩家所做的所有事件
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
# 退出(关窗口)
x
+=
30
position
.
append
((
x
,
y
))
position
.
pop
(
position
[
0
])
# 将背景图渲染上去
screen
.
blit
(
background
,(
0
,
0
))
# 将贪吃蛇渲染上去
screen
.
blit
(
right
,
(
x
,
y
))
# 将贪吃蛇的身体(3个)渲染上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,(
position
[
i
]))
# 将果实渲染上去
screen
.
blit
(
food
,
(
360
,
300
))
#渲染是有顺序的,后渲染上的比前一个要靠上一层(后渲染上的可以覆盖前渲染上的)
# 刷新画面(不然还是看不见图片)
pygame
.
display
.
update
()
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