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

飞翔的小鸟(游戏英文名: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 = 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()
# 加载必要的游戏资源
# --导入音频
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)
‘’‘run’‘’
if __name__ == '__main__':
while True:
main()
代码太多,我就没放完啦,完整源码、素材皆可点击文章下方名片获取此处跳转
好了,今天的分享就差不多到这里了!
完整代码、更多资源、疑惑解答直接点击下方名片自取即可。
对下一篇大家想看什么,可在评论区留言哦!看到我会更新哒(ง •_•)ง
喜欢就关注一下博主,或点赞收藏评论一下我的文章叭!!!

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
这个问题在这里已经有了答案:关闭10年前。PossibleDuplicate:Pythonconditionalassignmentoperator对于这样一个简单的问题表示歉意,但是谷歌搜索||=并不是很有帮助;)Python中是否有与Ruby和Perl中的||=语句等效的语句?例如:foo="hey"foo||="what"#assignfooifit'sundefined#fooisstill"hey"bar||="yeah"#baris"yeah"另外,类似这样的东西的通用术语是什么?条件分配是我的第一个猜测,但Wikipediapage跟我想的不太一样。
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
其实做自媒体的成本并不高,入门只需要一部手机即可!在手机上找视频素材、使用手机剪辑视频、最后使用手机发布视频作品获得收益!方法并不难,今天这期内容就来给粉丝们分享一种小方法,每天稳定收益100-300,抓紧点赞收藏!1、找素材(1)使用手机拍摄自己喜欢的经典段落,使用程序把文案内容提取出来(2)也可以在豆瓣、知乎、微博等网站中找一些自己需要的文案素材(3)把文案进行润色修改,可以加入一些自己的观点(4)视频素材可以使用软件中自带的素材,也可以在素材网站中下载完整版的素材2、文案配音(1)把复制好的文案直接导入小程序中(2)调整音色、音调后一键合成音频即可(3)可以选择自己朗读配音,需要花一点时
华为OD机试题本篇题目:明明的随机数题目输入描述输出描述:示例1输入输出说明代码编写思路最近更新的博客华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典【华为OD机试】全流程解析+经验分享,题型分享,防作弊指南华为o
我想解析一个已经存在的.mid文件,改变它的乐器,例如从“acousticgrandpiano”到“violin”,然后将它保存回去或作为另一个.mid文件。根据我在文档中看到的内容,该乐器通过program_change或patch_change指令进行了更改,但我找不到任何在已经存在的MIDI文件中执行此操作的库.他们似乎都只支持从头开始创建的MIDI文件。 最佳答案 MIDIpackage会为您完成此操作,但具体方法取决于midi文件的原始内容。一个MIDI文件由一个或多个音轨组成,每个音轨是十六个channel中任何一个上的
本文主要介绍在使用Selenium进行自动化测试或者任务时,对于使用了iframe的页面,如何定位iframe中的元素文章目录场景描述解决方案具体代码场景描述当我们在使用Selenium进行自动化测试的时候,可能会遇到一些界面或者窗体是使用HTML的iframe标签进行承载的。对于iframe中的标签,如果直接查找是无法找到的,会抛出没有找到元素的异常。比如近在咫尺的例子就是,CSDN的登录窗体就是使用的iframe,大家可以尝试通过F12开发者模式查看到的tag_name,class_name,id或者xpath来定位中的页面元素,会抛出NoSuchElementException异常。解决
2022/8/4更新支持加入水印水印必须包含透明图像,并且水印图像大小要等于原图像的大小pythonconvert_image_to_video.py-f30-mwatermark.pngim_dirout.mkv2022/6/21更新让命令行参数更加易用新的命令行使用方法pythonconvert_image_to_video.py-f30im_dirout.mkvFFMPEG命令行转换一组JPG图像到视频时,是将这组图像视为MJPG流。我需要转换一组PNG图像到视频,FFMPEG就不认了。pyav内置了ffmpeg库,不需要系统带有ffmpeg工具因此我使用ffmpeg的python包装p
ValidPalindromeGivenastring,determineifitisapalindrome,consideringonlyalphanumericcharactersandignoringcases. [#125]Example:"Aman,aplan,acanal:Panama"isapalindrome."raceacar"isnotapalindrome.Haveyouconsiderthatthestringmightbeempty?Thisisagoodquestiontoaskduringaninterview.Forthepurposeofthisproblem