I'm trying to make simple game using pygame, where there are two ships and they shoot each other. I'm now trying to make them shoot, but since the ships are inside a group, I cannot access their rect. How can I make these ships shoot? Both the ship and the bullets are sprites.
Here is the class for the red bullets:
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)
The only problem I have is linking the bullet with the ship. ss_game is what I'm calling the game, and red_shooters is the group in ss_game, even though, there is only one shooter.