Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson1_4
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
2148dfae
authored
Jul 31, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
05767b43
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
123 additions
and
34 deletions
EZ_snake.py
snake.py
EZ_snake.py
0 → 100644
View file @
2148dfae
import
pygame
import
random
from
pygame
import
locals
seth
=
[
'left'
,
'right'
,
'up'
,
'down'
]
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
bg
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
FPS
=
pygame
.
time
.
Clock
()
x
,
y
=
240
,
120
z1
,
a1
=
540
,
300
pos
=
[(
180
,
120
),(
210
,
120
),(
x
,
y
)]
# 创建一个窗口
seth
=
'right'
EWSN
=
right
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_w
:
if
seth
!=
'down'
:
seth
=
'up'
EWSN
=
up
if
event
.
key
==
locals
.
K_s
:
if
seth
!=
'up'
:
seth
=
'down'
EWSN
=
down
if
event
.
key
==
locals
.
K_a
:
if
seth
!=
'right'
:
seth
=
'left'
EWSN
=
left
if
event
.
key
==
locals
.
K_d
:
if
seth
!=
'left'
:
seth
=
'right'
EWSN
=
right
if
seth
==
'up'
:
y
-=
30
if
seth
==
'down'
:
y
+=
30
if
seth
==
'left'
:
x
-=
30
if
seth
==
'right'
:
x
+=
30
pos
.
append
((
x
,
y
))
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
EWSN
,(
x
,
y
))
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,
pos
[
i
])
if
x
==
z1
and
y
==
a1
:
r_x
=
x
+
60
z1
=
r_x
else
:
screen
.
blit
(
food
,(
z1
,
a1
))
pygame
.
display
.
update
()
FPS
.
tick
(
3
)
if
x
>=
660
or
y
>=
480
:
exit
()
\ No newline at end of file
snake.py
View file @
2148dfae
...
...
@@ -4,6 +4,9 @@ from pygame import locals
seth
=
[
'left'
,
'right'
,
'up'
,
'down'
]
# 初始化pygame,为使用pygame做准备
pygame
.
init
()
s
=
0
speed
=
1
mfont
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
16
)
bg
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
...
...
@@ -13,33 +16,31 @@ food=pygame.image.load('apple.png')
body
=
pygame
.
image
.
load
(
'body.png'
)
FPS
=
pygame
.
time
.
Clock
()
x
,
y
=
240
,
120
z
,
a
=
540
,
300
pos
=
[(
180
,
90
),(
180
,
120
),(
210
,
120
),(
x
,
y
)]
# 创建一个窗口
seth
=
'right'
EWSN
=
right
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_w
:
if
seth
!=
'down'
:
seth
=
'up'
EWSN
=
up
if
event
.
key
==
locals
.
K_s
:
if
seth
!=
'up'
:
seth
=
'down'
EWSN
=
down
if
event
.
key
==
locals
.
K_a
:
if
seth
!=
'right'
:
seth
=
'left'
EWSN
=
left
if
event
.
key
==
locals
.
K_d
:
if
seth
!=
'left'
:
seth
=
'right'
EWSN
=
right
z1
,
a1
=
540
,
300
pos
=
[(
180
,
120
),(
210
,
120
),(
x
,
y
)]
# 创建一个窗d
mf
=
mfont
.
render
(
str
(
s
),
True
,(
0
,
0
,
0
))
yf
=
mfont
.
render
(
''
,
True
,(
0
,
0
,
0
))
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_w
:
if
seth
!=
'down'
:
seth
=
'up'
EWSN
=
up
if
event
.
key
==
locals
.
K_s
:
if
seth
!=
'up'
:
seth
=
'down'
EWSN
=
down
if
event
.
key
==
locals
.
K_a
:
if
seth
!=
'right'
:
seth
=
'left'
EWSN
=
left
if
event
.
key
==
locals
.
K_d
:
if
seth
!=
'left'
:
seth
=
'right'
EWSN
=
right
if
seth
==
'up'
:
y
-=
30
if
seth
==
'down'
:
...
...
@@ -49,14 +50,38 @@ while True:
if
seth
==
'right'
:
x
+=
30
pos
.
append
((
x
,
y
))
if
x
==
z
and
y
==
a
:
pass
else
:
pos
.
pop
(
0
)
screen
.
blit
(
bg
,(
0
,
0
))
screen
.
blit
(
EWSN
,(
x
,
y
))
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,
pos
[
i
])
screen
.
blit
(
food
,(
z
,
a
))
if
x
==
z1
and
y
==
a1
:
r_x
=
random
.
randint
(
1
,
21
)
r_y
=
random
.
randint
(
1
,
15
)
z
=
r_x
*
30
a
=
r_y
*
30
z1
=
z
y1
=
y
s
+=
1
print
(
s
)
else
:
pos
.
pop
(
0
)
screen
.
blit
(
food
,(
z1
,
a1
))
screen
.
blit
(
mf
,(
640
,
20
))
screen
.
blit
(
yf
,(
640
,
60
))
pygame
.
display
.
update
()
FPS
.
tick
(
3
)
\ No newline at end of file
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_f
:
speed
+=
1
if
speed
>=
1
and
event
.
key
==
locals
.
K_e
:
speed
-=
1
if
speed
==
2
:
FPS
.
tick
(
6
)
else
:
FPS
.
tick
(
3
)
# for i in range(len(pos)-1):
# for b in range(i):
# for c in pos:
# pass
#if x>=660 or y>=480 or x<=0 or y<=0:
# exit()
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