Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_diy1
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
139ca2e6
authored
3 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
4aa4d8b5
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
5 deletions
snake.py
snake.py
View file @
139ca2e6
import
pygame
,
random
import
pygame
,
random
,
os
from
pygame
import
locals
score
=
0
# 初始化pygame,为使用pygame做准备
def
shuax
():
global
apple_x
global
apple_y
apple_x
=
30
*
random
.
randint
(
0
,
20
)
apple_y
=
30
*
random
.
randint
(
0
,
16
)
pygame
.
init
()
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
...
...
@@ -19,6 +27,7 @@ FPSCLOCK = pygame.time.Clock()
x
,
y
=
240
,
120
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
...
...
@@ -47,20 +56,41 @@ while True:
elif
setheading
==
"down"
:
y
+=
30
if
(
x
,
y
)
==
(
apple_x
,
apple_y
):
print
()
apple_x
=
30
*
random
.
randint
(
0
,
20
)
apple_y
=
30
*
random
.
randint
(
0
,
16
)
shuax
()
score
+=
1
# os.system('cls')
# print('score:'+str(score))
if
(
apple_x
,
apple_y
)
in
pos
:
shuax
()
else
:
pos
.
pop
(
0
)
pos
.
pop
(
0
)
if
(
x
,
y
)
in
pos
[
0
:
-
2
]:
exit
()
pos
.
append
((
x
,
y
))
screen
.
blit
(
background
,(
0
,
0
))
screen
.
blit
(
snake_head
,
pos
[
-
1
])
screen
.
blit
(
food
,(
apple_x
,
apple_y
))
for
i
in
pos
[
0
:
-
1
]:
screen
.
blit
(
body
,
i
)
info
=
'score:'
+
str
(
score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
pygame
.
display
.
update
()
if
x
>
660
:
(
x
,
y
)
=
(
0
,
y
)
elif
x
<
0
:
(
x
,
y
)
=
(
680
,
y
)
elif
y
>
480
:
(
x
,
y
)
=
(
x
,
0
)
elif
y
<
0
:
(
x
,
y
)
=
(
x
,
480
)
FPSCLOCK
.
tick
(
3
)
...
...
This diff is collapsed.
Click to expand it.
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