水管道小鸟的游戏学习

学习兴趣:

之前在CSDN上看到一篇关于水管道小鸟的游戏,自己是初学python,想做个有趣的东西练练手,然后就参考他的代码,做了一点点修改。


学习内容:

利用pygame包做的一个简单小游戏,自己添加了一个开始游戏的按钮,在游戏结束后可以继续开始游戏,改进了一点小bug,是个挺好的新手练习项目。


学习产出:

下面我把代码贴上来,挺简单得到,就没写注释了

import sys
import pygame


class Bird:
    def __init__(self):
        self.birdRect = pygame.Rect(50, 60, 50, 50)
        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.jumpSpeed
        self.birdRect[1] = self.birdY


class PipeLine:
    def __init__(self):
        self.wallX = 400
        self.pipeUp = pygame.image.load('assets/top.png')
        self.pipeDown = pygame.image.load('assets/bottom.png')

    def updatePipeLine(self):
        self.wallX -= 5
        if self.wallX < -40:
            global score
            score += 1
            self.wallX = 400

    def stop(self):
        screen.blit(self.pipeUp, (PipeLine.wallX, -300))
        screen.blit(self.pipeDown, (PipeLine.wallX, 500))


def checkDead():
    # 上方管道位置矩形
    upRect = pygame.Rect(PipeLine.wallX, -300, PipeLine.pipeUp.get_width() - 10,
                         PipeLine.pipeUp.get_height())
    # 下方管道位置矩形
    downRect = pygame.Rect(PipeLine.wallX, 500, PipeLine.pipeDown.get_width() - 10,
                           PipeLine.pipeDown.get_height())
    # 小鸟是否与管道碰撞
    if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
        Bird.dead = True
        return True
    # 小鸟是否飞出边界
    if not 0 < Bird.birdRect[1] < height:
        Bird.dead = True
        return True
    else:
        return False


def createMap():
    screen.fill((255, 255, 255))
    screen.blit(background, (0, 0))

    # 显示管道
    screen.blit(PipeLine.pipeUp, (PipeLine.wallX, -300))
    screen.blit(PipeLine.pipeDown, (PipeLine.wallX, 500))
    # 显示小鸟
    if Bird.dead:
        Bird.status = 2
    elif Bird.jump:
        Bird.status = 1
    screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))

    # 显示分数
    font = pygame.font.SysFont("Arial", 70)
    screen.blit(font.render('Score:' + str(score), -1, (255, 255, 255)), (100, 50))  # 设置颜色及坐标位置
    pygame.display.update()  # 更新显示


def run():
    PipeLine.updatePipeLine()
    Bird.birdUpdate()


def getResult():
    final_text1 = "Game Over"
    final_text2 = "Your final score is:  " + str(score)
    ft1_font = pygame.font.SysFont("Arial", 50)  # 设置第一行文字字体
    ft1_surf = ft1_font.render(final_text1, 1, (242, 3, 36))  # 设置第一行文字颜色
    ft2_font = pygame.font.SysFont("Arial", 30)  # 设置第二行文字字体
    ft2_surf = ft2_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.update()


class startGame:
    def __init__(self):
        self.flag = False

    def start(self):
        x1, y1 = pygame.mouse.get_pos()
        if 150 < x1 < 250 and 400 < y1 < 550 and pygame.mouse.get_pressed()[0]:
            self.flag = True
            return True
        else:
            return False

    def end(self):
        self.flag = False

    def create(self):
        startXY = pygame.Rect(150, 400, 100, 150)
        startText = 'start'
        startFont = pygame.font.SysFont('Arial', 70)
        start_surf = startFont.render(startText, 1, (134, 164, 255))
        screen.blit(start_surf, startXY)
        pygame.display.update()


if __name__ == '__main__':
    pygame.init()
    size = width, height = 400, 650
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    PipeLine = PipeLine()
    Bird = Bird()
    startGame = startGame()
    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 or event.type == pygame.MOUSEBUTTONDOWN:
                Bird.jump = True
                Bird.gravity = 5  # 重力
                Bird.jumpSpeed = 10

        background = pygame.image.load('assets/background.png')
        if not startGame.flag:
            createMap()
            startGame.create()
            startGame.start()
        else:
            createMap()
            run()
        if checkDead():
            PipeLine.stop()
            getResult()
            startGame.create()
            startGame.end()
            if startGame.start():
                Bird.__init__()
                PipeLine.__init__()
                Bird.dead = False
                createMap()
                score = 0
                pygame.time.wait(500)
                run()

原文章衔接地址:

https://blog.csdn.net/zha6476003/article/details/82940350

我的博客里也有代码和资源文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值