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

阅读: 评论:0

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

使用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 += (d).get_width()  offset = (cfg.SCREENWIDTH - width) / 2  for d in digits:  screen.blit((d), (offset, cfg.SCREENHEIGHT*0.1))  offset += (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'] = ate(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() - _width()  clock = pygame.time.Clock()  # --管道类  pipe_sprites = pygame.sprite.Group()  for i in range(2):  pipe_pos = Pipe.randomPipe(cfg, ('top'))  pipe_sprites.add(Pipe(image=('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, ('top')[-1])))  pipe_sprites.add(Pipe(image=('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, ('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 ():  pe == pygame.QUIT or (pe == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):  pygame.quit()  it()  pe == 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 llide_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:  left -= 4  enterx < x and not pipe.used_for_score:  pipe.used_for_score = True  score += 0.5  if '.5' in str(score):  sounds['point'].play()  left < 5 left > 0 and is_add_pipe:  pipe_pos = Pipe.randomPipe(cfg, ('top'))  pipe_sprites.add(Pipe(image=('top'), position=('top')))  pipe_sprites.add(Pipe(image=('bottom'), position=('bottom')))  is_add_pipe = False  ight < 0:  ve(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必备资料(视频+源码+工具+软件) 安全链接免费领取

本文发布于:2024-02-01 05:52:03,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170673792534364.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:我连   手机游戏   小鸟   热门   Python
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23