Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson7_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
243c73cb
authored
Jun 26, 2021
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
8291cfef
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
129 additions
and
6 deletions
1.py
my_game.py
1.py
0 → 100644
View file @
243c73cb
import
pygame
,
sys
,
random
from
pygame.locals
import
*
#定义颜色变量 目标方块的颜色
redColor
=
pygame
.
Color
(
250
,
0
,
0
)
#贪吃蛇的颜色
whiteColor
=
pygame
.
Color
(
255
,
255
,
255
)
#背景颜色
blackColor
=
pygame
.
Color
(
0
,
0
,
0
)
def
gameOver
():
pygame
.
quit
()
sys
.
exit
()
def
main
():
#初始化pygame
pygame
.
init
()
#控制游戏速度
fpsColck
=
pygame
.
time
.
Clock
()
#创建pygame显示层
playSurface
=
pygame
.
display
.
set_mode
((
640
,
480
))
pygame
.
display
.
set_caption
(
'贪吃蛇'
)
#初始化贪吃蛇的起始坐标
snakePosition
=
[
100
,
100
]
#初始化贪吃蛇的长度
snakeBody
=
[[
100
,
100
],[
80
,
100
],[
60
,
100
]]
#初始化目标方块的坐标
targetPosition
=
[
300
,
300
]
#初始化一个目标方块的标记 目的:用来判断是否吃掉这个目标方块
targerflag
=
1
#初始化方向
direction
=
'right'
#定义一个方向变量
changeDirection
=
direction
while
True
:
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
QUIT
:
pygame
.
quit
()
sys
.
exit
()
elif
event
.
type
==
KEYDOWN
:
if
event
.
key
==
K_RIGHT
:
changeDirection
=
'right'
if
event
.
key
==
K_LEFT
:
changeDirection
=
'left'
if
event
.
key
==
K_UP
:
changeDirection
=
'up'
if
event
.
key
==
K_DOWN
:
changeDirection
=
'down'
#对应键盘Esc键
if
event
.
key
==
K_ESCAPE
:
pygame
.
event
.
post
(
pygame
.
event
.
Event
(
QUIT
))
#确定方向
if
changeDirection
==
'left'
and
not
direction
==
'right'
:
direction
=
changeDirection
if
changeDirection
==
'right'
and
not
direction
==
'left'
:
direction
=
changeDirection
if
changeDirection
==
'up'
and
not
direction
==
'down'
:
direction
=
changeDirection
if
changeDirection
==
'down'
and
not
direction
==
'up'
:
direction
=
changeDirection
#根据方向移动蛇头的坐标
if
direction
==
'right'
:
snakePosition
[
0
]
+=
20
if
direction
==
'left'
:
snakePosition
[
0
]
-=
20
if
direction
==
'up'
:
snakePosition
[
1
]
-=
20
if
direction
==
'down'
:
snakePosition
[
1
]
+=
20
#增加蛇的长度
snakeBody
.
insert
(
0
,
list
(
snakePosition
))
#判断目标方块是否被吃掉
if
snakePosition
[
0
]
==
targetPosition
[
0
]
and
snakePosition
[
1
]
==
targetPosition
[
1
]:
targerflag
=
0
else
:
snakeBody
.
pop
()
if
targerflag
==
0
:
x
=
random
.
randrange
(
1
,
32
)
y
=
random
.
randrange
(
1
,
24
)
targetPosition
=
[
int
(
x
*
20
),
int
(
y
*
20
)]
targerflag
=
1
#绘制pygame的显示层
playSurface
.
fill
(
blackColor
)
for
position
in
snakeBody
:
pygame
.
draw
.
rect
(
playSurface
,
whiteColor
,
Rect
(
position
[
0
],
position
[
1
],
20
,
20
))
pygame
.
draw
.
rect
(
playSurface
,
redColor
,
Rect
(
targetPosition
[
0
],
targetPosition
[
1
],
20
,
20
))
pygame
.
display
.
flip
()
if
snakePosition
[
0
]
>
620
or
snakePosition
[
0
]
<
0
:
gameOver
()
elif
snakePosition
[
1
]
>
460
or
snakePosition
[
1
]
<
0
:
gameOver
()
fpsColck
.
tick
(
50
)
if
__name__
==
'__main__'
:
main
()
my_game.py
View file @
243c73cb
import
pygame
import
pygame
from
pygame
import
locals
from
pygame
import
locals
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
# 创建一个窗口
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
pygame
.
display
.
set_caption
(
"悟空酷跑"
)
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
...
@@ -17,21 +19,35 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -17,21 +19,35 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
index
=
0
index
=
0
wukong
=
hero
[
0
]
y
=
400
jumpState
=
"runing"
while
True
:
while
True
:
for
event
in
pygame
.
event
.
get
():
for
event
in
pygame
.
event
.
get
():
if
event
.
type
==
locals
.
QUIT
:
if
event
.
type
==
locals
.
QUIT
:
# 接收到退出事件后退出程序
# 接收到退出事件后退出程序
exit
()
exit
()
if
event
.
type
==
locals
.
KEYDOWN
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
if
jumpState
==
"up"
:
if
y
>
150
:
y
-=
5
else
:
jumpState
=
"down"
if
jumpState
==
"down"
:
if
y
<
400
:
y
+=
5
else
:
jumpState
=
"runing"
wukong
=
hero
[
index
]
wukong
=
hero
[
index
]
index
+=
1
index
+=
1
if
index
==
5
:
if
index
>=
5
:
index
=
0
index
=
0
# 将背景图画上去
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
screen
.
blit
(
background
,
(
0
,
0
))
#背景
screen
.
blit
(
road
,
(
0
,
500
))
screen
.
blit
(
road
,
(
0
,
500
))
#路
screen
.
blit
(
wukong
,
(
150
,
400
))
screen
.
blit
(
wukong
,
(
150
,
y
))
#悟空
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
\ 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