python带你成功复刻热门手机游戏——飞翔的小鸟
创始人
2024-05-30 22:11:01
0

前言

大家早好、午好、晚好吖 ❤ ~欢迎光临本文章

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

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

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

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


完整源码、素材皆可点击文章下方名片获取此处跳转


环境使用:

  • Python 3.8

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

  • Pycharm

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


所需素材

音效素材

图片素材

效果展示

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

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

代码展示


(完整源码、素材皆可点击此处+获取)


‘’‘配置文件’‘’

导入模块

import os
# FPS
FPS = 60

屏幕

SCREENWIDTH = 288
SCREENHEIGHT = 512

管道之间的空隙

PIPE_GAP_SIZE = 100

图片

NUMBER_IMAGE_PATHS = {'0': os.path.join(os.getcwd(), 'resources/images/0.png'),'1': os.path.join(os.getcwd(), 'resources/images/1.png'),'2': os.path.join(os.getcwd(), 'resources/images/2.png'),'3': os.path.join(os.getcwd(), 'resources/images/3.png'),'4': os.path.join(os.getcwd(), 'resources/images/4.png'),'5': os.path.join(os.getcwd(), 'resources/images/5.png'),'6': os.path.join(os.getcwd(), 'resources/images/6.png'),'7': os.path.join(os.getcwd(), 'resources/images/7.png'),'8': os.path.join(os.getcwd(), 'resources/images/8.png'),'9': os.path.join(os.getcwd(), 'resources/images/9.png')
}
BIRD_IMAGE_PATHS = {'red': {'up': os.path.join(os.getcwd(), 'resources/images/redbird-upflap.png'),'mid': os.path.join(os.getcwd(), 'resources/images/redbird-midflap.png'),'down': os.path.join(os.getcwd(), 'resources/images/redbird-downflap.png')},'blue': {'up': os.path.join(os.getcwd(), 'resources/images/bluebird-upflap.png'),'mid': os.path.join(os.getcwd(), 'resources/images/bluebird-midflap.png'),'down': os.path.join(os.getcwd(), 'resources/images/bluebird-downflap.png')},'yellow': {'up': os.path.join(os.getcwd(), 'resources/images/yellowbird-upflap.png'),'mid': os.path.join(os.getcwd(), 'resources/images/yellowbird-midflap.png'),'down': os.path.join(os.getcwd(), 'resources/images/yellowbird-downflap.png')}
}
BACKGROUND_IMAGE_PATHS = {'day': os.path.join(os.getcwd(), 'resources/images/background-day.png'),'night': os.path.join(os.getcwd(), 'resources/images/background-night.png')
}
PIPE_IMAGE_PATHS = {'green': os.path.join(os.getcwd(), 'resources/images/pipe-green.png'),'red': os.path.join(os.getcwd(), 'resources/images/pipe-red.png')
}
OTHER_IMAGE_PATHS = {'gameover': os.path.join(os.getcwd(), 'resources/images/gameover.png'),'message': os.path.join(os.getcwd(), 'resources/images/message.png'),'base': os.path.join(os.getcwd(), 'resources/images/base.png')
}

音频路径

AUDIO_PATHS = {'die': os.path.join(os.getcwd(), 'resources/audios/die.wav'),'hit': os.path.join(os.getcwd(), 'resources/audios/hit.wav'),'point': os.path.join(os.getcwd(), 'resources/audios/point.wav'),'swoosh': os.path.join(os.getcwd(), 'resources/audios/swoosh.wav'),'wing': os.path.join(os.getcwd(), 'resources/audios/wing.wav')
}

主运行文件

导入模块

import cfg
import sys
import random
import pygame
from modules import *

‘’‘游戏初始化’‘’

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

‘’‘显示当前分数’‘’

def showScore(screen, score, number_images):digits = list(str(int(score)))width = 0for d in digits:width += number_images.get(d).get_width()offset = (cfg.SCREENWIDTH - width) / 2for 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()# 加载必要的游戏资源# --导入音频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 = 0bird_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)# --是否增加pipeis_add_pipe = True# --游戏是否进行中is_game_running = Truewhile 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 = Falsefor pipe in pipe_sprites:pipe.rect.left -= 4if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:pipe.used_for_score = Truescore += 0.5if '.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 = Falseelif pipe.rect.right < 0:pipe_sprites.remove(pipe)flag = Trueif 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)

‘’‘run’‘’

if __name__ == '__main__':while True:main()

代码太多,我就没放完啦,完整源码、素材皆可点击文章下方名片获取此处跳转


尾语 💝

好了,今天的分享就差不多到这里了!

完整代码、更多资源、疑惑解答直接点击下方名片自取即可。

对下一篇大家想看什么,可在评论区留言哦!看到我会更新哒(ง •_•)ง

喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

最后,宣传一下呀~👇👇👇更多源码、资料、素材、解答、交流皆点击下方名片获取呀👇👇👇

相关内容

热门资讯

122.(leaflet篇)l... 听老人家说:多看美女会长寿 地图之家总目录(订阅之前建议先查看该博客) 文章末尾处提供保证可运行...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
育碧GDC2018程序化大世界... 1.传统手动绘制森林的问题 采用手动绘制的方法的话,每次迭代地形都要手动再绘制森林。这...
Vue使用pdf-lib为文件... 之前也写过两篇预览pdf的,但是没有加水印,这是链接:Vu...
PyQt5数据库开发1 4.1... 文章目录 前言 步骤/方法 1 使用windows身份登录 2 启用混合登录模式 3 允许远程连接服...
Android studio ... 解决 Android studio 出现“The emulator process for AVD ...
Linux基础命令大全(上) ♥️作者:小刘在C站 ♥️个人主页:小刘主页 ♥️每天分享云计算网络运维...
再谈解决“因为文件包含病毒或潜... 前面出了一篇博文专门来解决“因为文件包含病毒或潜在的垃圾软件”的问题,其中第二种方法有...
南京邮电大学通达学院2023c... 题目展示 一.问题描述 实验题目1 定义一个学生类,其中包括如下内容: (1)私有数据成员 ①年龄 ...
PageObject 六大原则 PageObject六大原则: 1.封装服务的方法 2.不要暴露页面的细节 3.通过r...
【Linux网络编程】01:S... Socket多进程 OVERVIEWSocket多进程1.Server2.Client3.bug&...
数据结构刷题(二十五):122... 1.122. 买卖股票的最佳时机 II思路:贪心。把利润分解为每天为单位的维度,然后收...
浏览器事件循环 事件循环 浏览器的进程模型 何为进程? 程序运行需要有它自己专属的内存空间࿰...
8个免费图片/照片压缩工具帮您... 继续查看一些最好的图像压缩工具,以提升用户体验和存储空间以及网站使用支持。 无数图像压...
计算机二级Python备考(2... 目录  一、选择题 1.在Python语言中: 2.知识点 二、基本操作题 1. j...
端电压 相电压 线电压 记得刚接触矢量控制的时候,拿到板子,就赶紧去测各种波形,结...
如何使用Python检测和识别... 车牌检测与识别技术用途广泛,可以用于道路系统、无票停车场、车辆门禁等。这项技术结合了计...
带环链表详解 目录 一、什么是环形链表 二、判断是否为环形链表 2.1 具体题目 2.2 具体思路 2.3 思路的...
【C语言进阶:刨根究底字符串函... 本节重点内容: 深入理解strcpy函数的使用学会strcpy函数的模拟实现⚡strc...
Django web开发(一)... 文章目录前端开发1.快速开发网站2.标签2.1 编码2.2 title2.3 标题2.4 div和s...