Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_diy3
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
d354f024
authored
Oct 10, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
69cdbf6b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
8 deletions
snake.py
snake.py
View file @
d354f024
...
@@ -22,7 +22,7 @@ apple_x=360
...
@@ -22,7 +22,7 @@ apple_x=360
apple_y
=
300
apple_y
=
300
setheading
=
"right"
#头部初始变量
setheading
=
"right"
#头部初始变量
sna
ck
_head
=
right
#头部初始朝向
sna
ke
_head
=
right
#头部初始朝向
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
#遍历事件
for
event
in
pygame
.
event
.
get
():
#遍历事件
...
@@ -31,16 +31,16 @@ while True:
...
@@ -31,16 +31,16 @@ while True:
elif
event
.
type
==
locals
.
KEYDOWN
:
elif
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
'left'
:
if
event
.
key
==
locals
.
K_RIGHT
and
setheading
!=
'left'
:
setheading
=
'right'
setheading
=
'right'
sna
ck
_head
=
right
sna
ke
_head
=
right
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
if
event
.
key
==
locals
.
K_LEFT
and
setheading
!=
'right'
:
setheading
=
'left'
setheading
=
'left'
sna
ck
_head
=
left
sna
ke
_head
=
left
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
'down'
:
if
event
.
key
==
locals
.
K_UP
and
setheading
!=
'down'
:
setheading
=
'up'
setheading
=
'up'
sna
ck
_head
=
up
sna
ke
_head
=
up
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
if
event
.
key
==
locals
.
K_DOWN
and
setheading
!=
'up'
:
setheading
=
'down'
setheading
=
'down'
sna
ck
_head
=
down
#判断按键
sna
ke
_head
=
down
#判断按键
if
setheading
==
'right'
:
if
setheading
==
'right'
:
x
+=
30
x
+=
30
elif
setheading
==
'left'
:
elif
setheading
==
'left'
:
...
@@ -51,9 +51,12 @@ while True:
...
@@ -51,9 +51,12 @@ while True:
y
+=
30
#移动
y
+=
30
#移动
position
.
append
((
x
,
y
))
position
.
append
((
x
,
y
))
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
apple_x
=
random
.
randint
(
0
,
660
)
num1
=
random
.
randint
(
0
,
22
)
apple_y
=
random
.
randint
(
0
,
480
)
num2
=
random
.
randint
(
0
,
16
)
position
.
pop
(
0
)
apple_x
=
num1
*
30
-
30
apple_y
=
num2
*
30
-
30
else
:
position
.
pop
(
0
)
screen
.
blit
(
background
,
(
0
,
0
))
#将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
#将背景图画上去
screen
.
blit
(
right
,
position
[
-
1
])
#将贪吃蛇画上去
screen
.
blit
(
right
,
position
[
-
1
])
#将贪吃蛇画上去
for
i
in
range
(
len
(
position
)
-
1
):
for
i
in
range
(
len
(
position
)
-
1
):
...
...
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