Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
Administrator
/
pygame_lesson9_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
528e4b4a
authored
Mar 12, 2022
by
BellCodeEditor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
save project
parent
686c5f10
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
77 deletions
STKAITI.TTF
my_game.py
STKAITI.TTF
0 → 100644
View file @
528e4b4a
File added
my_game.py
View file @
528e4b4a
...
@@ -4,31 +4,27 @@ import random
...
@@ -4,31 +4,27 @@ import random
pygame
.
init
()
# 初始化
pygame
.
init
()
# 初始化
class
Block
(
pygame
.
sprite
.
Sprite
):
# 障碍物精灵类
class
Zhangai
(
pygame
.
sprite
.
Sprite
):
#子类Zhangai继承父类精灵
def
__init__
(
self
,
image1
,
image2
,
image3
):
def
__init__
(
self
,
image1
,
image2
,
image3
):
#形参,后面再用实参
super
()
.
__init__
()
super
()
.
__init__
()
#继承
# 加载障碍物的图片
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
#随机选,后面用实参
self
.
image
=
random
.
choice
([
image1
,
image2
,
image3
])
self
.
rect
=
self
.
image
.
get_rect
()
#前面要加self
# 根据障碍物位图的宽高设置矩形
self
.
rect
=
self
.
image
.
get_rect
()
# 障碍物绘制坐标
self
.
rect
.
x
=
1000
self
.
rect
.
x
=
1000
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
rect
.
y
=
500
-
self
.
rect
.
height
self
.
score
=
1
class
Player
(
pygame
.
sprite
.
Sprite
):
# 悟空
class
Player
(
pygame
.
sprite
.
Sprite
):
#子类Player继承父类精灵
def
__init__
(
self
,
image
):
def
__init__
(
self
,
image
):
#形参,后面再用实参
super
()
.
__init__
()
super
()
.
__init__
()
#继承
# 加载悟空精灵图像
self
.
image
=
image
#后面用实参
self
.
image
=
image
self
.
rect
=
self
.
image
.
get_rect
()
#前面要加self
# image的get_rect()方法,可以返回pygame.Rect(0,0,图像宽,图像高)的对象
self
.
rect
.
x
=
150
#初始x坐标
self
.
rect
=
self
.
image
.
get_rect
()
self
.
rect
.
y
=
400
#初始y坐标
self
.
rect
.
x
=
150
self
.
rect
.
y
=
400
# 创建一个窗口
# 创建一个窗口
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
(
"悟空酷跑"
)
# 载入图片
# 载入图片
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
background
=
pygame
.
image
.
load
(
'bg.png'
)
# 背景
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
road
=
pygame
.
image
.
load
(
'road.png'
)
# 路
...
@@ -40,84 +36,97 @@ hero = [pygame.image.load('hero1.png'),
...
@@ -40,84 +36,97 @@ hero = [pygame.image.load('hero1.png'),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero3.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero4.png'
),
pygame
.
image
.
load
(
'hero5.png'
)]
pygame
.
image
.
load
(
'hero5.png'
)]
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
32
)
basic_font
=
pygame
.
font
.
Font
(
'STKAITI.TTF'
,
38
)
index
=
0
index
=
0
jumpstate
=
'runing'
#初始状态
y
=
400
y
=
400
jumpState
=
"runing"
t
=
30
t
=
30
road_x
=
0
road_x
=
0
bg_x
=
0
bg_x
=
0
time
=
0
gamestate
=
True
block_list
=
pygame
.
sprite
.
Group
()
# 创建精灵组
time
=
0
gamestate
=
True
#游戏状态,后面会变成False,只有True才会运行
score
=
0
#zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list
=
pygame
.
sprite
.
Group
()
#创建精灵组
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
.
type
==
locals
.
KEYDOWN
:
if
jump
State
==
"runing"
:
if
jump
state
==
'runing'
:
#只有在跑步状态才能跳
if
event
.
key
==
locals
.
K_SPACE
:
if
event
.
key
==
locals
.
K_SPACE
:
jumpState
=
"up"
jumpstate
=
'up'
#按下空格,模式为跳跃
# 悟空造型
wukong
=
Player
(
hero
[
index
])
#变向遍历列表
wukong
=
Player
(
hero
[
index
])
if
jumpstate
==
'runing'
:
#只有在跑步才能切换造型
if
jumpState
==
"runing"
:
# 跑步状态下
index
+=
1
#索引增加
index
+=
1
if
index
>=
5
:
#如果大于索引
if
index
>=
5
:
index
=
0
#改为零
index
=
0
if
gamestate
==
True
:
#只有在True状态下才能渲染
if
jumpstate
==
'up'
:
#起跳
if
gamestate
==
True
:
if
t
>
0
:
#小于起跳最大值
if
jumpState
==
"up"
:
# 起跳状态
y
-=
t
#起跳
if
t
>
0
:
wukong
.
rect
.
y
=
y
#避免出现人在天上飞却死了的bug
y
-=
t
wukong
.
rect
.
y
=
y
t
-=
2
t
-=
2
else
:
else
:
jump
State
=
"down"
jump
state
=
'down'
#落下
if
jump
State
==
"down"
:
# 降落状态
if
jump
state
==
'down'
:
#降落
if
t
<=
30
:
if
t
<=
30
:
#大于落地最大值
y
+=
t
y
+=
t
#落下
wukong
.
rect
.
y
=
y
wukong
.
rect
.
y
=
y
#同理
t
+=
2
t
+=
2
else
:
else
:
jumpState
=
"runing"
jumpstate
=
'runing'
#跑步
t
=
30
t
=
30
#重定义t
# 将背景图画上去
# 将背景图画上去
bg_x
-=
1
bg_x
-=
1
#移动
if
bg_x
<=-
1000
:
if
bg_x
<=
-
1000
:
#因为背景长2000,且为俩段长度为1000的相同图片合成
bg_x
=
0
bg_x
=
0
#重滚
screen
.
blit
(
background
,
(
bg_x
,
0
))
# 远景
screen
.
blit
(
background
,
(
bg_x
,
0
))
road_x
-=
8
road_x
-=
8
if
road_x
<=-
1000
:
if
road_x
<=
-
1000
:
#与背景相同
road_x
=
0
road_x
=
0
screen
.
blit
(
road
,
(
road_x
,
500
))
# 道路
screen
.
blit
(
road
,
(
road_x
,
500
))
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
#渲染名要换一个
screen
.
blit
(
wukong
.
image
,
(
150
,
y
))
# 悟空
time
+=
1
#每渲染一次加一
time
+=
1
if
time
>=
60
:
#如果渲染了一秒,因为帧数设置为60
if
time
>=
60
:
# 创建障碍物精灵
r
=
random
.
randint
(
0
,
100
)
#随机选
time
=
0
if
r
>
40
:
#这样只有随机选时达与40才能渲染,相当于只有60%的概率渲染
num
=
random
.
randint
(
0
,
50
)
zhangai
=
Zhangai
(
stone
,
cacti
,
bush
)
#渲染
if
num
>
20
:
zhangai_list
.
add
(
zhangai
)
#添加进列表
obstacle
=
Block
(
bush
,
cacti
,
stone
)
time
=
0
#重新设为零
block_list
.
add
(
obstacle
)
for
prop
in
zhangai_list
:
#遍历
for
sprite
in
block_list
:
# 遍历、展示障碍物精灵
prop
.
rect
.
x
-=
8
#移动
sprite
.
rect
.
x
-=
8
screen
.
blit
(
prop
.
image
,
(
prop
.
rect
.
x
,
prop
.
rect
.
y
))
#渲染
screen
.
blit
(
sprite
.
image
,
(
sprite
.
rect
.
x
,
sprite
.
rect
.
y
))
if
prop
.
rect
.
x
<=
0
-
prop
.
rect
.
width
:
#超出界限时
if
sprite
.
rect
.
x
<=
0
-
sprite
.
rect
.
width
:
prop
.
kill
()
#杀掉精灵
sprite
.
kill
()
if
pygame
.
sprite
.
collide_rect
(
wukong
,
prop
):
#检测悟空是否与障碍碰撞
if
pygame
.
sprite
.
collide_rect
(
wukong
,
sprite
):
# 精灵碰撞检测
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
#展示失败
gameover
=
pygame
.
image
.
load
(
'gameover.png'
)
# 游戏结束
screen
.
blit
(
gameover
,(
400
,
200
))
#失败位置
screen
.
blit
(
gameover
,
(
400
,
200
))
gamestate
=
False
#变为False
gamestate
=
False
else
:
scoreSurf
=
basic_font
.
render
(
"分数:"
+
str
(
score
),
True
,(
255
,
0
,
0
))
if
prop
.
rect
.
x
+
prop
.
rect
.
width
<
wukong
.
rect
.
x
:
screen
.
blit
(
scoreSurf
,(
850
,
20
))
score
+=
prop
.
score
prop
.
score
=
0
#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))#渲染
scoreSurf
=
basic_font
.
render
(
'分数:'
+
str
(
score
),
True
,(
200
,
200
,
200
))
screen
.
blit
(
scoreSurf
,(
880
,
20
))
# 刷新画面
# 刷新画面
pygame
.
display
.
update
()
pygame
.
display
.
update
()
FPS
.
tick
(
60
)
FPS
.
tick
(
60
)
\ No newline at end of file
\ 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