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)