坦克在pygame中与墙壁碰撞

问题描述 投票:1回答:2

我一直在从事有关坦克的项目(基于游戏Tank Trouble的工作,并且已经在屏幕上显示了墙壁。当水箱与墙壁碰撞时,如何使水箱停止。

将来,我也计划使子弹也可以撞到墙上。

任何帮助将不胜感激!

完整游戏代码:https://gist.github.com/vairiskovels/1d975e02e4140c116fe259141c75f2e4

class Game:

    def __init__(self):
        self.run = True
        self.screen_width = 1060
        self.screen_height = 798
        self.image = pygame.image.load("bin/sprites/background/background1.png")
        self.image = pygame.transform.scale(self.image, (self.screen_width, self.screen_height))
        self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))

        # all_sprites is used to update and draw all sprites together.
        self.all_sprites = pygame.sprite.Group()

        # for collision detection with enemies.
        self.bullet_group = pygame.sprite.Group()

        # for collision detection with walls.
        self.wall_list = pygame.sprite.Group()

        self.tank = Tank()
        self.all_sprites.add(self.tank)

        self.enemy = Enemy()
        self.all_sprites.add(self.enemy)

        keys = pygame.key.get_pressed()
        if keys[pygame.K_SPACE]:
            bullet = Bullet(self.tank)
            self.bullet_group.add(bullet)
            self.all_sprites.add(bullet)

        # -------------- Walls --------------

        self.wall = Wall(0, 0, 16, 798)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(0, 0, 1060, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(1044, 0, 16, 798)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(0, 782, 1060, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(0, 260, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(260, 0, 16, 130)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(146, 130, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(130, 130, 16, 408)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(146, 522, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(146, 390, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(130, 652, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(146, 652, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(390, 0, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(522, 0, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(390, 130, 148, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(260, 260, 146, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(390, 260, 16, 408)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(406, 390, 132, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(522, 260, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(522, 260, 130, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(522, 522, 16, 260)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(652, 130, 278, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(652, 130, 16, 296)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(782, 0, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(914, 0, 16, 146)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(782, 260, 278, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(782, 390, 148, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(914, 390, 16, 148)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(914, 522, 148, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(914, 652, 148, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(652, 522, 146, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(652, 652, 146, 16)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)

        self.wall = Wall(782, 522, 16, 276)
        self.wall_list.add(self.wall)
        self.all_sprites.add(self.wall)


    def handle_events(self):
        self.tank.handle_events()
        self.enemy.handle_events()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                self.run = False
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    self.run = False
                if event.key == pygame.K_SPACE:
                    bullet = Bullet(self.tank)
                    self.bullet_group.add(bullet)
                    self.all_sprites.add(bullet)

    def update(self):
        # Calls `update` methods of all contained sprites.
        self.all_sprites.update()

    def draw(self):
        self.screen.blit(self.image, (0, 0))
        self.all_sprites.draw(self.screen)  # Draw the contained sprites.
        pygame.display.update()


class Tank(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bin/sprites/player/player_tank.png")
        self.org_image = self.image.copy()

        # A nicer way to set the start pos with `get_rect`.
        self.rect = self.image.get_rect(center=(70, 600))

        self.vel = 3

        self.angle = 270  # starts looking right
        self.direction = pygame.Vector2(1, 0)
        self.pos = pygame.Vector2(self.rect.center)

        self.hp = 1

    def handle_events(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT]:
            self.angle += 3
        if keys[pygame.K_RIGHT]:
            self.angle -= 3
        if keys[pygame.K_UP] and self.rect.left - 5 > 0 and self.rect.top - 5 > 0 and self.rect.right + 5 < 1060 and self.rect.bottom + 5 < 798:
            self.move(-3)
        if keys[pygame.K_DOWN] and self.rect.left - 5 > 0 and self.rect.top - 5 > 0 and self.rect.right + 5 < 1060 and self.rect.bottom + 5 < 798:
            self.move(3)

        self.direction = pygame.Vector2(1, 0).rotate(-self.angle)
        self.image = pygame.transform.rotate(self.org_image, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, vel):
        direction = pygame.Vector2(0, vel).rotate(-self.angle)
        self.pos += direction
        self.rect.center = round(self.pos[0]), round(self.pos[1])


class Enemy(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bin/sprites/enemy/enemy_tank.png")
        self.org_image = self.image.copy()

        self.spawnx = [600, 850, 860]  # spawning x coord
        self.spawny = [70, 200, 700]  # spawning y coord
        self.i = random.randint(0, len(self.spawnx) - 1)

        # A nicer way to set the start pos with `get_rect`.
        self.rect = self.image.get_rect(center=(self.spawnx[self.i], self.spawny[self.i]))

        self.vel = 3
        self.hp = 1

        if self.i == 0:
            self.angle = 180
        elif self.i == 1:
            self.angle = 90
        elif self.i == 2:
            self.angle = 0

        self.direction = pygame.Vector2(1, 0)
        self.pos = pygame.Vector2(self.rect.center)

    def handle_events(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_a]:
            self.angle += 3
        if keys[pygame.K_d]:
            self.angle -= 3
        if keys[pygame.K_w]:
            self.move(-3)
        if keys[pygame.K_s]:
            self.move(3)

        self.direction = pygame.Vector2(1, 0).rotate(-self.angle)
        self.image = pygame.transform.rotate(self.org_image, self.angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, vel):
        direction = pygame.Vector2(0, vel).rotate(-self.angle)
        self.pos += direction
        self.rect.center = round(self.pos[0]), round(self.pos[1])


class Wall(pygame.sprite.Sprite):

    def __init__(self, x, y, width, height):
        super().__init__()

        # Make a wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(dark_gray)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x


class Bullet(pygame.sprite.Sprite):

    def __init__(self, tank):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("bin/sprites/bullet/bullet.png")
        self.image = pygame.transform.scale(self.image, (16, 16))
        self.rect = self.image.get_rect()
        self.rect.centerx = tank.rect.centerx + 3  # How much pixels from tank turret on x axis
        self.rect.centery = tank.rect.centery - 25  # How much pixels from tank turret on y axis
        self.angle = tank.angle
        self.pos = pygame.Vector2(self.rect.center)
        self.direction = pygame.Vector2(0, -10).rotate(-self.angle)
        self.lives = 4  # how many times bounces

    def update(self):
        self.pos += self.direction
        self.rect.center = round(self.pos[0]), round(self.pos[1])

        if self.rect.left < 0:
            self.direction.x *= -1
            self.rect.left = 0
            self.pos.x = self.rect.centerx
            self.lives -= 1
            if self.lives == 0:
                return self.kill()
            bounce.play()

        if self.rect.right > 1060:
            self.direction.x *= -1
            self.rect.right = 1060
            self.pos.x = self.rect.centerx
            self.lives -= 1
            if self.lives == 0:
                return self.kill()
            bounce.play()

        if self.rect.top < 0:
            self.direction.y *= -1
            self.rect.top = 0
            self.pos.y = self.rect.centery
            self.lives -= 1
            if self.lives == 0:
                return self.kill()
            bounce.play()

        if self.rect.bottom > 798:
            self.direction.y *= -1
            self.rect.right = 798
            self.pos.y = self.rect.centery
            self.lives -= 1
            if self.lives == 0:
                return self.kill()
            bounce.play()
python pygame collision
2个回答
1
投票

我在pygame和其他游戏上都不怎么好,但是也许这部影片可能会有所帮助,它是关于碰撞和击打盒我希望将其放在评论中,但我没有声誉。希望对您有所帮助


1
投票

坦克是Sprite对象。墙壁也是Sprite对象,收集在Sprite Sprite中。因此,您可以使用Group来检测碰撞:

Group

注意,wall_list返回导致冲突的pygame.sprite.spritecollide()对象的列表。在您的情况下,坦克碰撞时,列表可能包含1个元素:

pygame.sprite.spritecollide()

或者,您可以遍历造成碰撞的墙壁:

if pygame.sprite.spritecollide(self.tank, self.wall_list, False):
    print("tank collides with wall")

例如,当水箱撞到pygame.sprite.spritecollide()中的墙壁时,您可以取消水箱的移动:

pygame.sprite.spritecollide()
© www.soinside.com 2019 - 2024. All rights reserved.