Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy3
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
85d98579
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
4c381640
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
3 deletions
snake.py
snake.py
View file @
85d98579
...
@@ -8,7 +8,11 @@ pygame.init()
...
@@ -8,7 +8,11 @@ pygame.init()
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
score
=
0
b
=
random
.
randint
(
10
,
50
)
c
=
random
.
randint
(
0
,
200
)
d
=
random
.
randint
(
0
,
200
)
e
=
random
.
randint
(
0
,
200
)
# 背景
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
...
@@ -17,7 +21,7 @@ body = pygame.image.load('body.png') # 身体
...
@@ -17,7 +21,7 @@ body = pygame.image.load('body.png') # 身体
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
left
=
pygame
.
image
.
load
(
'left.png'
)
# 头 朝左
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
up
=
pygame
.
image
.
load
(
'up.png'
)
# 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
fen
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
b
)
x
,
y
=
240
,
120
x
,
y
=
240
,
120
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
position
=
[(
180
,
90
),
(
180
,
120
),
(
210
,
120
),
(
x
,
y
)]
apple_x
=
360
apple_x
=
360
...
@@ -27,6 +31,7 @@ snake_head = right
...
@@ -27,6 +31,7 @@ snake_head = right
while
True
:
while
True
:
a
=
random
.
randint
(
0
,
10
)
a
=
random
.
randint
(
0
,
10
)
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
...
@@ -58,9 +63,12 @@ while True:
...
@@ -58,9 +63,12 @@ while True:
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
(
random
.
randint
(
0
,
630
))
//
30
*
30
apple_x
=
(
random
.
randint
(
0
,
630
))
//
30
*
30
apple_y
=
(
random
.
randint
(
0
,
450
))
//
30
*
30
apple_y
=
(
random
.
randint
(
0
,
450
))
//
30
*
30
score
+=
10
else
:
else
:
position
.
pop
(
0
)
position
.
pop
(
0
)
# if (apple_x,apple_y) in position[:len[position]-2]:
# apple_x=(random.randint(0,630))//30*30
# apple_y=(random.randint(0,450))//30*30
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
# 将贪吃蛇的头画上去
...
@@ -71,6 +79,9 @@ while True:
...
@@ -71,6 +79,9 @@ while True:
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
info
=
'Score:'
+
str
(
score
)
fen1
=
fen
.
render
(
info
,
True
,(
c
,
d
,
e
))
screen
.
blit
(
fen1
,(
480
,
0
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
a
)
FPSCLOCK
.
tick
(
a
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
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