使用Python开发热门手机游戏飞翔的小鸟,我连第十下都过不去

哈喽大家好

今天给大家分享一个用Python开发一款飞翔的小鸟游戏。

飞翔的小鸟(游戏英文名:Flappy Bird)

一款由越南独立开发者开发的手机游戏,是之前非常流行的一款手机游戏

小游戏目标:让小鸟穿过管子,不要碰到任何物体,挑战更远距离

今天,就让我们一起用python来复刻一下这款游戏吧!!!

环境使用

  • Python 3.8

–> 解释器 <执行python代码>

  • Pycharm

–> 编辑器 <写python代码的>

所需素材

音效素材

图片素材

效果展示

背景啊其他素材啊也是可以修改的

给你们看看博主魔改的背景

代码展示

使用的模块

import cfg  
import sys  
import random  
import pygame  

游戏初始化

def initGame():  
    pygame.init()  
    pygame.mixer.init()  
    screen = pygame.display.set_mode((cfg.SCREENWIDTH, cfg.SCREENHEIGHT))  
    pygame.display.set_caption('Flappy Bird ')  
    return screen  

显示当前分数

def showScore(screen, score, number_images):  
    digits = list(str(int(score)))  
    width = 0  
    for d in digits:  
        width += number_images.get(d).get_width()  
    offset = (cfg.SCREENWIDTH - width) / 2  
    for d in digits:  
        screen.blit(number_images.get(d), (offset, cfg.SCREENHEIGHT*0.1))  
        offset += number_images.get(d).get_width()  

主函数

def main():  
    screen = initGame()  
    # 加载必要的游戏资源  
    # Python学习交流群 872937351  
    sounds = dict()  
    for key, value in cfg.AUDIO_PATHS.items():  
        sounds[key] = pygame.mixer.Sound(value)  
    # --导入数字图片  
    number_images = dict()  
    for key, value in cfg.NUMBER_IMAGE_PATHS.items():  
        number_images[key] = pygame.image.load(value).convert_alpha()  
    # --管道  
    pipe_images = dict()  
    pipe_images['bottom'] = pygame.image.load(random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()  
    pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)  
    # --小鸟图片  
    bird_images = dict()  
    for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(list(cfg.BIRD_IMAGE_PATHS.keys()))].items():  
        bird_images[key] = pygame.image.load(value).convert_alpha()  
    # --背景图片  
    backgroud_image = pygame.image.load(random.choice(list(cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()  
    # --其他图片  
    other_images = dict()  
    for key, value in cfg.OTHER_IMAGE_PATHS.items():  
        other_images[key] = pygame.image.load(value).convert_alpha()  
    # 游戏开始界面  
    game_start_info = startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg)  
    # 进入主游戏  
    score = 0  
    bird_pos, base_pos, bird_idx = list(game_start_info.values())  
    base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()  
    clock = pygame.time.Clock()  
    # --管道类  
    pipe_sprites = pygame.sprite.Group()  
    for i in range(2):  
        pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))  
        pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('top')[-1])))  
        pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('bottom')[-1])))  
    # --bird类  
    bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)  
    # --是否增加pipe  
    is_add_pipe = True  
    # --游戏是否进行中  
    is_game_running = True  
    while is_game_running:  
        for event in pygame.event.get():  
            if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):  
                pygame.quit()  
                sys.exit()  
            elif event.type == pygame.KEYDOWN:  
                if event.key == pygame.K_SPACE or event.key == pygame.K_UP:  
                    bird.setFlapped()  
                    sounds['wing'].play()  
        # --碰撞检测  
        for pipe in pipe_sprites:  
            if pygame.sprite.collide_mask(bird, pipe):  
                sounds['hit'].play()  
                is_game_running = False  
        # --更新小鸟  
        boundary_values = [0, base_pos[-1]]  
        is_dead = bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)  
        if is_dead:  
            sounds['hit'].play()  
            is_game_running = False  
        # --移动base实现小鸟往前飞的效果  
        base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)  
        # --移动pipe实现小鸟往前飞的效果  
        flag = False  
        for pipe in pipe_sprites:  
            pipe.rect.left -= 4  
            if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:  
                pipe.used_for_score = True  
                score += 0.5  
                if '.5' in str(score):  
                    sounds['point'].play()  
            if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:  
                pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))  
                pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=pipe_pos.get('top')))  
                pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=pipe_pos.get('bottom')))  
                is_add_pipe = False  
            elif pipe.rect.right < 0:  
                pipe_sprites.remove(pipe)  
                flag = True  
        if flag: is_add_pipe = True  
        # --绑定必要的元素在屏幕上  
        screen.blit(backgroud_image, (0, 0))  
        pipe_sprites.draw(screen)  
        screen.blit(other_images['base'], base_pos)  
        showScore(screen, score, number_images)  
        bird.draw(screen)  
        pygame.display.update()  
        clock.tick(cfg.FPS)  
    endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg)  

另外怕大家不会使用,直接给大家准备了写好的,直接下载打开即可使用!
源码放在百度云盘上了需要可以微信扫描下方CSDN官方认证二维码免费领取

读者福利:如果你也喜欢编程,想通过学习Python转行获取更高薪资,那这套Python学习资料一定对你有用!

对于0基础小白入门:

如果你是零基础小白,想快速入门Python是可以考虑的
一方面是学习时间相对较短,学习内容更全面更集中
二方面是可以找到适合自己的学习方案

包括:Python安装包+激活码、Python web开发,Python爬虫,Python数据分析,人工智能、机器学习等教程。带你从零基础系统性的学好Python!

读者福利:2023年零基础学Python必备资料(视频+源码+工具+软件) 安全链接免费领取

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

在这里插入图片描述

四、实战案例

光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。

在这里插入图片描述

五、100道Python练习题

检查学习结果。

在这里插入图片描述

六、面试资料

我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。


​​

👉 这份完整版的Python全套学习资料已经上传,朋友们如果需要可以扫描下方CSDN官方认证二维码或者点击链接免费领取保证100%免费

读者福利:2023年零基础学Python必备资料(视频+源码+工具+软件) 安全链接免费领取

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值