Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_2
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
12e3aa4e
authored
May 29, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
c6188a1e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
6 deletions
smalle.fon
snake.py
smalle.fon
0 → 100644
View file @
12e3aa4e
File added
snake.py
View file @
12e3aa4e
import
pygame
import
pygame
#调用模块
import
random
#调用模块,使用其中的随机数
def
gameover
():
#定义游戏失败函数
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
50
)
#设置字体格式和大小
text1
=
my_font
.
render
(
'GAME OVER'
,
True
,(
255
,
0
,
0
))
screen
.
blit
(
text1
,(
150
,
180
))
pygame
.
display
.
update
()
#刷新到屏幕
sz
.
tick
(
0.5
)
#等待一段时间,因为失败有退出了,所以函数里没有加退出。
def
success
():
#定义游戏胜利函数
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
50
)
#设置字体格式和大小
text1
=
my_font
.
render
(
'SUCCESS'
,
True
,(
255
,
0
,
0
))
screen
.
blit
(
text1
,(
150
,
180
))
pygame
.
display
.
update
()
#刷新到屏幕
sz
.
tick
(
0.5
)
#等待一段时间
exit
()
#退出程序
from
pygame
import
locals
#调用模块中的方法
pygame
.
init
()
#初始化pygame,为硬件使用做准备
#加载图片并赋值角色
background
=
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'
)
#蛇头向下图片
body
=
pygame
.
image
.
load
(
'body.png'
)
#蛇身
food
=
pygame
.
image
.
load
(
'apple.png'
)
#食物
x
=
60
#初始化蛇头初始位置
y
=
90
setheading
=
'right'
#设置蛇的初始朝向
snackhead_heading
=
right
#初始化蛇头方向的图片,蛇更协调
sz
=
pygame
.
time
.
Clock
()
#设置时钟,一个定时器,类似于wait()
position
=
[(
0
,
60
),(
30
,
60
),(
30
,
90
),(
x
,
y
)]
#初始化一个列表保存坐标位置,蛇的移动是后一节身体下一秒移动到上一节身体现在的位置
apple_x
=
330
#设置苹果位置坐标
apple_y
=
300
score
=
0
#初始化分数变量
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
20
)
#设置字体格式和大小
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#创建一个窗口,注意尺寸也要用括号,所以是双括号
# 创建一个窗口
??
\ No newline at end of file
while
True
:
#循环,窗口一直显示;实时监测操作
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'
snackhead_heading
=
right
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
#蛇不能穿过自己的身体
setheading
=
'left'
snackhead_heading
=
left
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
'down'
:
setheading
=
'up'
snackhead_heading
=
up
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
setheading
=
'down'
snackhead_heading
=
down
for
i
in
range
(
len
(
position
)
-
1
):
#判断蛇头是否碰到自己的身体
if
(
x
,
y
)
==
position
[
i
]:
#如果蛇头和身体在同一个位置坐标,就算是吃到了
gameover
()
exit
()
if
setheading
==
'right'
:
#蛇面向按下的方向前进
x
+=
30
if
setheading
==
'left'
:
x
-=
30
if
setheading
==
'up'
:
y
-=
30
if
setheading
==
'down'
:
y
+=
30
position
.
append
((
x
,
y
))
#把蛇头新坐标加进位置的最后
if
x
==
apple_x
and
y
==
apple_y
:
#蛇头和苹果在同一个坐标表示吃掉苹果
num1
=
random
.
randint
(
1
,
22
)
#随机设置苹果在横向多少格660/30=22,坐标刷新位置在格子的左上角
num2
=
random
.
randint
(
1
,
16
)
#随机设置苹果在纵向多少格660/30=22,坐标刷新位置在格子的左上角
apple_x
=
num1
*
30
-
30
#计算在每个格子左上角的坐标
apple_y
=
num2
*
30
-
30
score
+=
10
else
:
position
.
pop
(
0
)
#删除第一个坐标,通过这两步实现蛇的移动;吃到苹果不删除,蛇就变长
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
#游戏窗口大小660x480,但是坐标要减去素材大小30
gameover
()
exit
()
screen
.
blit
(
background
,(
0
,
0
))
#渲染背景,背景格子大小是30x30,要保证蛇和苹果在格子里,要能整除30
screen
.
blit
(
snackhead_heading
,
position
[
-
1
])
#渲染蛇头
for
i
in
range
(
len
(
position
)
-
1
):
#遍历位置,依次刷新蛇身体坐标
screen
.
blit
(
body
,
position
[
i
])
#渲染身体
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
#渲染食物坐标
info
=
'Score:'
+
str
(
score
)
#字符串拼接
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
#要渲染的文字设置
screen
.
blit
(
text
,(
500
,
10
))
#文字渲染到窗口
if
score
==
150
:
#游戏胜利条件
success
()
#刷新
pygame
.
display
.
update
()
#不停刷新才能看到变化效果
sz
.
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