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
c64bff18
authored
Jul 17, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
00df811d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
184 additions
and
148 deletions
e.py
snake.py
e.py
View file @
c64bff18
import
pygame
import
pygame
import
sys
from
os
import
path
import
time
from
sys
import
exit
import
random
from
time
import
sleep
from
pygame.locals
import
*
from
random
import
choice
from
itertools
import
product
# 初始化pygame
from
pygame.locals
import
QUIT
,
KEYDOWN
pygame
.
init
()
# 这是游戏框
fps_clock
=
pygame
.
time
.
Clock
()
def
direction_check
(
moving_direction
,
change_direction
):
play_sur_face
=
pygame
.
display
.
set_mode
((
640
,
480
))
directions
=
[[
'up'
,
'down'
],
[
'left'
,
'right'
]]
# 设置标题
if
moving_direction
in
directions
[
0
]
and
change_direction
in
directions
[
1
]:
pygame
.
display
.
set_caption
(
"小蛇冲冲冲!!!"
)
return
change_direction
# 加载图标
elif
moving_direction
in
directions
[
1
]
and
change_direction
in
directions
[
0
]:
image
=
pygame
.
image
.
load
(
"game.jpg"
)
return
change_direction
pygame
.
display
.
set_icon
(
image
)
return
moving_direction
# 需要自定义设置一些颜色
class
Snake
:
RED_COLOR
=
pygame
.
Color
(
255
,
0
,
0
)
BLACK_COLOR
=
pygame
.
Color
(
0
,
0
,
0
)
colors
=
list
(
product
([
0
,
64
,
128
,
192
,
255
],
repeat
=
3
))[
1
:
-
1
]
WITHER_COLOR
=
pygame
.
Color
(
255
,
255
,
255
)
GRE_COLOR
=
pygame
.
Color
(
150
,
150
,
150
)
def
__init__
(
self
):
LIGHT_GRE
=
pygame
.
Color
(
220
,
220
,
220
)
self
.
map
=
{(
x
,
y
):
0
for
x
in
range
(
32
)
for
y
in
range
(
24
)}
self
.
body
=
[[
100
,
100
],
[
120
,
100
],
[
140
,
100
]]
self
.
head
=
[
140
,
100
]
# 游戏结束
self
.
food
=
[]
def
game_over
(
play_sur_face
,
score
):
self
.
food_color
=
[]
# 显示GAME OVER 并定义字体以及大小
self
.
moving_direction
=
'right'
# game_over_font = pygame.font.Font('arial.tff', 72)
self
.
speed
=
4
game_over_font
=
pygame
.
font
.
Font
(
'E:
\\
untitled
\\
arial.ttf'
,
72
)
self
.
generate_food
()
game_over_surf
=
game_over_font
.
render
(
"GAME OVER"
,
True
,
GRE_COLOR
)
self
.
game_started
=
False
game_over_rect
=
game_over_surf
.
get_rect
()
game_over_rect
.
midtop
=
(
320
,
125
)
def
check_game_status
(
self
):
play_sur_face
.
blit
(
game_over_surf
,
game_over_rect
)
if
self
.
body
.
count
(
self
.
head
)
>
1
:
# 显示分数并定义字体大小
return
True
score_font
=
pygame
.
font
.
Font
(
'E:
\\
untitled
\\
arial.ttf'
,
48
)
if
self
.
head
[
0
]
<
0
or
self
.
head
[
0
]
>
620
or
self
.
head
[
1
]
<
0
or
self
.
head
[
1
]
>
460
:
score_surf
=
score_font
.
render
(
'score '
+
str
(
score
),
True
,
GRE_COLOR
)
return
True
score_rect
=
score_surf
.
get_rect
()
return
False
score_rect
.
midtop
=
(
320
,
255
)
play_sur_face
.
blit
(
score_surf
,
score_rect
)
def
move_head
(
self
):
# 刷新页面
moves
=
{
pygame
.
display
.
flip
()
'right'
:
(
20
,
0
),
time
.
sleep
(
5
)
'up'
:
(
0
,
-
20
),
pygame
.
quit
()
'down'
:
(
0
,
20
),
sys
.
exit
()
'left'
:
(
-
20
,
0
)
}
step
=
moves
[
self
.
moving_direction
]
self
.
head
[
0
]
+=
step
[
0
]
snake_position
=
[
100
,
100
]
# 蛇头位置
self
.
head
[
1
]
+=
step
[
1
]
snake_h
=
[[
100
,
100
],
[
80
,
100
],
[
60
,
100
]]
# 初始长度,三个单位
tree_position
=
[
300
,
300
]
def
generate_food
(
self
):
# 初始化树莓的数量
self
.
speed
=
len
(
self
.
body
)
//
16
if
len
(
self
.
body
)
//
16
>
4
else
self
.
speed
tree
=
1
for
seg
in
self
.
body
:
direction
=
'right'
# 初始化方向
x
,
y
=
seg
change_direction
=
direction
self
.
map
[
x
//
20
,
y
//
20
]
=
1
score
=
0
empty_pos
=
[
pos
for
pos
in
self
.
map
.
keys
()
if
not
self
.
map
[
pos
]]
# 检测例如按键等pygame事件
result
=
choice
(
empty_pos
)
while
True
:
self
.
food_color
=
list
(
choice
(
self
.
colors
))
self
.
food
=
[
result
[
0
]
*
20
,
result
[
1
]
*
20
]
def
main
():
key_direction_dict
=
{
119
:
'up'
,
# W
115
:
'down'
,
# S
97
:
'left'
,
# A
100
:
'right'
,
# D
273
:
'up'
,
# UP
274
:
'down'
,
# DOWN
276
:
'left'
,
# LEFT
275
:
'right'
,
# RIGHT
}
fps_clock
=
pygame
.
time
.
Clock
()
pygame
.
init
()
pygame
.
mixer
.
init
()
snake
=
Snake
()
sound
=
False
if
path
.
exists
(
'eat.wav'
):
sound_wav
=
pygame
.
mixer
.
Sound
(
"eat.wav"
)
sound
=
True
title_font
=
pygame
.
font
.
SysFont
(
'arial'
,
32
)
welcome_words
=
title_font
.
render
(
'Welcome to My Snake'
,
True
,
(
0
,
0
,
0
),
(
255
,
255
,
255
))
tips_font
=
pygame
.
font
.
SysFont
(
'arial'
,
24
)
start_game_words
=
tips_font
.
render
(
'Click to Start Game'
,
True
,
(
0
,
0
,
0
),
(
255
,
255
,
255
))
close_game_words
=
tips_font
.
render
(
'Press ESC to Close'
,
True
,
(
0
,
0
,
0
),
(
255
,
255
,
255
))
gameover_words
=
title_font
.
render
(
'GAME OVER'
,
True
,
(
205
,
92
,
92
),
(
255
,
255
,
255
))
win_words
=
title_font
.
render
(
'THE SNAKE IS LONG ENOUGH AND YOU WIN!'
,
True
,
(
0
,
0
,
205
),
(
255
,
255
,
255
))
screen
=
pygame
.
display
.
set_mode
((
640
,
480
),
0
,
32
)
pygame
.
display
.
set_caption
(
'My Snake'
)
new_direction
=
snake
.
moving_direction
while
1
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
# print(event.type)
if
event
.
type
==
QUIT
:
if
event
.
type
==
QUIT
:
exit
()
pygame
.
quit
()
elif
event
.
type
==
KEYDOWN
:
sys
.
exit
()
if
event
.
key
==
27
:
elif
event
.
type
==
KEYDOWN
:
exit
()
# 判断键盘事件
if
snake
.
game_started
and
event
.
key
in
key_direction_dict
:
if
event
.
key
==
K_RIGHT
or
event
.
key
==
ord
(
'd'
):
direction
=
key_direction_dict
[
event
.
key
]
change_direction
=
'right'
new_direction
=
direction_check
(
snake
.
moving_direction
,
direction
)
if
event
.
key
==
K_LEFT
or
event
.
key
==
ord
(
'a'
):
elif
(
not
snake
.
game_started
)
and
event
.
type
==
pygame
.
MOUSEBUTTONDOWN
:
change_direction
=
'left'
x
,
y
=
pygame
.
mouse
.
get_pos
()
if
event
.
key
==
K_UP
or
event
.
key
==
ord
(
'w'
):
if
213
<=
x
<=
422
and
304
<=
y
<=
342
:
change_direction
=
"up"
snake
.
game_started
=
True
if
event
.
key
==
K_DOWN
or
event
.
key
==
ord
(
's'
):
screen
.
fill
((
255
,
255
,
255
))
change_direction
=
'down'
if
snake
.
game_started
:
if
event
.
key
==
K_ESCAPE
:
# 按esc键
snake
.
moving_direction
=
new_direction
# 在这里赋值,而不是在event事件的循环中赋值,避免按键太快
pygame
.
event
.
post
(
pygame
.
event
.
Event
(
QUIT
))
# 退出游戏
snake
.
move_head
()
snake
.
body
.
append
(
snake
.
head
[:])
if
change_direction
==
"right"
and
not
direction
==
'left'
:
if
snake
.
head
==
snake
.
food
:
direction
=
change_direction
if
sound
:
if
change_direction
==
"left"
and
not
direction
==
'right'
:
sound_wav
.
play
()
direction
=
change_direction
snake
.
generate_food
()
if
change_direction
==
"up"
and
not
direction
==
'down'
:
else
:
direction
=
change_direction
snake
.
body
.
pop
(
0
)
if
change_direction
==
"down"
and
not
direction
==
'up'
:
for
seg
in
snake
.
body
:
direction
=
change_direction
pygame
.
draw
.
rect
(
screen
,
[
0
,
0
,
0
],
[
seg
[
0
],
seg
[
1
],
20
,
20
],
0
)
pygame
.
draw
.
rect
(
screen
,
snake
.
food_color
,
[
snake
.
food
[
0
],
snake
.
food
[
1
],
20
,
20
],
0
)
# 根据放下移动蛇头坐标
if
snake
.
check_game_status
():
if
direction
==
'right'
:
screen
.
blit
(
gameover_words
,
(
241
,
310
))
snake_position
[
0
]
+=
20
pygame
.
display
.
update
()
if
direction
==
'left'
:
snake
=
Snake
()
snake_position
[
0
]
-=
20
new_direction
=
snake
.
moving_direction
if
direction
==
'up'
:
sleep
(
3
)
snake_position
[
1
]
-=
20
elif
len
(
snake
.
body
)
==
512
:
if
direction
==
'down'
:
screen
.
blit
(
win_words
,
(
33
,
210
))
snake_position
[
1
]
+=
20
pygame
.
display
.
update
()
snake_h
.
insert
(
0
,
list
(
snake_position
))
snake
=
Snake
()
new_direction
=
snake
.
moving_direction
# 判断是否吃到树莓
sleep
(
3
)
if
snake_position
[
0
]
==
tree_position
[
0
]
and
snake_position
[
1
]
==
tree_position
[
1
]:
tree
=
0
else
:
else
:
snake_h
.
pop
()
# 每次将最后一单位蛇身剔除列表
screen
.
blit
(
welcome_words
,
(
188
,
100
))
screen
.
blit
(
start_game_words
,
(
236
,
310
))
# 重新生成树莓
screen
.
blit
(
close_game_words
,
(
233
,
350
))
if
tree
==
0
:
pygame
.
display
.
update
()
x
=
random
.
randrange
(
1
,
32
)
fps_clock
.
tick
(
snake
.
speed
)
y
=
random
.
randrange
(
1
,
24
)
tree_position
=
[
int
(
20
*
x
),
int
(
20
*
y
)]
tree
=
1
if
__name__
==
'__main__'
:
score
+=
1
main
()
\ No newline at end of file
# 刷新显示层
# def sx_face():
# # 绘制pygame显示层
play_sur_face
.
fill
(
BLACK_COLOR
)
for
position
in
snake_h
[
1
:]:
# 蛇身为白色
pygame
.
draw
.
rect
(
play_sur_face
,
WITHER_COLOR
,
Rect
(
position
[
0
],
position
[
1
],
20
,
20
))
pygame
.
draw
.
rect
(
play_sur_face
,
LIGHT_GRE
,
Rect
(
snake_position
[
0
],
snake_position
[
1
],
20
,
20
))
# 蛇头为灰色
pygame
.
draw
.
rect
(
play_sur_face
,
RED_COLOR
,
Rect
(
tree_position
[
0
],
tree_position
[
1
],
20
,
20
))
# 刷新显示层
pygame
.
display
.
flip
()
# def check_is_alive():
# """
# 判断蛇是否死亡
# :return:
# """
if
snake_position
[
0
]
>
620
or
snake_position
[
0
]
<
0
:
# 超出左右边界
game_over
(
play_sur_face
,
score
)
if
snake_position
[
1
]
>
460
or
snake_position
[
1
]
<
0
:
# 超出上下边界
game_over
(
play_sur_face
,
score
)
for
snack_body
in
snake_h
[
1
:]:
if
snake_position
[
0
]
==
snack_body
[
0
]
==
snack_body
[
1
]
==
snake_position
[
1
]:
game_over
(
play_sur_face
,
score
)
if
len
(
snake_h
)
<
40
:
speed
=
6
+
len
(
snake_h
)
//
4
else
:
speed
=
16
fps_clock
.
tick
(
speed
)
\ No newline at end of file
snake.py
View file @
c64bff18
import
pygame
import
pygame
import
random
from
pygame
import
locals
from
pygame
import
locals
seth
=
[
'left'
,
'right'
,
'up'
,
'down'
]
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
bg
=
pygame
.
image
.
load
(
'bg.png'
)
bg
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.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'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
FPS
=
pygame
.
time
.
Clock
()
FPS
=
pygame
.
time
.
Clock
()
x
,
y
=
240
,
120
x
,
y
=
240
,
120
z
,
a
=
540
,
300
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
# 创建一个窗口
# 创建一个窗口
seth
=
'right'
EWSN
=
right
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
while
True
:
while
True
:
x
+=
30
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
print
(
event
)
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
exit
()
exit
()
if
event
.
type
==
locals
.
K_w
:
if
event
.
type
==
locals
.
KEYDOWN
:
pass
if
event
.
key
==
locals
.
K_w
:
if
seth
!=
'down'
:
seth
=
'up'
EWSN
=
up
if
event
.
key
==
locals
.
K_s
:
if
seth
!=
'up'
:
seth
=
'down'
EWSN
=
down
if
event
.
key
==
locals
.
K_a
:
if
seth
!=
'right'
:
seth
=
'left'
EWSN
=
left
if
event
.
key
==
locals
.
K_d
:
if
seth
!=
'left'
:
seth
=
'right'
EWSN
=
right
if
seth
==
'up'
:
y
-=
30
if
seth
==
'down'
:
y
+=
30
if
seth
==
'left'
:
x
-=
30
if
seth
==
'right'
:
x
+=
30
pos
.
append
((
x
,
y
))
if
x
==
z
and
y
==
a
:
pass
else
:
pos
.
pop
(
0
)
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
right
,(
x
,
y
))
screen
.
blit
(
EWSN
,(
x
,
y
))
screen
.
blit
(
body
,(
x
-
30
,
y
))
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,(
x
-
60
,
y
))
screen
.
blit
(
body
,
pos
[
i
])
screen
.
blit
(
body
,(
x
-
90
,
y
))
screen
.
blit
(
food
,(
z
,
a
))
screen
.
blit
(
food
,(
540
,
300
))
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
3
)
FPS
.
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