代码如下:
# -*- coding:utf-8 -*- import pygame import sys import random class Bird(object): def __init__(self): self.birdRect=pygame.Rect(65,50,50,50) self.birdStatus =[pygame.image.load("assets/1.png"), pygame.image.load("assets/2.png"), pygame.image.load("assets/dead.png")] # self.birdStatus = [pygame.image.load("assets/1.png"), # pygame.image.load("assets/2.png"), # pygame.image.load("assets/dead.png")] self.status=0 self.birdX=120 self.birdY=350 self.jump=False self.jumpSpeed=10 self.gravity=5 self.dead=False def birdUpdate(self): if self.jump: self.jumpSpeed-=1 self.birdY-=self.jumpSpeed else: self.gravity+=0.2 self.birdY+=self.gravity self.birdRect[1]=self.birdY class Pipeline(object): def __init__(self): pass def updatePipeline(self): pass def createMap(): """定义创建地图的方法""" screen.fill((255, 255, 255)) # 填充颜色(screen还没定义不要着急) screen.blit(background, (0, 0)) # 填入到背景 if Bird.dead: Bird.status=2 elif Bird.jump: Bird.status=1 screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY)) Bird.birdUpdate() pygame.display.update() pygame.display.update() # 更新显示 # if __name__=='__main__': pygame.init() size =width,height=400,620 screen=pygame.display.set_mode(size) clock = pygame.time.Clock() Pipeline=Pipeline() Bird=Bird() while True: clock.tick(60) for event in pygame.event.get(): if event.type==pygame.QUIT: sys.exit() if(event.type==pygame.KEYDOWN or event.type==pygame.MOUSEBUTTONDOWN)and not Bird.dead: Bird.jump=True Bird.gravity=5 Bird.jumpSpeed=10 background = pygame.image.load("assets/background.png") createMap() pygame.quit()
运行结果如图:
用鼠标或者键盘任意键连续不停的按点击或者按下,鸟才能飞起来