-1

I'm trying to code some breakout game with Pygame, but I thought i'd need to define some special kind of collisions between the ball and the block, but it doesn't really work :

class Block:

    def __init__(self, pg_game, width, height, color):
        super().__init__()
        self.screen = pg_game.screen
        self.screen_rect = pg_game.screen.get_rect()
        self.settings = pg_game.settings
        self.color = color

        self.rect = pygame.Rect(0, 0, width, height)

    def draw_block(self):
        pygame.draw.rect(self.screen, self.color, self.rect)

class Breakout :
    def draw_wall(self):
        block = Block(self, 300, 200, "red")

        self.blocks.append(block)

    def _check_ball_block_collisions(self):
        for block in self.blocks:
            if self.ball.moving_up :
                if self.ball.moving_right:
                    if self.ball.rect.right == block.rect.left:
                        self.ball.moving_right = False
                        self.ball.moving_left = True
                    elif self.ball.rect.top == block.rect.bottom:
                        
                        self.ball.moving_up = False
                        self.ball.moving_down = True
                elif self.ball.moving_left :
                    if self.ball.rect.left == block.rect.right:
                        
                        self.ball.moving_left = False
                        self.ball.moving_right = True
                    elif self.ball.rect.top == block.rect.bottom:
                        
                        self.ball.moving_up = False
                        self.ball.moving_down = True
            elif self.ball.moving_down :
                if self.ball.moving_right :
                    if self.ball.rect.right == block.rect.left:
                        
                        self.ball.moving_right = False
                        self.ball.moving_left = True
                    elif self.ball.rect.bottom == block.rect.top:
                        
                        self.ball.moving_down = False
                        self.ball.moving_up = True
                elif self.ball.moving_left :
                    if self.ball.rect.left == block.rect.right:
                        
                        self.ball.moving_left = False
                        self.ball.moving_right = True
                    elif self.ball.rect.bottom == block.rect.top:
                        
                        self.ball.moving_down = False
                        self.ball.moving_up = True
def _update_screen(self):
    for block in self.blocks:
        block.draw_block()

        pygame.display.flip()

NOTE : I shortened the code by removing some functions, but in the original code, everything is updated, these are just the lines of codes I think the problem comes from ; This is a test version.

The hitbox of the block doesn't match, my ball is bouncing even when it doesn't even touch it. Should I use some Pygame function, and which one ?

  • you could use colliderect. block.rect.colliderect(ballrect) –  Jan 16 '22 at 08:35
  • I don't think I can specify which side of the block the ball collides with by using this function, that's why I thought it would be better to define the collisions by myself. The problem is really that the block's height and size are infinite lines (i suck at pygame, so idk how to use it) –  Jan 17 '22 at 17:43
  • The ball needs to bounce when it collides with any side of of the rectangle isn't it ? So you dont really need the side. –  Jan 17 '22 at 17:46
  • The ball will bounce differently depending the side of the block it collides with : Imagine the ball goes up and right ; If it collides with the left of the block, it will go up and left ; If it collides with the bottom of the block, it will go down and right. –  Jan 17 '22 at 17:50
  • well you already have that information in the four self.ball.moving.___ variables. You dont need to get that from collision. What I think would help is if you stored single bool per direction. Then, you could bounce the ball by simply "flipping" the bools. `if colliderect. block.rect.colliderect(ballrect): self.ball.moving_down = not self.ball.moving_down; self.ball.moving_right = not self.ball.moving_right` –  Jan 17 '22 at 17:58
  • but still, I don't understand why it does that. I didn't define the height and width wrong, did I ? I used the same thing to make the ball bounce off the screen, and it works fine, and I used it on the bar rect earlier and the ball bounced off it perfectlyfine. but just... not with the Block, i don't get it –  Jan 17 '22 at 19:29
  • "but still, I don't understand why it does that". Does what? –  Jan 17 '22 at 21:05
  • the bouncing bug –  Jan 19 '22 at 16:29

0 Answers0