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
0b147c2d
authored
Jun 25, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
2b884c94
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
11 deletions
snake.py
阿朱泡泡体.ttf
snake.py
View file @
0b147c2d
...
...
@@ -3,21 +3,29 @@ from pygame import *;
import
random
;
import
sys
;
setHeadingString
=
"right"
playerX
=
0
playerY
=
0
position
=
[(
playerX
,
playerY
)]
setHeading
=
"right"
;
fps
=
time
.
Clock
();
score
=
0
;
appleX
=
240
;
appleY
=
150
;
playerX
=
240
;
playerY
=
120
;
position
=
[(
210
,
120
),
(
210
,
150
),
(
playerX
,
playerY
)];
# 初始化pygame,为使用pygame做准备
init
();
background
=
image
.
load
(
'bg.png'
);
snake
=
image
.
load
(
'right.png'
);
right
=
image
.
load
(
'right.png'
);
left
=
image
.
load
(
'left.png'
);
up
=
image
.
load
(
'up.png'
);
down
=
image
.
load
(
'down.png'
);
body
=
image
.
load
(
'body.png'
);
apple
=
image
.
load
(
'apple.png'
);
setHeading
=
snake
myFont
=
font
.
Font
(
'阿朱泡泡体.ttf'
,
30
)
snakeHead
=
right
;
# 创建一个窗口
screen
=
display
.
set_mode
((
660
,
480
));
...
...
@@ -27,12 +35,59 @@ while 1:
if
events
.
type
==
QUIT
:
sys
.
exit
();
if
events
.
type
==
KEYDOWN
:
if
events
.
key
==
K_w
:
if
events
.
key
==
K_w
and
setHeading
!=
"down"
:
setHeading
=
"up"
;
snakeHead
=
up
;
if
events
.
key
==
K_s
and
setHeading
!=
"up"
:
setHeading
=
"down"
;
snakeHead
=
down
;
if
events
.
key
==
K_a
and
setHeading
!=
"right"
:
setHeading
=
"left"
;
snakeHead
=
left
;
if
events
.
key
==
K_d
and
setHeading
!=
"left"
:
setHeading
=
"right"
;
snakeHead
=
right
;
if
setHeading
==
"up"
:
playerY
-=
30
;
if
playerX
==
appleX
and
playerY
==
appleY
:
score
+=
30
;
appleX
=
random
.
randint
(
1
,
21
)
*
30
;
appleY
=
random
.
randint
(
1
,
17
)
*
30
;
elif
setHeading
==
"down"
:
playerY
+=
30
;
if
playerX
==
appleX
and
playerY
==
appleY
:
score
+=
30
;
appleX
=
random
.
randint
(
1
,
21
)
*
30
;
appleY
=
random
.
randint
(
1
,
17
)
*
30
;
elif
setHeading
==
"left"
:
playerX
-=
30
;
if
playerX
==
appleX
and
playerY
==
appleY
:
score
+=
30
;
appleX
=
random
.
randint
(
1
,
21
)
*
30
;
appleY
=
random
.
randint
(
1
,
17
)
*
30
;
else
:
playerX
+=
30
;
if
playerX
==
appleX
and
playerY
==
appleY
:
score
+=
30
;
appleX
=
random
.
randint
(
1
,
21
)
*
30
;
appleY
=
random
.
randint
(
1
,
17
)
*
30
;
position
.
append
((
playerX
,
playerY
));
position
.
pop
(
0
);
screen
.
blit
(
background
,
(
0
,
0
));
screen
.
blit
(
snake
,
(
240
,
120
));
screen
.
blit
(
body
,
(
210
,
120
));
screen
.
blit
(
apple
,
(
240
,
150
));
screen
.
blit
(
snakeHead
,
position
[
-
1
]);
screen
.
blit
(
apple
,
(
appleX
,
appleY
));
for
i
in
range
(
len
(
position
)
-
1
):
screen
.
blit
(
body
,
position
[
i
]);
display
.
update
();
fps
.
tick
(
3
)
\ No newline at end of file
阿朱泡泡体.ttf
0 → 100755
View file @
0b147c2d
File added
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