Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_3
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
a0a25e43
authored
Jan 16, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
e1b1eefe
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
3 deletions
snake.py
snake.py
View file @
a0a25e43
...
...
@@ -7,6 +7,11 @@ background = pygame.image.load('bg.png')
right
=
pygame
.
image
.
load
(
'right.png'
)
apple
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
setheading
=
"right"
snake_head
=
right
# 创建一个窗口
x
,
y
=
240
,
120
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
...
...
@@ -15,11 +20,32 @@ while True:
print
(
event
)
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
"down"
:
setheading
=
"up"
snake_head
=
up
elif
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
"up"
:
setheading
=
"down"
snake_head
=
down
elif
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
"right"
:
setheading
=
"left"
snake_head
=
left
else
:
setheading
=
"right"
snake_head
=
right
if
setheading
==
"right"
:
x
+=
30
if
setheading
==
"left"
:
x
-=
30
if
setheading
==
"down"
:
y
+=
30
if
setheading
==
"up"
:
y
-=
30
screen
.
blit
(
background
,(
0
,
0
))
x
+=
30
pos
.
append
((
x
,
y
))
pos
.
pop
(
0
)
screen
.
blit
(
right
,
pos
[
-
1
])
screen
.
blit
(
snake_head
,
pos
[
-
1
])
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,(
pos
[
i
]))
# screen.blit(body,(150,120))
...
...
@@ -29,4 +55,4 @@ while True:
screen
.
blit
(
apple
,(
120
,
300
))
pygame
.
display
.
update
()
aaz
=
pygame
.
time
.
Clock
()
aaz
.
tick
(
3
)
aaz
.
tick
(
10
)
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