1

I am writing a Breakout game. Collision seems to work fine whenever I upload an image. But it will stop functioning if I use a rect. In the first block of code I used pygame.Surface in which case collision did not work.

In second block I used an uploaded pic and it worked seamlessly. I cannot figure out why.

# Slider
class Slider(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((100, 10))
        self.image.fill(RED)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.mask = pygame.mask.from_surface(self.image)

    def slide_left(self):
        self.rect.x -= 13

    def slide_right(self):
        self.rect.x += 13


# Ball
class Ball(pygame.sprite.Sprite):
    BALL_SPEED = 5
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        self.direction_x = 1
        self.direction_y = 1
        self.image = pygame.image.load("ball.png")
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        self.mask = pygame.mask.from_surface(self.image)
halfer
  • 19,471
  • 17
  • 87
  • 173
  • I'm just guessing, but I would remove the `self.mask` from the `Slider`, and re-test. Reading the doco, it *should* work though - unless you defined `RED` to have a significant amount of transparency. What is `RED` ? – Kingsley Jun 02 '20 at 22:34
  • `RED = (194, 58, 58)` also tried to remove the mask, didnt resolve it. I'm trying to think why images work, whats the difference – hello_there1 Jun 03 '20 at 00:31

0 Answers0