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
06b3687b
authored
Jul 28, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
d5739bd2
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
117 additions
and
2 deletions
bapple.png
ddd.py
snake - 副本.py
snake.py
bapple.png
0 → 100644
View file @
06b3687b
334 Bytes
ddd.py
0 → 100644
View file @
06b3687b
snake - 副本.py
0 → 100644
View file @
06b3687b
import
pygame
,
random
import
pygame
,
random
from
pygame
import
locals
# 初始化pygame,为使用硬件做准备
pygame
.
init
()
score
=
0
o
=
5
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
660
,
480
))
FPSCLOCK
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
food
=
pygame
.
image
.
load
(
'apple.png'
)
body
=
pygame
.
image
.
load
(
'body.png'
)
bapple
=
pygame
.
image
.
load
(
'bapple.png'
)
left
=
pygame
.
image
.
load
(
'left.png'
)
down
=
pygame
.
image
.
load
(
'down.png'
)
up
=
pygame
.
image
.
load
(
'up.png'
)
font
=
pygame
.
font
.
Font
(
'neuropol.ttf'
,
18
)
x
,
y
=
240
,
120
#
x1
,
y1
=
360
,
300
x2
,
y2
=
390
,
270
pos
=
[(
180
,
90
),
(
180
,
120
),(
210
,
120
),(
x
,
y
)]
sethead
=
'right'
snake_head
=
right
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_RIGHT
and
sethead
!=
'left'
:
sethead
=
'right'
snake_head
=
right
if
event
.
key
==
locals
.
K_LEFT
and
sethead
!=
'right'
:
sethead
=
'left'
snake_head
=
left
if
event
.
key
==
locals
.
K_DOWN
and
sethead
!=
'up'
:
sethead
=
'down'
snake_head
=
down
if
event
.
key
==
locals
.
K_UP
and
sethead
!=
'down'
:
sethead
=
'up'
snake_head
=
up
if
event
.
key
==
o
:
o
+=
10
if
sethead
==
'right'
:
x
+=
30
elif
sethead
==
'left'
:
x
-=
30
elif
sethead
==
'up'
:
y
-=
30
else
:
y
+=
30
if
(
x
,
y
)
==
(
x1
,
y1
):
score
+=
10
x1
=
30
*
random
.
randint
(
0
,
20
)
y1
=
30
*
random
.
randint
(
0
,
16
)
else
:
pos
.
pop
(
0
)
while
(
x1
,
y1
)
in
pos
:
x1
=
30
*
random
.
randint
(
0
,
20
)
y1
=
30
*
random
.
randint
(
0
,
16
)
if
(
x
,
y
)
==
(
x2
,
y2
):
score
-=
10
x2
=
30
*
random
.
randint
(
0
,
20
)
y2
=
30
*
random
.
randint
(
0
,
16
)
else
:
pos
.
append
((
x
,
y
))
#if x>660 or y>480 or x<0 or y<0:
if
x
<
0
:
x
=
660
if
x
>
660
:
x
=
-
30
if
y
<
0
:
y
=
480
if
y
>
480
:
y
=
-
30
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 将贪吃蛇画上去
screen
.
blit
(
snake_head
,
pos
[
-
1
])
#
# 将贪吃蛇的身体画上去
for
i
in
range
(
len
(
pos
)
-
1
):
screen
.
blit
(
body
,
pos
[
i
])
# 将果实画上去
screen
.
blit
(
food
,
(
x1
,
y1
))
info
=
'Score'
+
str
(
score
)
text
=
font
.
render
(
info
,
True
,(
0
,
0
,
0
))
screen
.
blit
(
text
,(
540
,
10
))
screen
.
blit
(
bapple
,
(
x2
,
y2
))
# 刷新画面
pygame
.
display
.
update
()
FPSCLOCK
.
tick
(
o
)
\ No newline at end of file
snake.py
View file @
06b3687b
...
...
@@ -12,10 +12,11 @@ FPSCLOCK = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
background
=
pygame
.
image
.
load
(
'bg.png'
)
right
=
pygame
.
image
.
load
(
'right.png'
)
# 头 朝右
food
=
pygame
.
image
.
load
(
'apple.png'
)
# 食物 苹果
body
=
pygame
.
image
.
load
(
'body.png
'
)
# 身体
body
=
pygame
.
image
.
load
(
'body.png) # 身体
left = pygame.image.load('
left
.
png
') # 头 朝左
up = pygame.image.load('
up
.
png
') # 头 朝上
down
=
pygame
.
image
.
load
(
'down.png'
)
# 头 朝下
down = pygame.image.load('
down
.
png
')
font = pygame.font.Font('
neuropol
.
ttf
',18) # 头 朝下
x, y = 240, 120
position = [(180, 90), (180, 120), (210, 120), (x, y)]
...
...
@@ -63,6 +64,7 @@ while True:
# 将果实画上去
screen.blit(food, (360, 300))
# 刷新画面
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