2 years ago

#55393

test-img

user16774307

Pygame special collision

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 ?

python

pygame

collision

0 Answers

Your Answer

Accepted video resources