Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson2_diy2
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
b17e1c4c
authored
Dec 11, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
cc3b049a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
5 deletions
bg.png
snake.py
bg.png
View file @
b17e1c4c
46.1 KB
|
W:
|
H:
22.5 KB
|
W:
|
H:
2-up
Swipe
Onion skin
snake.py
View file @
b17e1c4c
import
pygame
from
pygame
import
locals
import
random
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
...
...
@@ -12,9 +12,16 @@ background = pygame.image.load('bg.png')
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
my_font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
Score
=
0
x
,
y
=
240
,
120
apple_x
=
360
apple_y
=
300
setheading
=
"right"
snake_head
=
right
position
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
while
True
:
for
event
in
pygame
.
event
.
get
():
...
...
@@ -24,12 +31,16 @@ while True:
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_d
and
setheading
!=
"left"
:
setheading
=
"right"
snake_head
=
right
if
event
.
key
==
locals
.
K_a
and
setheading
!=
"right"
:
setheading
=
"left"
snake_head
=
left
if
event
.
key
==
locals
.
K_w
and
setheading
!=
"down"
:
setheading
=
"up"
snake_head
=
up
if
event
.
key
==
locals
.
K_s
and
setheading
!=
"up"
:
setheading
=
"down"
snake_head
=
down
if
setheading
==
"right"
:
x
+=
30
elif
setheading
==
"left"
:
...
...
@@ -39,16 +50,30 @@ while True:
else
:
y
+=
30
position
.
append
((
x
,
y
))
position
.
pop
(
0
)
if
x
==
apple_x
and
y
==
apple_y
:
num1
=
random
.
randint
(
1
,
22
)
num2
=
random
.
randint
(
1
,
16
)
apple_x
=
num1
*
30
-
30
apple_Y
=
num2
*
30
-
30
Score
+=
10
else
:
position
.
pop
(
0
)
if
x
<
0
or
x
>
660
or
y
<
0
or
y
>
480
:
exit
()
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇画上去
screen
.
blit
(
right
,
position
[
-
1
])
screen
.
blit
(
snake_head
,
position
[
-
1
])
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
360
,
300
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
info
=
"Score:"
+
str
(
Score
)
text
=
my_font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
# 刷新画面
pygame
.
display
.
update
()
FPSCLOCK
.
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