问题描述
我一直在从事有关坦克的项目(基于游戏Tank Trouble),并且已经在屏幕上显示了墙壁.当水箱与墙壁碰撞时,如何使水箱停止.
I've been working on this project about tanks (based on game Tank Trouble) and I've made walls appear on the screen. How can I make the tank stop when it collides with the wall.
将来,我也计划使子弹也可以撞到墙壁上.
In the future, I also plan so the bullets can collide with the wall too.
任何帮助将不胜感激!
完整游戏代码: 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()
推荐答案
坦克是 Sprite
对象.墙壁是 Sprite
对象,也收集到 Group 代码>
wall_list
.因此,您可以使用 pygame.sprite.spritecollide()
以检测冲突:
Tank is a Sprite
object. The walls are Sprite
objects, too and collected in the Group
wall_list
. Thus you can use pygame.sprite.spritecollide()
to detect a collision:
if pygame.sprite.spritecollide(self.tank, self.wall_list, False):
print("tank collides with wall")
请注意, pygame.sprite.spritecollide()
返回引起碰撞的 Wall
对象的列表.在您的情况下,当战车相撞时,列表可能包含1个元素:
Note, pygame.sprite.spritecollide()
returns a list of the Wall
objects, which caused the collision. In your case the list will probably contain 1 element when the tank collides:
hit_walls = pygame.sprite.spritecollide(self.tank, self.wall_list, False)
if hit_walls:
hit_wall = hit_walls[0]
或者,您可以遍历导致碰撞的墙壁:
Alternatively you can iterate through the walls, which caused the collision:
for hit_wall in pygame.sprite.spritecollide(self.tank, self.wall_list, False):
# [...]
例如,当坦克撞到 Game.handle_events
class Game:
# [...]
def handle_events(self):
tank_pos = pygame.math.Vector2(self.tank.pos)
self.tank.handle_events()
if pygame.sprite.spritecollide(self.tank, self.wall_list, False):
self.tank.pos = tank_pos
self.tank.rect.center = round(tank_pos[0]), round(tank_pos[1])
# [...]
这篇关于坦克在pygame中与墙壁碰撞的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!