Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_4
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
701f32ab
authored
Jul 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
8d92bff3
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
113 additions
and
23 deletions
snake.py
test.py
snake.py
View file @
701f32ab
import
pygame
import
pygame
,
random
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 帧率初始化
FPSCLICK
=
pygame
.
time
.
Clock
()
# 创建一个窗口
baclground
=
pygame
.
image
.
load
(
"bg.png"
)
right
=
pygame
.
image
.
load
(
"right.png"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
x
,
y
=
240
,
120
position
=
[(
180
,
120
),(
180
,
270
),(
210
,
120
),(
x
,
y
)]
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
# 导入图片
baclground
=
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"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
#屁股
body
=
pygame
.
image
.
load
(
"body.png"
)
#身体
# 头部初始化坐标
x
,
y
=
240
,
120
# 屁股初始化坐标
apple_x
=
360
apple_y
=
300
# 吃屁股
eat_apple
=
0
# 贪吃蛇每节身体的坐标的初始化
position
=
[(
180
,
120
),(
180
,
270
),(
210
,
120
),(
x
,
y
)]
setheading
=
'right'
snake_head
=
right
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
12
:
# 接收到退出事件后退出程序
exit
()
# if event.type == locals.KEYDOWN:
# if event.key == event.K_DOWN:
# y += 30
x
+=
30
# 按键设置
if
event
.
type
==
locals
.
KEYDOWN
:
# w键
if
event
.
key
==
locals
.
K_w
and
setheading
!=
'down'
:
setheading
=
'up'
snake_head
=
up
# a键
if
event
.
key
==
locals
.
K_a
and
setheading
!=
'right'
:
setheading
=
'left'
snake_head
=
left
# s键
if
event
.
key
==
locals
.
K_s
and
setheading
!=
'up'
:
setheading
=
'down'
snake_head
=
down
# d键
if
event
.
key
==
locals
.
K_d
and
setheading
!=
'left'
:
setheading
=
'right'
snake_head
=
right
# 设置贪吃蛇的移动方向(头部坐标)
if
setheading
==
'right'
:
x
+=
30
if
setheading
==
'left'
:
x
-=
30
if
setheading
==
'up'
:
y
-=
30
if
setheading
==
'down'
:
y
+=
30
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
screen
.
blit
(
baclground
,(
0
,
0
))
screen
.
blit
(
right
,(
x
,
y
))
screen
.
blit
(
apple
,(
360
,
300
))
screen
.
blit
(
body
,(
210
,
120
))
# 恰苹果
if
(
x
,
y
)
==
(
apple_x
,
apple_y
):
apple_x
=
30
*
random
.
randint
(
0
,
16
)
apple_x
=
30
*
random
.
randint
(
0
,
20
)
eat_apple
+=
1
else
:
position
.
pop
(
0
)
# 碰墙退出
if
x
>
660
or
y
>
480
or
x
<
0
or
y
<
0
:
exit
()
# 分数退出
if
eat_apple
>
150
:
exit
()
# 自杀退出
if
position
[
-
1
]
in
position
[
0
:
-
1
]:
exit
()
# 背景图片添加于屏幕
screen
.
blit
(
baclground
,(
0
,
0
))
# 头部图片添加于屏幕
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 屁股图片添加于屏幕
screen
.
blit
(
apple
,(
apple_x
,
apple_y
))
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
pygame
.
display
.
update
()
FPSCLICK
.
tick
(
3
)
# 帧率
FPSCLICK
.
tick
(
5
)
test.py
View file @
701f32ab
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
while
True
:
screen
=
pygame
.
display
.
set_mode
((
1920
,
1080
))
# 帧率初始化
FPSCLICK
=
pygame
.
time
.
Clock
()
# 创建窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
12
:
print
(
event
)
\ No newline at end of file
exit
()
print
(
event
)
\ 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