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
e45e03aa
authored
Jun 25, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
5d2278c0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
26 deletions
snake.py
snake.py
View file @
e45e03aa
import
pygame
from
pygame
import
locals
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
bg
=
pygame
.
image
.
load
(
"bg.png"
)
apple
=
pygame
.
image
.
load
(
"apple.png"
)
body
=
pygame
.
image
.
load
(
"body.png"
)
down
=
pygame
.
image
.
load
(
"down.png"
)
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
apple
,(
270
,
240
))
screen
.
blit
(
body
,(
240
,
90
))
screen
.
blit
(
body
,(
240
,
60
))
screen
.
blit
(
body
,(
240
,
30
))
screen
.
blit
(
body
,(
210
,
30
))
screen
.
blit
(
body
,(
180
,
30
))
screen
.
blit
(
body
,(
150
,
30
))
screen
.
blit
(
down
,(
240
,
120
))
pygame
.
display
.
update
()
import
pygame
from
pygame
import
locals
pygame
.
init
()
ticks
=
pygame
.
time
.
Clock
()
a
=
"right"
x
=
330
y
=
60
location
=
[(
270
,
90
),(
300
,
90
),(
300
,
60
),(
x
,
y
)]
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
apple
=
pygame
.
image
.
load
(
"apple.png"
)
bg
=
pygame
.
image
.
load
(
"bg.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"
)
up
=
pygame
.
image
.
load
(
"up.png"
)
b
=
right
while
True
:
for
i
in
pygame
.
event
.
get
():
if
i
.
type
==
locals
.
QUIT
:
exit
()
elif
i
.
type
==
locals
.
KEYDOWN
:
if
i
.
key
==
locals
.
K_RIGHT
and
a
!=
"left"
:
a
=
"right"
b
=
right
elif
i
.
key
==
locals
.
K_LEFT
and
a
!=
"right"
:
a
=
"left"
b
=
left
elif
i
.
key
==
locals
.
K_UP
and
a
!=
"down"
:
a
=
"up"
b
=
up
elif
i
.
key
==
locals
.
K_DOWN
and
a
!=
"up"
:
a
=
"down"
b
=
down
if
a
==
"right"
:
x
+=
30
elif
a
==
"left"
:
x
-=
30
elif
a
==
"up"
:
y
-=
30
elif
a
==
"down"
:
y
+=
30
location
.
append
((
x
,
y
))
location
.
pop
(
0
)
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
apple
,(
30
,
90
))
for
j
in
range
(
len
(
location
)
-
1
):
screen
.
blit
(
body
,
location
[
j
])
screen
.
blit
(
b
,
location
[
-
1
])
pygame
.
display
.
update
()
ticks
.
tick
(
5
)
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