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
1e5a7fb2
authored
Aug 06, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
c6188a1e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
113 additions
and
4 deletions
snake.py
snake.py
View file @
1e5a7fb2
#引入pygame库
import
pygame
import
pygame
#引入pygame的local方法
from
pygame
import
locals
#引入随机数
import
random
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
#创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
600
,
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"
)
#蛇头 向左
left
=
pygame
.
image
.
load
(
"left.png"
)
#蛇头 向上
up
=
pygame
.
image
.
load
(
"up.png"
)
#蛇头 向下
down
=
pygame
.
image
.
load
(
"down.png"
)
#设置字体
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#设置x,y的初始化坐标
x
,
y
=
240
,
120
#设置所有物品在舞台上的坐标
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#设置苹果的x,y坐标
apple_x
=
360
apple_y
=
300
setheading
=
"right"
snake_head
=
"right"
#初始化分数
score
=
0
while
True
:
for
event
in
pygame
.
event
.
get
():
#如果接收到退出时间,结束程序
if
event
.
type
==
locals
.
QUIT
:
#退出
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RCTRL
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
=
x
+
30
if
setheading
==
"left"
:
x
=
x
-
30
if
setheading
==
"up"
:
y
=
y
+
30
else
:
y
=
y
-
30
position
.
append
((
x
,
y
))
#假如贪吃蛇吃到了苹果
if
x
==
apple_x
and
y
==
apple_y
:
#随机设置出现在横排的格子
num1
=
random
.
randint
(
1
,
22
)
#竖排的格子
num2
=
random
.
randint
(
1
,
16
)
#苹果的x坐标
apple_x
=
30
*
num1
-
30
#苹果的y坐标
apple_y
=
30
*
num2
-
30
else
:
#吃到苹果就删除这个角色
position
.
pop
(
0
)
#检测贪吃蛇撞墙,如果撞墙就结束游戏
if
x
>
0
or
x
>
630
or
y
<
0
or
y
>
450
:
#结束游戏
exit
()
#将背景画上去
screen
.
blit
(
background
,(
0
,
0
))
#将蛇头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
#将蛇的身体画上去
for
i
in
range
(
len
(
position
-
1
)):
screen
.
blit
(
body
,
position
[
i
])
#将果实画上去
screen
.
blit
(
food
,(
apple_y
,
apple_y
))
#绘制分数
info
=
"Score:"
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
,))
screen
.
blit
(
text
,(
540
,
10
))
#刷新画面
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