harry -小鸟管道游戏终极版

时光总嘲笑我的痴心妄想 提交于 2019-12-28 02:52:32
import sys
import pygame

# 小鸟类
class Bird():
    def __init__(self):
        self.birdRect = pygame.Rect(65, 50, 50, 50)
        self.birdStatus = [pygame.image.load("assets/0.png"),
                           pygame.image.load("assets/1.png"),
                           pygame.image.load("assets/2.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  # 更改Y轴位置

# 管道类
class Pipeline():

    def __init__(self):
        self.wallx = 400  # 初始的x坐标
        self.pineUp = pygame.image.load("assets/top.png")
        self.pineDown = pygame.image.load("assets/bottom.png")

    def updatePipeline(self):
        self.wallx -= 5
        if self.wallx < -80:
            global score
            score = score + 1
            self.wallx = 400

def createMap():
    screen.fill((0,100 ,0)) # (1,2,3) 元组  []列表  {}字典
    screen.blit(background, (0,0))

    screen.blit(Pipeline.pineUp,(Pipeline.wallx,-300))
    screen.blit(Pipeline.pineDown,(Pipeline.wallx,500))
    Pipeline.updatePipeline()
    if Bird.dead:
        Bird.status = 2
    elif Bird.jump:
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status],(Bird.birdX,Bird.birdY))
    Bird.birdUpdate()
    screen.blit(font.render("Score:"+str(score),-1,(255,233,233)),(100,50))
    pygame.display.update()

def checkDead():
    # 上方管道的长方形坐标
    upRect = pygame.Rect(Pipeline.wallx, -300,
                         Pipeline.pineUp.get_width() -10,
                         Pipeline.pineUp.get_height())
    # 下方管道的长方形位置
    downRect = pygame.Rect(
        Pipeline.wallx,500,
        Pipeline.pineDown.get_width() - 10,
        Pipeline.pineUp.get_height()
    )

    # 检测小鸟与上下方管子是否碰撞:
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True

    # 检测小鸟是否飞出上边界和下边界
    if not 0 < Bird.birdRect[1] <height:
        Bird.dead = True
        return True
    else:
        return False


def getResult():

    final_text1 = "Game Over"
    final_text2 = "Your final score is:  " + str(score)
    ft1_font = pygame.font.SysFont("Arial", 70)  # 设置第一行文字字体
    ft1_surf = font.render(final_text1, 1, (242, 3, 36))  # 设置第一行文字颜色
    ft2_font = pygame.font.SysFont("Arial", 50)  # 设置第二行文字字体
    ft2_surf = font.render(final_text2, 1, (253, 177, 6))  # 设置第二行文字颜色
    screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])  # 设置第一行文字显示位置
    screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])  # 设置第二行文字显示位置
    pygame.display.flip()




# 主函数
if __name__ == '__main__':
    def main(Pipeline,Bird):
        pygame.init()  # 初始化
        pygame.font.init()  # 字体初始化
        font = pygame.font.SysFont('ziti.ttf', 50)  # 设置字体和大小哦
        size = width, height = 400, 650  # 给宽和高赋值
        screen = pygame.display.set_mode(size)  # 设置屏幕大小
        clock = pygame.time.Clock()  # 设置时钟
        Pipeline = Pipeline()  # 实例化一个管道的对象
        Bird = Bird()  # 实例化一个小鸟的对象
        score = 0  # 分数
        while True:
            clock.tick(60)
            #判断按钮
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    sys.exit()
                if (event.type == pygame.KEYDOWN) and not Bird.dead:
                    Bird.jump = True
                    Bird.gravity = 5
                    Bird.jumpSpeed = 10

            background = pygame.image.load("assets/background.png")
            # 1.检查生命状态
            if checkDead():
                getResult()
            else:
                createMap()
            # 2.小鸟是否死亡
            # 3.创建地图


        pygame.quit()

    main(Pipeline,Bird)






标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!