Commit 910e2c45 by BellCodeEditor

save project

parent a0351b05
Showing with 38 additions and 20 deletions
...@@ -12,6 +12,13 @@ class Zhangai(pygame.sprite.Sprite):#子类Zhangai继承父类精灵 ...@@ -12,6 +12,13 @@ class Zhangai(pygame.sprite.Sprite):#子类Zhangai继承父类精灵
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height 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)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
...@@ -37,6 +44,7 @@ road_x = 0 ...@@ -37,6 +44,7 @@ road_x = 0
bg_x = 0 bg_x = 0
time = 0 time = 0
gamestate = True#游戏状态,后面会变成False,只有True才会运行
#zhangai = Zhangai(stone,cacti,bush)#设置变量 #zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list = pygame.sprite.Group()#创建精灵组 zhangai_list = pygame.sprite.Group()#创建精灵组
...@@ -50,26 +58,30 @@ while True: ...@@ -50,26 +58,30 @@ while True:
if jumpstate == 'runing':#只有在跑步状态才能跳 if jumpstate == 'runing':#只有在跑步状态才能跳
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jumpstate = 'up'#按下空格,模式为跳跃 jumpstate = 'up'#按下空格,模式为跳跃
wukong = Player(hero[index])#变向遍历列表
if jumpstate == 'runing':#只有在跑步才能切换造型
index += 1#索引增加
if index >= 5:#如果大于索引
index = 0#改为零
if gamestate == True:#只有在True状态下才能渲染
if jumpstate =='up':#起跳 if jumpstate =='up':#起跳
if t >0:#小于起跳最大值 if t >0:#小于起跳最大值
y -= t#起跳 y -= t#起跳
t -= 1 wukong.rect.y = y#避免出现
t -= 2
else: else:
jumpstate = 'down'#落下 jumpstate = 'down'#落下
if jumpstate == 'down':#降落 if jumpstate == 'down':#降落
if t <= 30:#大于落地最大值 if t <= 30:#大于落地最大值
y += t#落下 y += t#落下
t += 1 wukong.rect.y = y
t += 2
else: else:
jumpstate = 'runing'#跑步 jumpstate = 'runing'#跑步
t = 30 t = 30
wukong = hero[index]#变向遍历列表
if jumpstate == 'runing':#只有在跑步才能切换造型
index += 1#索引增加
if index >= 5:#如果大于索引
index = 0#改为零
# 将背景图画上去 # 将背景图画上去
bg_x -= 1 bg_x -= 1
...@@ -80,20 +92,26 @@ while True: ...@@ -80,20 +92,26 @@ while True:
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, (150, y)) screen.blit(wukong.image, (150,y))
time+=1 time+=1#每秒加一
if time >=60: if time >=60:#如果渲染了一秒,因为帧数设置为60
r = random.randint(0,100) r = random.randint(0,100)#随机选
if r >40: if r >40:#这样只有随机选时达与40才能渲染,相当于只有60%的概率渲染
zhangai = Zhangai(stone,cacti,bush) zhangai = Zhangai(stone,cacti,bush)#渲染
zhangai_list.add(zhangai) zhangai_list.add(zhangai)#添加进列表
time = 0 time = 0#重新设为零
for prop in zhangai_list: for prop in zhangai_list:#遍历
prop.rect.x -=8 prop.rect.x -=8#移动
screen.blit(prop.image, (prop.rect.x, prop.rect.y))#渲染 screen.blit(prop.image, (prop.rect.x, prop.rect.y))#渲染
if prop.rect.x <= 0-prop.rect.width: if prop.rect.x <= 0-prop.rect.width:#超出界限时
prop.kill() prop.kill()#杀掉精灵
if pygame.sprite.collide_rect(wukong,prop):
gameover = pygame.image.load('gameover.png')
screen.blit(gameover,(400,200))
gamestate = False
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口 #if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新 # zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移 #zhangai.rect.x -=8 #左移
......
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