【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?

前言

作者 :“程序员梨子”

**文章简介 **:本篇文章主要利用pygame模块写的一款坦克大战小游戏啦!

**文章源码获取 **: 为了感谢每一个关注我的小可爱每篇文章的项目源码都是无偿分

享滴

点这里蓝色这行字体自取,需要什么源码记得说标题名字哈!私信我也可!

欢迎小伙伴们 点赞、收藏⭐、留言

【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?_第1张图片

正文

《坦克大战》是当年火遍大江南北的FC游戏,小霸王时代的装机必备。现在看来是觉得画面有点

简陋,但在以前绝对是属于玩不腻的游戏之一。今天小编有时间就带大家一起来康康吧~

开始准备的图片素材很多分类处理:以下是部分截图。

​​

(2)不同的音乐背景:当熟悉的背景音乐响起,扑面而来的是整个童年啊!

​​

(3)游戏规则:

游戏有单人和双人两种模式,己方大本营被破或者己方坦克被歼灭则游戏失败,成功通过所有关卡

则游戏胜利。另外,玩家可以通过射击特定的坦克使地图上随机出现一个道具,若己方坦克捡到该

道具,则触发一个事件,例如坦克能力的增强。

【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?_第2张图片​​

玩家操作方式如下:

玩家一:

wsad键:上下左右;

空格键:射击。

玩家二:

↑↓←→键:上下左右;

小键盘0键:射击。

# 玩家一, WSAD移动, 空格键射击
            if tank_player1.num_lifes >= 0:
                if key_pressed[pygame.K_w]:
                    player_tanks_group.remove(tank_player1)
                    tank_player1.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player1)
                elif key_pressed[pygame.K_s]:
                    player_tanks_group.remove(tank_player1)
                    tank_player1.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player1)
                elif key_pressed[pygame.K_a]:
                    player_tanks_group.remove(tank_player1)
                    tank_player1.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player1)
                elif key_pressed[pygame.K_d]:
                    player_tanks_group.remove(tank_player1)
                    tank_player1.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player1)
                elif key_pressed[pygame.K_SPACE]:
                    bullet = tank_player1.shoot()
                    if bullet:
                        self.sounds['fire'].play() if tank_player1.tanklevel < 2 else self.sounds['Gunfire'].play()
                        player_bullets_group.add(bullet)
            # 玩家二, ↑↓←→移动, 小键盘0键射击
            if self.is_dual_mode and (tank_player2.num_lifes >= 0):
                if key_pressed[pygame.K_UP]:
                    player_tanks_group.remove(tank_player2)
                    tank_player2.move('up', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player2)
                elif key_pressed[pygame.K_DOWN]:
                    player_tanks_group.remove(tank_player2)
                    tank_player2.move('down', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player2)
                elif key_pressed[pygame.K_LEFT]:
                    player_tanks_group.remove(tank_player2)
                    tank_player2.move('left', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player2)
                elif key_pressed[pygame.K_RIGHT]:
                    player_tanks_group.remove(tank_player2)
                    tank_player2.move('right', self.scene_elems, player_tanks_group, enemy_tanks_group, home)
                    player_tanks_group.add(tank_player2)
                elif key_pressed[pygame.K_KP0]:
                    bullet = tank_player2.shoot()
                    if bullet:
                        player_bullets_group.add(bullet)
                        self.sounds['fire'].play() if tank_player2.tanklevel < 2 else self.sounds['Gunfire'].play()

加载游戏素材中:

​​​​​​​​【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?_第3张图片​​

附:

​
 # 加载游戏素材
    sounds = {}
    for key, value in cfg.AUDIO_PATHS.items():
        sounds[key] = pygame.mixer.Sound(value)
        sounds[key].set_volume(1)


​

(4)在坦克移动中、打到对方等肯定有碰撞:

 # 碰撞检测
            # --子弹和砖墙
            pygame.sprite.groupcollide(player_bullets_group, self.scene_elems.get('brick_group'), True, True)
            pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('brick_group'), True, True)
            # --子弹和铁墙
            for bullet in player_bullets_group:
                if pygame.sprite.spritecollide(bullet, self.scene_elems.get('iron_group'), bullet.is_stronger, None):
                    player_bullets_group.remove(bullet)
            pygame.sprite.groupcollide(enemy_bullets_group, self.scene_elems.get('iron_group'), True, False)
            # --子弹撞子弹
            pygame.sprite.groupcollide(player_bullets_group, enemy_bullets_group, True, True)
            # --我方子弹撞敌方坦克
            for tank in enemy_tanks_group:
                if pygame.sprite.spritecollide(tank, player_bullets_group, True, None):
                    if tank.food:
                        foods_group.add(tank.food)
                        tank.food = None
                    if tank.decreaseTankLevel():
                        self.sounds['bang'].play()
                        self.total_enemy_num -= 1
            # --敌方子弹撞我方坦克
            for tank in player_tanks_group:
                if pygame.sprite.spritecollide(tank, enemy_bullets_group, True, None):
                    if tank.is_protected:
                        self.sounds['blast'].play()
                    else:
                        if tank.decreaseTankLevel():
                            self.sounds['bang'].play()
                        if tank.num_lifes < 0:
                            player_tanks_group.remove(tank)
            # --我方子弹撞我方大本营
            if pygame.sprite.spritecollide(home, player_bullets_group, True, None):
                is_win = False
                is_running = False
                home.setDead()
            # --敌方子弹撞我方大本营
            if pygame.sprite.spritecollide(home, enemy_bullets_group, True, None):
                is_win = False
                is_running = False
                home.setDead()

(5)坦克游戏中会随机生成食物:

 # --坦克吃到食物
            for player_tank in player_tanks_group:
                for food in foods_group:
                    if pygame.sprite.collide_rect(player_tank, food):
                        if food.name == 'boom':
                            self.sounds['add'].play()
                            for _ in enemy_tanks_group:
                                self.sounds['bang'].play()
                            self.total_enemy_num -= len(enemy_tanks_group)
                            enemy_tanks_group = pygame.sprite.Group()
                        elif food.name == 'clock':
                            self.sounds['add'].play()
                            for enemy_tank in enemy_tanks_group:
                                enemy_tank.setStill()
                        elif food.name == 'gun':
                            self.sounds['add'].play()
                            player_tank.improveTankLevel()
                        elif food.name == 'iron':
                            self.sounds['add'].play()
                            self.__pretectHome()
                        elif food.name == 'protect':
                            self.sounds['add'].play()
                            player_tank.setProtected()
                        elif food.name == 'star':
                            self.sounds['add'].play()
                            player_tank.improveTankLevel()
                            player_tank.improveTankLevel()
                        elif food.name == 'tank':
                            self.sounds['add'].play()
                            player_tank.addLife()
                        foods_group.remove(food)

游戏效果:

​​​​​​​​【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?_第4张图片

​​总结

 这游戏的话经典哈,还是你门记忆中的坦克吗?

说实话代码真挺多的,你感觉这款坦克大战有还原多少呀??

注小编获取更多精彩内容!记得点击传送门哈

记得三连哦! 如需打包好的源码+素材免费分享滴!传送门

【Pygame小游戏】《坦克大战》,那些童年的游戏你还记得几个呢?_第5张图片

你可能感兴趣的:(Python,游戏,程序员,pygame,游戏,python)