Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy4
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
b57da0b6
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
90221fc3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
5 deletions
snake.py
路障2.png
snake.py
View file @
b57da0b6
...
@@ -9,7 +9,7 @@ pygame.init()
...
@@ -9,7 +9,7 @@ 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时钟,控制游戏速度(帧数)
ticks
=
5
# 背景
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
...
@@ -18,12 +18,16 @@ body = pygame.image.load('body.png') # 身体
...
@@ -18,12 +18,16 @@ 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'
)
# 头 朝下
roadblock
=
pygame
.
image
.
load
(
"路障2.png"
)
font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
18
)
font
=
pygame
.
font
.
Font
(
"neuropol.ttf"
,
18
)
score
=
0
score
=
0
x
,
y
=
240
,
120
x
,
y
=
240
,
120
position
=
[(
x
,
y
)]
position
=
[(
x
,
y
)]
apple_x
=
360
apple_x
=
360
apple_y
=
300
apple_y
=
300
roadblock_x
=
300
roadblock_y
=
240
roadblock_position
=
[(
roadblock_x
,
roadblock_y
)]
setheading
=
"right"
setheading
=
"right"
snake_head
=
right
snake_head
=
right
...
@@ -60,34 +64,52 @@ while True:
...
@@ -60,34 +64,52 @@ while True:
if
x
>
630
or
y
>
450
or
x
<
0
or
y
<
0
:
if
x
>
630
or
y
>
450
or
x
<
0
or
y
<
0
:
exit
()
exit
()
for
i
in
roadblock_position
:
if
i
==
(
x
,
y
):
exit
()
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
num1
=
random
.
randint
(
1
,
22
)
num1
=
random
.
randint
(
1
,
22
)
num2
=
random
.
randint
(
1
,
16
)
num2
=
random
.
randint
(
1
,
16
)
apple_x
=
num1
*
30
-
30
apple_x
=
num1
*
30
-
30
apple_y
=
num2
*
30
-
30
apple_y
=
num2
*
30
-
30
if
len
(
roadblock_position
)
<
10
:
num3
=
random
.
randint
(
1
,
22
)
num4
=
random
.
randint
(
1
,
16
)
if
num3
==
num1
and
num2
==
num4
or
num3
==
x
and
num4
==
y
:
num3
=
random
.
randint
(
1
,
22
)
num4
=
random
.
randint
(
1
,
16
)
roadblock_x
=
num3
*
30
-
30
roadblock_y
=
num4
*
30
-
30
roadblock_position
.
append
((
roadblock_x
,
roadblock_y
))
score
+=
1
score
+=
1
else
:
else
:
position
.
pop
(
0
)
position
.
pop
(
0
)
info
=
"SCORE:"
+
str
(
score
)
info
=
"SCORE:"
+
str
(
score
)
a
=
font
.
render
(
info
,
True
,(
0
,
0
,
0
))
a
=
font
.
render
(
info
,
True
,(
0
,
0
,
0
))
# 将背景图画上去
# 将背景图画上去
c
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
])
for
i
in
range
(
len
(
roadblock_position
)
-
1
):
screen
.
blit
(
roadblock
,
roadblock_position
[
i
])
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
screen
.
blit
(
a
,
(
0
,
0
))
screen
.
blit
(
a
,
(
0
,
0
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
ticks
)
\ No newline at end of file
\ No newline at end of file
This diff is collapsed.
Click to expand it.
路障2.png
0 → 100644
View file @
b57da0b6
858 Bytes
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