def addObject(): global surObject,object,objectList,object rate=4 #是否生成障碍物 if not random.randint(0,300)return y=random.choice([height-100,height-200,height-300,height-400]) object=Object(surObject,width+40,y) objectList.append(object)
def updateLogic(): global gameState,score #键盘事件处理 for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type==pygame.KEYDOWN: #空格键跳跃 if gameState==0: if event.key==pygame.K_SPACE: if role.state==0: role.state=1 role.vy=role.vy_start elif role.state==1: role.state=2 role.vy=role.vy_start elif gameState==1: if event.key==pygame.K_SPACE: #重新开始游戏 initGame()
if gameState==0: #背景的移动 bg.dx+=10 if bg.dx==1200: bg.dx=0
#人物的移动 if role.state==0: role.currentFrame+=1 if role.currentFrame==12: role.currentFrame=0 else: role.y+=role.vy role.vy+=role.g if role.y>=508-85: role.y=508-85 role.state=0 #障碍物的移动 addObject()
for object in objectList: object.x-=10 #障碍物移动 # 障碍物超出屏幕,移除障碍物 if object.x+object.w<=0: objectList.remove(object) score+=10 #避开障碍物,加10分 print("移除了一个目标") #碰撞检测 if object.collision(role.getRect(),object.getRect()): if(object.currentFrame==6): objectList.remove(object) score+=100 #吃金币加100分 print(score) print("吃了一个金币") else: gameState=1 #游戏失败 print("发生了碰撞!")