Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson8_diy2
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
910e2c45
authored
Mar 12, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
a0351b05
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
65 additions
and
47 deletions
my_game.py
my_game.py
View file @
910e2c45
...
...
@@ -12,6 +12,13 @@ class Zhangai(pygame.sprite.Sprite):#子类Zhangai继承父类精灵
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
class
Player
(
pygame
.
sprite
.
Sprite
):
#子类Player继承父类精灵
def
__init__
(
self
,
image
):
#形参,后面再用实参
super
()
.
__init__
()
#继承
self
.
image
=
image
#后面用实参
self
.
rect
=
self
.
image
.
get_rect
()
#前面要加self
self
.
rect
.
x
=
150
#初始x坐标
self
.
rect
.
y
=
400
#初始y坐标
# 创建一个窗口
screen
=
pygame
.
display
.
set_mode
((
1000
,
600
))
FPS
=
pygame
.
time
.
Clock
()
# pygame时钟,控制游戏速度(帧数)
...
...
@@ -37,6 +44,7 @@ road_x = 0
bg_x
=
0
time
=
0
gamestate
=
True
#游戏状态,后面会变成False,只有True才会运行
#zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list
=
pygame
.
sprite
.
Group
()
#创建精灵组
...
...
@@ -50,54 +58,64 @@ while True:
if
jumpstate
==
'runing'
:
#只有在跑步状态才能跳
if
event
.
key
==
locals
.
K_SPACE
:
jumpstate
=
'up'
#按下空格,模式为跳跃
if
jumpstate
==
'up'
:
#起跳
if
t
>
0
:
#小于起跳最大值
y
-=
t
#起跳
t
-=
1
else
:
jumpstate
=
'down'
#落下
if
jumpstate
==
'down'
:
#降落
if
t
<=
30
:
#大于落地最大值
y
+=
t
#落下
t
+=
1
else
:
jumpstate
=
'runing'
#跑步
t
=
30
wukong
=
hero
[
index
]
#变向遍历列表
wukong
=
Player
(
hero
[
index
])
#变向遍历列表
if
jumpstate
==
'runing'
:
#只有在跑步才能切换造型
index
+=
1
#索引增加
if
index
>=
5
:
#如果大于索引
index
=
0
#改为零
index
=
0
#改为零
if
gamestate
==
True
:
#只有在True状态下才能渲染
if
jumpstate
==
'up'
:
#起跳
if
t
>
0
:
#小于起跳最大值
y
-=
t
#起跳
wukong
.
rect
.
y
=
y
#避免出现
t
-=
2
else
:
jumpstate
=
'down'
#落下
if
jumpstate
==
'down'
:
#降落
if
t
<=
30
:
#大于落地最大值
y
+=
t
#落下
wukong
.
rect
.
y
=
y
t
+=
2
else
:
jumpstate
=
'runing'
#跑步
t
=
30
# 将背景图画上去
bg_x
-=
1
if
bg_x
<=
-
1000
:
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
8
if
road_x
<=
-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
time
+=
1
#每秒加一
if
time
>=
60
:
#如果渲染了一秒,因为帧数设置为60
r
=
random
.
randint
(
0
,
100
)
#随机选
if
r
>
40
:
#这样只有随机选时达与40才能渲染,相当于只有60%的概率渲染
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
#渲染
zhangai_list
.
add
(
zhangai
)
#添加进列表
time
=
0
#重新设为零
for
prop
in
zhangai_list
:
#遍历
prop
.
rect
.
x
-=
8
#移动
screen
.
blit
(
prop
.
image
,
(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
#渲染
if
prop
.
rect
.
x
<=
0
-
prop
.
rect
.
width
:
#超出界限时
prop
.
kill
()
#杀掉精灵
if
pygame
.
sprite
.
collide_rect
(
wukong
,
prop
):
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
screen
.
blit
(
gameover
,(
400
,
200
))
gamestate
=
False
# 将背景图画上去
bg_x
-=
1
if
bg_x
<=
-
1000
:
bg_x
=
0
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
8
if
road_x
<=
-
1000
:
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
,
(
150
,
y
))
time
+=
1
if
time
>=
60
:
r
=
random
.
randint
(
0
,
100
)
if
r
>
40
:
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
zhangai_list
.
add
(
zhangai
)
time
=
0
for
prop
in
zhangai_list
:
prop
.
rect
.
x
-=
8
screen
.
blit
(
prop
.
image
,
(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
#渲染
if
prop
.
rect
.
x
<=
0
-
prop
.
rect
.
width
:
prop
.
kill
()
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移
#screen.blit(zhangai.image, (zhangai.rect.x, zhangai.rect.y))#渲染
# 刷新画面
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
\ No newline at end of file
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移
#screen.blit(zhangai.image, (zhangai.rect.x, zhangai.rect.y))#渲染
# 刷新画面
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