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
74bb31f1
authored
Jan 16, 2025
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
5fe01f68
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
7 deletions
snake.py
snake.py
View file @
74bb31f1
import
pygame
#导入pygame模块
import
pygame
#导入pygame模块
import
random
#导入random模块
import
random
#导入random模块
from
pygame
import
locals
#从pygame模块导入locals子模块
from
pygame
import
locals
#从pygame模块导入locals子模块
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
background
=
pygame
.
image
.
load
(
'bg.png'
)
#载入背景图
background
=
pygame
.
image
.
load
(
'bg.png'
)
#载入背景图
right
=
pygame
.
image
.
load
(
'right.png'
)
#贪吃蛇朝右的蛇头载入
right
=
pygame
.
image
.
load
(
'right.png'
)
#贪吃蛇朝右的蛇头载入
left
=
pygame
.
image
.
load
(
'left.png'
)
#贪吃蛇朝左的蛇头载入
left
=
pygame
.
image
.
load
(
'left.png'
)
#贪吃蛇朝左的蛇头载入
...
@@ -10,14 +9,15 @@ up=pygame.image.load('up.png')#贪吃蛇朝上的蛇头载入
...
@@ -10,14 +9,15 @@ up=pygame.image.load('up.png')#贪吃蛇朝上的蛇头载入
down
=
pygame
.
image
.
load
(
'down.png'
)
#贪吃蛇朝下的蛇头载入
down
=
pygame
.
image
.
load
(
'down.png'
)
#贪吃蛇朝下的蛇头载入
food
=
pygame
.
image
.
load
(
'apple.png'
)
#将食物苹果载入
food
=
pygame
.
image
.
load
(
'apple.png'
)
#将食物苹果载入
body
=
pygame
.
image
.
load
(
'body.png'
)
#将身体载入进去
body
=
pygame
.
image
.
load
(
'body.png'
)
#将身体载入进去
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
#将字体载入进去
x
,
y
=
240
,
120
#将蛇头的坐标赋值给一个变量
x
,
y
=
240
,
120
#将蛇头的坐标赋值给一个变量
apple_x
=
360
apple_x
=
360
apple_y
=
300
apple_y
=
300
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#建立一个列表
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
#建立一个列表
setheading
=
"right"
#设置贪吃蛇的朝向
setheading
=
"right"
#设置贪吃蛇的朝向
snake_head
=
right
snake_head
=
right
# 创建一个窗口
score
=
0
screen
=
pygame
.
display
.
set_mode
(
size
=
(
660
,
480
))
screen
=
pygame
.
display
.
set_mode
(
size
=
(
660
,
480
))
# 创建一个窗口
FPSCLOCK
=
pygame
.
time
.
Clock
()
FPSCLOCK
=
pygame
.
time
.
Clock
()
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
...
@@ -46,13 +46,22 @@ while True:
...
@@ -46,13 +46,22 @@ while True:
y
+=
30
y
+=
30
position
.
append
((
x
,
y
))
#将新坐标添加到新列表里
position
.
append
((
x
,
y
))
#将新坐标添加到新列表里
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
random
.
randint
(
0
,
660
)
num_1
=
random
.
randint
(
0
,
22
)
apple_y
=
random
.
randint
(
0
,
480
)
num_2
=
random
.
randint
(
0
,
16
)
position
.
pop
(
0
)
#删除掉第一个坐标
apple_x
=
num_1
*
30
-
30
apple_y
=
num_2
*
30
-
30
score
+=
10
else
:
position
.
pop
(
0
)
#删除掉第一个坐标
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
exit
()
screen
.
blit
(
background
,(
0
,
0
))
#将背景图画上去
screen
.
blit
(
background
,(
0
,
0
))
#将背景图画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
#将蛇头画上去
screen
.
blit
(
snake_head
,
position
[
-
1
])
#将蛇头画上去
for
i
in
range
(
len
(
position
)
-
1
):
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
screen
.
blit
(
body
,
position
[
i
])
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
#将食物画上去
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
#将食物画上去
info
=
"Score"
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
pygame
.
display
.
update
()
#刷新画面
pygame
.
display
.
update
()
#刷新画面
FPSCLOCK
.
tick
(
3
)
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