Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_diy2
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
dcfcd195
authored
Mar 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
20a2c085
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
20 deletions
snake.py
snake.py
View file @
dcfcd195
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
FPSCLOCK
=
pygame
.
time
.
Clock
()
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
),
0
,
32
)
background
=
pygame
.
image
.
load
(
"bg.png"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
down
=
pygame
.
image
.
load
(
"down.png"
)
left
=
pygame
.
image
.
load
(
"left.png"
)
right
=
pygame
.
image
.
load
(
"right.png"
)
x
,
y
=
240
,
120
while
True
:
import
random
caption_width
=
500
#画布宽度
caption_height
=
500
#画布高度
white_color
=
(
255
,
255
,
255
)
# 白色rgb
black_color
=
(
0
,
0
,
0
)
game_title
=
'蛇蛇兄弟'
cell
=
10
# 格子
snake_init_pos
=
[[
250
,
250
],
[
240
,
250
],
[
230
,
250
],
[
220
,
250
]]
# 蛇的初始位置
food_pos
=
[
random
.
randrange
(
1
,
50
)
*
10
,
random
.
randrange
(
1
,
50
)
*
10
]
# 食物初始随机位置
head_pos
=
[
250
,
250
]
pygame
.
init
()
# 初始化 pygame
clock
=
pygame
.
time
.
Clock
()
caption
=
pygame
.
display
.
set_mode
((
caption_width
,
caption_height
))
pygame
.
display
.
set_caption
(
game_title
)
def
draw_rect
(
color
,
position
):
pygame
.
draw
.
rect
(
caption
,
color
,
pygame
.
Rect
(
position
[
0
],
position
[
1
],
cell
,
cell
))
def
hit_the_self
():
if
snake_init_pos
[
0
]
in
snake_init_pos
[
1
:]:
return
True
else
:
return
False
def
hit_the_wall
(
head_pos
):
if
head_pos
[
0
]
>=
caption_width
or
head_pos
[
0
]
<
0
or
head_pos
[
1
]
>=
caption_height
or
head_pos
[
1
]
<
0
:
return
True
else
:
return
False
def
change_direction
(
head_pos
):
global
food_pos
snake_init_pos
.
insert
(
0
,
list
(
head_pos
))
if
head_pos
!=
food_pos
:
snake_init_pos
.
pop
()
else
:
food_pos
=
[
random
.
randrange
(
1
,
50
)
*
10
,
random
.
randrange
(
1
,
50
)
*
10
]
if
hit_the_self
()
or
hit_the_wall
(
head_pos
):
# 给我死
pygame
.
quit
()
def
main
():
for
pos
in
snake_init_pos
:
draw_rect
(
white_color
,
pos
)
draw_rect
(
white_color
,
food_pos
)
pygame
.
display
.
update
()
while
1
:
for
event
in
pygame
.
event
.
get
():
print
(
event
)
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
pygame
.
QUIT
:
pygame
.
quit
()
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_LEFT
:
head_pos
[
0
]
-=
cell
change_direction
(
head_pos
)
elif
event
.
key
==
pygame
.
K_RIGHT
:
head_pos
[
0
]
+=
cell
change_direction
(
head_pos
)
elif
event
.
key
==
pygame
.
K_UP
:
head_pos
[
1
]
-=
cell
change_direction
(
head_pos
)
elif
event
.
key
==
pygame
.
K_DOWN
:
head_pos
[
1
]
+=
cell
change_direction
(
head_pos
)
caption
.
fill
(
black_color
)
draw_rect
(
white_color
,
food_pos
)
for
pos
in
snake_init_pos
:
draw_rect
(
white_color
,
pos
)
pygame
.
display
.
update
()
clock
.
tick
(
300
)
if
__name__
==
'__main__'
:
main
()
\ 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