在好例子网,分享、交流、成长!
您当前所在位置:首页Python 开发实例Python语言基础 → pygame飞机大战

pygame飞机大战

Python语言基础

下载此实例
  • 开发语言:Python
  • 实例大小:1.72M
  • 下载次数:28
  • 浏览次数:235
  • 发布时间:2020-07-19
  • 实例类别:Python语言基础
  • 发 布 人:hgf1220
  • 文件格式:.rar
  • 所需积分:2
 相关标签: pygame 飞机大战 实例 已完成

实例介绍

【实例简介】自己开发的pygame飞机大战游戏(包含素材)

python需要pygame模块才能运行,主程序是game.py(整理了一些数据,在game_date.py里,可以自己改了测试,相当于走游戏后门)

【实例截图】

【核心代码】class Game(object):


    def __init__(self):

        self.main_window = pygame.display.set_mode(SCREEN_RECT.size)
        pygame.display.set_caption(GAME_NAME)

        self.is_game_over = False
        self.is_game_pause = False

        self.all_group = pygame.sprite.Group()
        self.enemies_group = pygame.sprite.Group()
        self.supplies_group = pygame.sprite.Group()

        self.all_group.add(Background(False),Background(True))

        self.hud_panel = HUDPanel(self.all_group)

        self.hero_sprite = Hero(self.all_group)
        self.hud_panel.show_bomb(self.hero_sprite.bomb_count)

        self.create_enemies()

        self.create_supply()

        self.player = MusicPlayer('game_music.ogg')
        self.player.play_music()

    def reset_game(self):

        self.is_game_over = False
        self.is_game_pause = False

        self.hud_panel.reset_panel()

        self.hero_sprite.rect.midbottom = HERO_DEFAULT_MID_BOTTOM

        for enemy in self.enemies_group:
            enemy.kill()

        for bullet in self.hero_sprite.bullets_group:
            bullet.kill()

        self.create_enemies()

    def start(self):

        clock = pygame.time.Clock()

        frame_count = 0

        while True:
            
            self.is_game_over = self.hud_panel.lives_count == 0

            if self.event_handler():

                self.hud_panel.save_best_score()

                return

            if self.is_game_over:

                self.hud_panel.panel_paused(True, self.all_group)
            
            elif self.is_game_pause:

                self.hud_panel.panel_paused(False, self.all_group)
            
            else:

                self.hud_panel.panel_resume(self.all_group)

                keys = pygame.key.get_pressed()
                move_hor = keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]
                move_ver = keys[pygame.K_DOWN] - keys[pygame.K_UP]

                self.check_collide()

                frame_count = (frame_count 1) % FRAME_INTERVAL
                self.all_group.update(frame_count == 0, move_hor, move_ver)
                

            self.all_group.draw(self.main_window)

            
            pygame.display.update()


            clock.tick(60)

    def event_handler(self):

        for event in pygame.event.get():
            if event.type == pygame.QUIT:

                return True
            
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:

                return True

            elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:

                if self.is_game_over:

                    self.reset_game()

                else:

                    self.is_game_pause = not self.is_game_pause
                    self.player.pause_music(self.is_game_pause)
        
            if not self.is_game_over and not self.is_game_pause:

                if event.type == pygame.KEYDOWN and event.key == pygame.K_b: 
                    
                    if self.hero_sprite.hp > 0 and self.hero_sprite.bomb_count > 0:
                        self.player.play_sound('use_bomb.wav')

                    score = self.hero_sprite.blowup(self.enemies_group)
                    self.hud_panel.show_bomb(self.hero_sprite.bomb_count)

                    if self.hud_panel.increase_score(score):

                        print('升级到 %d 级了,继续加油!'% self.hud_panel.level)
                        self.player.play_sound('upgrade.wav')
                        self.create_enemies()

                elif event.type == HERO_DEAD_EVENT:

                    self.hud_panel.lives_count -= 1
                    self.hud_panel.show_lives()
                    self.hud_panel.show_bomb(self.hero_sprite.bomb_count)

                elif event.type == HERO_POWER_OFF_EVENT:

                    self.hero_sprite.is_power = False
                    pygame.time.set_timer(HERO_POWER_OFF_EVENT, 0)

                elif event.type == HERO_FIRE_EVENT:
                    
                    self.player.play_sound('bullet.wav')
                    self.hero_sprite.fire(self.all_group)
                
                elif event.type == THROW_SUPPLY_EVENT:

                    self.player.play_sound('supply.wav')
                    supply = random.choice(self.supplies_group.sprites())
                    supply.throw_supply()

                elif event.type == BULLET_ENHANCED_OFF_EVENT:

                    self.hero_sprite.bullets_group = 0
                    pygame.time.set_timer(BULLET_ENHANCED_OFF_EVENT, 0)

        return False

实例下载地址

pygame飞机大战

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警