Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

导语

上班摸鱼有没有玩游戏啊!

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

如果没有

那你也肯定没有玩坠落的小鸟主题游戏咯~

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

不过没有关系

木子这就放大图给你过过眼瘾:

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

看看这个界面还真有app游戏软件哪味儿了!

这个可是大人小孩都可以玩的游戏哦~

真的敲简单的!只要点击空格开始然后点击空格路过管道就可以啦。

当然。。。小编基本上只能过10几个管道柱子撒!再多的不行了。

那是因为小编把速度调快了~~更有挑战性~手速慢的也可以调慢点儿撒!!

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

正文

游戏规则:小鸟需要顺利躲过管道,不能碰撞到,不然会显示0,更新的小鸟重新到第一关,

过多少则显示得分多少,测试下你玩游戏的反应能力叭~

环境安装部分:

import cfg
import sys
import random
import pygame

导入需要的图片素材、设置的音乐等:

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()

既然有管道那游戏的话需要得分过关肯定是不能碰到管道的,碰到直接更新小鸟在重新开始。

 # --碰撞检测
        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

游戏开始、结束的界面:

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。
'''显示开始界面'''
def startGame(screen, sounds, bird_images, other_images, backgroud_image, cfg):
    base_pos = [0, cfg.SCREENHEIGHT*0.79]
    base_diff_bg = other_images['base'].get_width() - backgroud_image.get_width()
    msg_pos = [(cfg.SCREENWIDTH-other_images['message'].get_width())/2, cfg.SCREENHEIGHT*0.12]
    bird_idx = 0
    bird_idx_change_count = 0
    bird_idx_cycle = itertools.cycle([0, 1, 2, 1])
    bird_pos = [cfg.SCREENWIDTH*0.2, (cfg.SCREENHEIGHT-list(bird_images.values())[0].get_height())/2]
    bird_y_shift_count = 0
    bird_y_shift_max = 9
    shift = 1
    clock = pygame.time.Clock()
    while True:
        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:
                    return {'bird_pos': bird_pos, 'base_pos': base_pos, 'bird_idx': bird_idx}
        sounds['wing'].play()
        bird_idx_change_count += 1
        if bird_idx_change_count % 5 == 0:
            bird_idx = next(bird_idx_cycle)
            bird_idx_change_count = 0
        base_pos[0] = -((-base_pos[0] + 4) % base_diff_bg)
        bird_y_shift_count += 1
        if bird_y_shift_count == bird_y_shift_max:
            bird_y_shift_max = 16
            shift = -1 * shift
            bird_y_shift_count = 0
        bird_pos[-1] = bird_pos[-1] + shift
        screen.blit(backgroud_image, (0, 0))
        screen.blit(list(bird_images.values())[bird_idx], bird_pos)
        screen.blit(other_images['message'], msg_pos)
        screen.blit(other_images['base'], base_pos)
        pygame.display.update()
        clock.tick(cfg.FPS)
'''游戏结束界面'''
def endGame(screen, sounds, showScore, score, number_images, bird, pipe_sprites, backgroud_image, other_images, base_pos, cfg):
    sounds['die'].play()
    clock = pygame.time.Clock()
    while True:
        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:
                    return
        boundary_values = [0, base_pos[-1]]
        bird.update(boundary_values, float(clock.tick(cfg.FPS))/1000.)
        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)

总结

玩了坠落的小鸟游戏的新迷们!有没有超过66根管道柱子呀??期待.jpg

小编会继续再接再厉为大家谋福利哒!记得三连哦~你们的支持是我的动力,奥里给~

​🎊🎊源码基地: 关注小编获取哦~💝记得三连吖

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

…………..更多内容敬请期待………..

Original: https://blog.csdn.net/weixin_55822277/article/details/120017897
Author: 顾木子吖
Title: Pygame实战:程序员小哥给女友写了一款锻炼反应能力的游戏,从此上班摸鱼再也没被扣工资。

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/781508/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球