翼度科技»论坛 编程开发 python 查看内容

python带你成功复刻热门手机游戏——飞翔的小鸟

1

主题

1

帖子

3

积分

新手上路

Rank: 1

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

 
 

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

 
 

一款由越南独立开发者开发的手机游戏,是之前非常流行的一款手机游戏
小游戏目标:让小鸟穿过管子,不要碰到任何物体,挑战更远距离

 
 

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


  • Python 3.8
–> 解释器

  • Pycharm
–> 编辑器
所需素材

音效素材

 
 

图片素材

 
 

效果展示


 
 

 
 


背景啊其他素材啊也是可以修改的
给你们看看博主魔改的背景

 
 

代码展示

使用的模块
  1. import cfg
  2. import sys
  3. import random
  4. import pygame
复制代码
 
游戏初始化
  1. def initGame():
  2.     pygame.init()
  3.     pygame.mixer.init()
  4.     screen = pygame.display.set_mode((cfg.SCREENWIDTH, cfg.SCREENHEIGHT))
  5.     pygame.display.set_caption('Flappy Bird ')
  6.     return screen
复制代码
 
显示当前分数
  1. def showScore(screen, score, number_images):
  2.     digits = list(str(int(score)))
  3.     width = 0
  4.     for d in digits:
  5.         width += number_images.get(d).get_width()
  6.     offset = (cfg.SCREENWIDTH - width) / 2
  7.     for d in digits:
  8.         screen.blit(number_images.get(d), (offset, cfg.SCREENHEIGHT*0.1))
  9.         offset += number_images.get(d).get_width()
复制代码
 
主函数
  1. def main():
  2.     screen = initGame()
  3.     # 加载必要的游戏资源
  4.     # 完整代码、素材领取抠裙:872937351
  5.     sounds = dict()
  6.     for key, value in cfg.AUDIO_PATHS.items():
  7.         sounds[key] = pygame.mixer.Sound(value)
  8.     # --导入数字图片
  9.     number_images = dict()
  10.     for key, value in cfg.NUMBER_IMAGE_PATHS.items():
  11.         number_images[key] = pygame.image.load(value).convert_alpha()
  12.     # --管道
  13.     pipe_images = dict()
  14.     pipe_images['bottom'] = pygame.image.load(random.choice(list(cfg.PIPE_IMAGE_PATHS.values()))).convert_alpha()
  15.     pipe_images['top'] = pygame.transform.rotate(pipe_images['bottom'], 180)
  16.     # --小鸟图片
  17.     bird_images = dict()
  18.     for key, value in cfg.BIRD_IMAGE_PATHS[random.choice(list(cfg.BIRD_IMAGE_PATHS.keys()))].items():
  19.         bird_images[key] = pygame.image.load(value).convert_alpha()
  20.     # --背景图片
  21.     backgroud_image = pygame.image.load(random.choice(list(cfg.BACKGROUND_IMAGE_PATHS.values()))).convert_alpha()
  22.     # --其他图片
  23.     other_images = dict()
  24.     for key, value in cfg.OTHER_IMAGE_PATHS.items():
  25.         other_images[key] = pygame.image.load(value).convert_alpha()
  26.     # 游戏开始界面
  27.     game_start_info = startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg)
  28.     # 进入主游戏
  29.     score = 0
  30.     bird_pos, base_pos, bird_idx = list(game_start_info.values())
  31.     base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
  32.     clock = pygame.time.Clock()
  33.     # --管道类
  34.     pipe_sprites = pygame.sprite.Group()
  35.     for i in range(2):
  36.         pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
  37.         pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('top')[-1])))
  38.         pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=(cfg.SCREENWIDTH+200+i*cfg.SCREENWIDTH/2, pipe_pos.get('bottom')[-1])))
  39.     # --bird类
  40.     bird = Bird(images=bird_images, idx=bird_idx, position=bird_pos)
  41.     # --是否增加pipe
  42.     is_add_pipe = True
  43.     # --游戏是否进行中
  44.     is_game_running = True
  45.     while is_game_running:
  46.         for event in pygame.event.get():
  47.             if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
  48.                 pygame.quit()
  49.                 sys.exit()
  50.             elif event.type == pygame.KEYDOWN:
  51.                 if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
  52.                     bird.setFlapped()
  53.                     sounds['wing'].play()
  54.         # --碰撞检测
  55.         for pipe in pipe_sprites:
  56.             if pygame.sprite.collide_mask(bird, pipe):
  57.                 sounds['hit'].play()
  58.                 is_game_running = False
  59.         # --更新小鸟
  60.         boundary_values = [0, base_pos[-1]]
  61.         is_dead = bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)
  62.         if is_dead:
  63.             sounds['hit'].play()
  64.             is_game_running = False
  65.         # --移动base实现小鸟往前飞的效果
  66.         base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
  67.         # --移动pipe实现小鸟往前飞的效果
  68.         flag = False
  69.         for pipe in pipe_sprites:
  70.             pipe.rect.left -= 4
  71.             if pipe.rect.centerx < bird.rect.centerx and not pipe.used_for_score:
  72.                 pipe.used_for_score = True
  73.                 score += 0.5
  74.                 if '.5' in str(score):
  75.                     sounds['point'].play()
  76.             if pipe.rect.left < 5 and pipe.rect.left > 0 and is_add_pipe:
  77.                 pipe_pos = Pipe.randomPipe(cfg, pipe_images.get('top'))
  78.                 pipe_sprites.add(Pipe(image=pipe_images.get('top'), position=pipe_pos.get('top')))
  79.                 pipe_sprites.add(Pipe(image=pipe_images.get('bottom'), position=pipe_pos.get('bottom')))
  80.                 is_add_pipe = False
  81.             elif pipe.rect.right < 0:
  82.                 pipe_sprites.remove(pipe)
  83.                 flag = True
  84.         if flag: is_add_pipe = True
  85.         # --绑定必要的元素在屏幕上
  86.         screen.blit(backgroud_image, (0, 0))
  87.         pipe_sprites.draw(screen)
  88.         screen.blit(other_images['base'], base_pos)
  89.         showScore(screen, score, number_images)
  90.         bird.draw(screen)
  91.         pygame.display.update()
  92.         clock.tick(cfg.FPS)
  93.     endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg)
复制代码
 
 
尾语
来源:https://www.cnblogs.com/hahaa/p/17204659.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具