Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_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
9a557cef
authored
May 01, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
auto save
parent
04844f5a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
17 deletions
my_game.py
my_game.py
View file @
9a557cef
...
...
@@ -3,6 +3,29 @@ from pygame import locals
import
random
# 导入random模块
pygame
.
init
()
# 初始化
class
Block
(
pygame
.
sprite
.
Sprite
):
# 创建障碍物精灵类,类名为Block
# def定义 __init__()函数加上参数,形参123为障碍物123
def
__init__
(
self
,
image1
,
image2
,
image3
):
super
()
.
__init__
()
# 调用super()函数继承父类属性
# 加载障碍物图片
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
# 随机选择一种作为障碍物
# 根据障碍物位图的宽高设置矩形
self
.
rect
=
self
.
image
.
get_rect
()
# 障碍物绘制坐标
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
class
Player
(
pygame
.
sprite
.
Sprite
):
# 悟空
def
__init__
(
self
,
image
):
# def定义 __init__()加上参数,图片参数
super
()
.
__init__
()
# 用super()方法继承父类属性
# 加载悟空精灵图像
self
.
image
=
image
# 重写image属性
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)
self
.
rect
=
self
.
image
.
get_rect
()
# 重写rect属性
self
.
rect
.
x
=
150
# 设置初始x坐标
self
.
rect
.
y
=
400
# 设置初始y坐标
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
...
...
@@ -22,10 +45,11 @@ index = 0
y
=
400
jumpState
=
"runing"
t
=
30
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
road_x
=
0
# 设置道路x坐标为0并赋值给变量road_x
bg_x
=
0
# 设置远景x坐标为0并赋值给变量bg_x
time
=
0
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
while
True
:
for
event
in
pygame
.
event
.
get
():
...
...
@@ -36,7 +60,7 @@ while True:
if
jumpState
==
"runing"
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
if
gamestate
==
True
:
# if判断当gamestate的值为True时
if
jumpState
==
"up"
:
# 起跳状态
if
t
>
0
:
y
-=
t
...
...
@@ -52,24 +76,39 @@ while True:
t
=
30
# 悟空造型
wukong
=
hero
[
index
]
wukong
=
Player
(
hero
[
index
])
# 将设置悟空造型的代码改成用类来实现
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
if
index
>=
5
:
index
=
0
# 将背景图画上去
screen
.
blit
(
background
,
(
0
,
0
))
# 远处背景
screen
.
blit
(
road
,
(
0
,
500
))
# 路
screen
.
blit
(
wukong
,
(
150
,
y
))
# 悟空
if
rect
.
x
<=
0
-
rect
.
width
:
# 障碍物消失
bg_x
-=
1
# 将远景x坐标每次循环都-1
if
bg_x
<=
-
1000
:
# if判断当远景x坐标小于-1000
bg_x
=
0
# 远景x坐标回到0
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景代码中x坐标改为bg_x
road_x
-=
8
# 将道路x坐标每次循环都-8 使道路和障碍物以相同速度向左移动
if
road_x
<=
-
1000
:
# if判断当道路x坐标小于-1000
road_x
=
0
# 道路x坐标回到0
screen
.
blit
(
road
,
(
road_x
,
500
))
# 道路背景代码中x坐标改为变量road_x
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空代码里参数1改为.image
# 创建障碍物对象
obstacle
=
random
.
choice
([
bush
,
stone
,
cacti
])
rect
=
obstacle
.
get_rect
()
rect
.
x
=
1000
rect
.
y
=
500
-
rect
.
height
rect
.
x
-=
8
screen
.
blit
(
obstacle
,
(
rect
.
x
,
rect
.
y
))
time
+=
1
# 将time值+1
if
time
>=
60
:
# if判断time达到60
r
=
random
.
randint
(
0
,
100
)
# 随机数0,100并赋值给遍历r
if
r
>
40
:
# 判断r>40
obstacle
=
Block
(
bush
,
cacti
,
stone
)
# 创建障碍物精灵
block_list
.
add
(
obstacle
)
# 将精灵添加进精灵组
time
=
0
# 将变量time设为0
for
sprite
in
block_list
:
# for循环,遍历、展示障碍物
sprite
.
rect
.
x
-=
8
# 将精灵的x坐标-8
# 将程序渲染出来
screen
.
blit
(
sprite
.
image
,(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
# if判断精灵是否完全消失在窗口左边
sprite
.
kill
()
# 使用kill()方法将自己清除
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
gemeover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 导入图片素材,游戏结束
screen
.
blit
(
gameover
,
(
400
,
200
))
# 将游戏结束展示在窗口上
gamestate
=
False
# 将变量gamestate的值设为False
# 刷新画面
pygame
.
display
.
update
()
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