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
ffbce351
authored
Sep 22, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
a7e541ab
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
5 deletions
snake.py
snake.py
View file @
ffbce351
import
pygame
import
pygame
#导入模块
from
pygame
import
locals
#从pygame中导入locals模块
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
bg
=
pygame
.
image
.
load
(
"bg.png"
)
#加载背景图
right
=
pygame
.
image
.
load
(
"right.png"
)
#加载蛇图片
body
=
pygame
.
image
.
load
(
"body.png"
)
#加载蛇身图片
apple
=
pygame
.
image
.
load
(
"apple.png"
)
#加载苹果图片
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
\ No newline at end of file
#创建计时器
FPSCLOCK
=
pygame
.
time
.
Clock
()
#定义xy坐标
x
,
y
=
120
,
90
position
=
[(
90
,
90
),(
60
,
90
),(
60
,
60
),(
x
,
y
)]
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
#退出事件
exit
()
x
+=
30
#每次追加贪吃蛇头部坐标,然后删除第一个坐标
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
screen
.
blit
(
bg
,(
0
,
0
))
#窗口画背景图
#窗口画蛇 取列表最后下标的数
screen
.
blit
(
right
,
position
[
-
1
])
#窗口画蛇身
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# screen.blit(body,(90,90))
# screen.blit(body,(60,90))
# screen.blit(body,(60,60))
screen
.
blit
(
apple
,(
330
,
210
))
#窗口画苹果
pygame
.
display
.
update
()
#刷新画面
FPSCLOCK
.
tick
(
3
)
#设置游戏画面每秒布超过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