Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
lesson16_1
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
cf439404
authored
Aug 26, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
42baf734
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
10 deletions
R-C.jpg
snake.py
R-C.jpg
deleted
100644 → 0
View file @
42baf734
53.6 KB
snake.py
View file @
cf439404
import
pygame
import
pygame
import
sys
import
sys
import
random
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
head_x
=
60
head_y
=
60
position
=
[(
0
,
0
),(
30
,
0
),(
30
,
30
),(
head_x
,
head_y
)]
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1080
,
550
))
screen
=
pygame
.
display
.
set_mode
((
1080
,
550
))
background
=
pygame
.
image
.
load
(
"r_c.png"
)
background
=
pygame
.
image
.
load
(
"r_c.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
right
=
pygame
.
image
.
load
(
"right.png"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
apple
=
pygame
.
image
.
load
(
"apple.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"
)
FPSCLOCK
=
pygame
.
time
.
Clock
()
heading
=
"right"
snake_head
=
right
apple_x
=
540
apple_y
=
150
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
pygame
.
QUIT
:
if
event
.
type
==
pygame
.
QUIT
:
sys
.
exit
()
sys
.
exit
()
if
event
.
type
==
pygame
.
KEYDOWN
:
if
event
.
key
==
pygame
.
K_w
and
heading
!=
"down"
:
heading
=
"up"
if
event
.
key
==
pygame
.
K_s
and
heading
!=
"up"
:
heading
=
"down"
if
event
.
key
==
pygame
.
K_a
and
heading
!=
"right"
:
heading
=
"left"
if
event
.
key
==
pygame
.
K_d
and
heading
!=
"left"
:
heading
=
"right"
screen
.
blit
(
background
,(
0
,
0
))
screen
.
blit
(
background
,(
0
,
0
))
screen
.
blit
(
body
,(
0
,
0
))
screen
.
blit
(
apple
,(
apple_x
,
apple_y
))
screen
.
blit
(
body
,(
30
,
0
))
screen
.
blit
(
body
,(
30
,
30
))
if
heading
==
"right"
:
screen
.
blit
(
body
,(
30
,
60
))
head_x
+=
30
screen
.
blit
(
right
,(
60
,
60
))
snake_head
=
right
screen
.
blit
(
apple
,(
525
,
140
))
if
heading
==
"left"
:
pygame
.
display
.
update
()
head_x
-=
30
\ No newline at end of file
snake_head
=
left
if
heading
==
"up"
:
head_y
-=
30
snake_head
=
up
if
heading
==
"down"
:
head_y
+=
30
snake_head
=
down
position
.
append
((
head_x
,
head_y
))
if
head_x
==
apple_x
and
head_y
==
apple_y
:
apple_x
=
random
.
randint
(
0
,
21
)
*
30
apple_y
=
random
.
randint
(
0
,
15
)
*
30
else
:
position
.
pop
(
0
)
screen
.
blit
(
snake_head
,(
head_x
,
head_y
))
for
index
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
index
])
FPSCLOCK
.
tick
(
10
)
pygame
.
display
.
update
()
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