美文网首页
12-坦克大战3

12-坦克大战3

作者: 痞子_4ae7 | 来源:发表于2020-05-15 16:09 被阅读0次

    坦克大战之实现敌方坦克的随机射击

        def shot(self):
            num = random.randint(1,1000)
            if num  <= 20:
                return Bullet(self)
    

    坦克大战之我方子弹与敌方坦克的碰撞处理

    ef hit_tank(self):
            for eTank in MainGame.enemy_tank_list:
                result = pygame.sprite.collide_rect(eTank,self)
                if result:
                    self.live = False
                    eTank.live = False
    

    坦克大战之实现爆炸效果

    class Explode(BaseItem):
        def __init__(self,rect):
            self.images = [
                pygame.image.load('img/blast0.gif'),
                pygame.image.load('img/blast1.gif'),
                pygame.image.load('img/blast2.gif'),
                pygame.image.load('img/blast3.gif'),
                pygame.image.load('img/blast4.gif'),
                pygame.image.load('img/blast5.gif'),
                pygame.image.load('img/blast6.gif')
            ]
            self.rect = rect
            self.image = self.images[0]
            self.live = True
            #记录当前图片的索引
            self.step = 0
        #展示爆炸效果
        def display_explode(self):
            if self.step < len(self.images):
                MainGame.window.blit(self.image,self.rect)
                self.image = self.images[self.step]
                self.step += 1
            else:
                self.live = False
                self.step = 0
    

    坦克大战之敌方子弹与我方坦克的碰撞

     def hit_my_tank(self):
        for eBullet in MainGame.enemy_bullet_list:
            if MainGame.PI_TANK and MainGame.PI_TANK.live:
                result = pygame.sprite.collide_rect(eBullet, MainGame.PI_TANK)
                if result:
                    #创建一个爆炸,加入到爆炸效果列表
                    explode = Explode(MainGame.PI_TANK.rect)
                    MainGame.explode_list.append(explode)
                    # 修改状态
                    eBullet.live = False
                    MainGame.PI_TANK.live = False
                else:
                    break
    

    坦克大战之实现墙壁类

    class Wall(BaseItem):
        def __init__(self,left,top):
            self.image = pygame.image.load('img/steels.gif')
            self.rect = self.image.get_rect()
            self.rect.left = left
            self.rect.top = top
        def display_wall(self):
            # 将墙壁加到窗口中
            MainGame.window.blit(self.image,self.rect)
    

    坦克大战之实现子弹不能穿墙

    def hit_wall(self):
        for wall in MainGame.wall_list:
            result = pygame.sprite.collide_rect(wall, self)
            if result:
               self.live = False
    

    坦克大战之实现坦克不能穿墙

     def move(self):
            #先记录移动之前的坐标(供还原时用)
            self.old_top = self.rect.top
            self.old_left = self.rect.left
            #修改tank的坐标:取决于坦克的方向
            if self.direction == 'U':
                if self.rect.top > 0:
                    self.rect.top -= self.speed
            elif self.direction == 'D':
                if self.rect.top < MainGame.SCREEN_HEIGHT-self.rect.height:
                    self.rect.top += self.speed
            elif self.direction == 'L':
                if self.rect.left > 0:
                    self.rect.left -= self.speed
            elif self.direction == 'R':
                if self.rect.left < MainGame.SCREEN_WIDTH-self.rect.width:
                    self.rect.left += self.speed
        # 坦克坐标还原的方法
        def stay(self):
            self.rect.left = self.old_left
            self.rect.top = self.old_top
        # 坦克与墙壁的碰撞方法
        def hit_wall(self):
            for wall in MainGame.wall_list:
                result = pygame.sprite.collide_rect(wall,self)
                if result:
                    #碰撞后,调用坐标还原方法
                    self.stay()
    

    坦克大战之音效处理

    class Musice():
        def __init__(self,music):
            pygame.mixer.init()
            self.music = music
            pygame.mixer.music.load(self.music)
        def play_music(self,count):
            pygame.mixer.music.play(count)
    
    

    相关文章

      网友评论

          本文标题:12-坦克大战3

          本文链接:https://www.haomeiwen.com/subject/wsqtohtx.html