Commit 910e2c45 by BellCodeEditor

save project

parent a0351b05
Showing with 65 additions and 47 deletions
......@@ -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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment