Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy1
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
de90e8cb
authored
Nov 26, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
da5a92d2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
63 additions
and
8 deletions
snake.py
snake.py
View file @
de90e8cb
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
# 初始化pygame,为使用硬件做准备
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
pygame
.
init
()
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
#660, 480
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
# 背景
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
food
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
food
=
pygame
.
image
.
load
(
'apple.png'
)
food1
=
pygame
.
image
.
load
(
'apple.png'
)
food2
=
pygame
.
image
.
load
(
'apple.png'
)
food3
=
pygame
.
image
.
load
(
'apple.png'
)
food4
=
pygame
.
image
.
load
(
'apple.png'
)
food5
=
pygame
.
image
.
load
(
'apple.png'
)
food6
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
body
=
pygame
.
image
.
load
(
'body.png'
)
# 身体
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'
)
# 头 朝下
ziti
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
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
,
apple_y
=
360
,
300
apple_x1
,
apple_y1
=
360
,
300
apple_x2
,
apple_y2
=
360
,
300
apple_x3
,
apple_y3
=
360
,
300
apple_x4
,
apple_y4
=
360
,
300
apple_x5
,
apple_y5
=
360
,
300
apple_x6
,
apple_y6
=
360
,
300
setheading
=
"right"
setheading
=
"right"
fenshu
=
0
snake_head
=
right
snake_head
=
right
while
True
:
while
True
:
...
@@ -52,7 +68,37 @@ while True:
...
@@ -52,7 +68,37 @@ while True:
else
:
else
:
y
+=
30
y
+=
30
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
random
.
randint
(
0
,
21
)
*
30
apple_y
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
if
x
==
apple_x1
and
y
==
apple_y1
:
apple_x1
=
random
.
randint
(
0
,
21
)
*
30
apple_y1
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
if
x
==
apple_x3
and
y
==
apple_y3
:
apple_x3
=
random
.
randint
(
0
,
21
)
*
30
apple_y3
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
if
x
==
apple_x4
and
y
==
apple_y4
:
apple_x4
=
random
.
randint
(
0
,
21
)
*
30
apple_y4
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
if
x
==
apple_x6
and
y
==
apple_y6
:
apple_x6
=
random
.
randint
(
0
,
21
)
*
30
apple_y6
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
if
x
==
apple_x5
and
y
==
apple_y5
:
apple_x5
=
random
.
randint
(
0
,
21
)
*
30
apple_y5
=
random
.
randint
(
0
,
15
)
*
30
fenshu
+=
100
else
:
position
.
pop
(
0
)
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇的头画上去
# 将贪吃蛇的头画上去
...
@@ -62,7 +108,16 @@ while True:
...
@@ -62,7 +108,16 @@ while True:
screen
.
blit
(
body
,
position
[
i
])
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
screen
.
blit
(
food1
,
(
apple_x1
,
apple_y1
))
screen
.
blit
(
food2
,
(
apple_x2
,
apple_y2
))
screen
.
blit
(
food3
,
(
apple_x3
,
apple_y3
))
screen
.
blit
(
food4
,
(
apple_x4
,
apple_y4
))
screen
.
blit
(
food5
,
(
apple_x5
,
apple_y5
))
screen
.
blit
(
food6
,
(
apple_x6
,
apple_y6
))
info
=
"scoro"
+
str
(
fenshu
)
text
=
ziti
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
3
)
FPSCLOCK
.
tick
(
5
)
\ No newline at end of file
\ 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