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
37b936b5
authored
May 27, 2023
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
0323848b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
9 deletions
snake.py
snake.py
View file @
37b936b5
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
import
random
# 初始化pygame,为使用pygame做准备
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
pygame
.
init
()
...
@@ -7,35 +8,54 @@ pygame.init()
...
@@ -7,35 +8,54 @@ pygame.init()
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
fps
=
pygame
.
time
.
Clock
()
fps
=
pygame
.
time
.
Clock
()
bg
=
pygame
.
image
.
load
(
'bg.png'
)
bg
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
apple
=
pygame
.
image
.
load
(
'apple.png'
)
apple
=
pygame
.
image
.
load
(
'apple.png'
)
snake
=
pygame
.
image
.
load
(
'right.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
x
=
60
x
=
60
y
=
420
y
=
420
pos
=
[(
60
,
360
),(
60
,
390
),(
60
,
420
),(
x
,
y
)]
pos
=
[(
60
,
360
),(
60
,
390
),(
60
,
420
),(
x
,
y
)]
head
=
'right'
head
=
'right'
a_x
=
360
a_y
=
300
tou
=
right
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
K_RIGHT
and
head
!=
'left'
:
if
event
.
key
==
locals
.
K_RIGHT
and
head
!=
'left'
:
head
=
'right'
head
=
'right'
if
event
.
key
==
K_RIGH
T
and
head
!=
'right'
:
if
event
.
key
==
locals
.
K_LEF
T
and
head
!=
'right'
:
head
=
'left'
head
=
'left'
if
event
.
key
==
K_RIGHT
and
head
!=
'down'
:
if
event
.
key
==
locals
.
K_UP
and
head
!=
'down'
:
head
=
'up'
head
=
'up'
if
event
.
key
==
K_RIGHT
and
head
!=
'up'
:
if
event
.
key
==
locals
.
K_DOWN
and
head
!=
'up'
:
head
=
'down'
head
=
'down'
x
+=
30
if
head
==
"right"
:
x
+=
30
tou
=
right
if
head
==
"left"
:
x
-=
30
tou
=
left
if
head
==
"down"
:
y
+=
30
tou
=
down
if
head
==
"up"
:
y
-=
30
tou
=
up
pos
.
append
((
x
,
y
))
pos
.
append
((
x
,
y
))
pos
.
pop
(
0
)
pos
.
pop
(
0
)
if
x
==
a_x
and
y
==
a_y
:
a_x
=
random
.
randint
(
0
,
22
)
*
30
a_y
=
random
.
randint
(
0
,
16
)
*
30
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
apple
,(
300
,
30
))
screen
.
blit
(
tou
,(
x
,
y
))
screen
.
blit
(
snake
,(
x
,
y
))
for
i
in
range
(
len
(
pos
)
-
1
):
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,
pos
[
i
])
screen
.
blit
(
body
,
pos
[
i
])
screen
.
blit
(
apple
,(
a_x
,
a_y
))
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