Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson3_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
ba8b8e62
authored
2 years ago
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
8f3f1cad
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
6 deletions
snake.py
snake.py
View file @
ba8b8e62
import
random
import
random
import
pygame
import
pygame
pygame
.
font
.
init
()
from
pygame
import
locals
from
pygame
import
locals
x
,
y
=
240
,
120
x
,
y
=
240
,
120
apple_x
,
apple_y
=
360
,
300
apple_x
,
apple_y
=
360
,
300
...
@@ -17,7 +16,9 @@ pygame.init()
...
@@ -17,7 +16,9 @@ pygame.init()
sj
=
pygame
.
time
.
Clock
()
sj
=
pygame
.
time
.
Clock
()
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
# 背景
# 背景
a
=
pygame
.
image
.
load
(
'a.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
...
@@ -28,6 +29,7 @@ down = pygame.image.load('down.png')
...
@@ -28,6 +29,7 @@ down = pygame.image.load('down.png')
z
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
z
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
fx
=
'right'
fx
=
'right'
snakehead
=
right
snakehead
=
right
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
print
(
event
)
print
(
event
)
...
@@ -47,6 +49,7 @@ while True:
...
@@ -47,6 +49,7 @@ while True:
if
event
.
key
==
locals
.
K_DOWN
and
fx
!=
'up'
:
if
event
.
key
==
locals
.
K_DOWN
and
fx
!=
'up'
:
fx
=
'down'
fx
=
'down'
snakehead
=
down
snakehead
=
down
if
fx
==
'right'
:
if
fx
==
'right'
:
x
+=
30
x
+=
30
elif
fx
==
'left'
:
elif
fx
==
'left'
:
...
@@ -55,9 +58,8 @@ while True:
...
@@ -55,9 +58,8 @@ while True:
y
-=
30
y
-=
30
else
:
else
:
y
+=
30
y
+=
30
if
x
<
0
or
x
>
630
or
y
<
0
or
y
>
450
:
exit
()
# x+=30
# x+=30
z
.
append
((
x
,
y
))
z
.
append
((
x
,
y
))
if
x
==
apple_x
and
y
==
apple_y
:
if
x
==
apple_x
and
y
==
apple_y
:
...
@@ -68,6 +70,7 @@ while True:
...
@@ -68,6 +70,7 @@ while True:
# screen.blit(body,())
# screen.blit(body,())
# z.pop(0)
# z.pop(0)
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇画上去
# 将贪吃蛇画上去
screen
.
blit
(
snakehead
,
(
x
,
y
))
screen
.
blit
(
snakehead
,
(
x
,
y
))
...
@@ -79,7 +82,9 @@ while True:
...
@@ -79,7 +82,9 @@ while True:
# screen.blit(body, (180,90))
# screen.blit(body, (180,90))
# 将果实画上去
# 将果实画上去
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
screen
.
blit
(
food
,
(
apple_x
,
apple_y
))
pygame
.
font
.
init
()
#print(pygame.font.get_fonts())
#self.font=pygame.font.SysFont(None,48)
#self.font = pygame.font.SysFont('arial',48)
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
sj
.
tick
(
15
)
sj
.
tick
(
4
)
\ No newline at end of file
\ No newline at end of file
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