Я пытаюсь сделать простую игру, используя pygame, где есть два корабля, и они стреляют друг в друга. Я сейчас пытаюсь заставить их стрелять, но так как корабли внутри группы, я не могу получить доступ к их ректу. Как заставить эти корабли стрелять? И корабль, и пули — спрайты.
Вот класс для красных пуль:
class RedBullet(Sprite):
"""Represent one bullet shot from the shooter."""
def __init__(self, ss_game):
"""Create a bullet shot from shooter."""
super().__init__()
self.screen = ss_game.screen
self.screen_rect = ss_game.screen.get_rect()
self.color = (255, 0, 0)
# Here is where the problem appears
# Create bullet and place at (0, 0) and then set correct position
self.rect = pygame.Rect(0, 0, 15, 3)
self.rect.right = ss_game.red_shooters.right
# Set position as decimal
self.x = float(self.rect.x)
def update(self):
"""Move the bullet across the screen."""
self.x += 1
# Set rect position
self.rect.x = self.x
def draw_bullet(self):
"""Draw the bullet on the screen."""
pygame.draw.rect(self.screen, self.color, self.rect)
Единственная проблема, с которой я столкнулся, это связь пули с кораблем. ss_game — это то, что я называю игрой, а red_shooters — это группа в ss_game, хотя там только один стрелок.