I'm making a Pong game using Pygame through livewires and I can't get the collision detection to work propery

432 views Asked by At

Working on a pong game while I am reading the Python Programming for the beginner book and I am lost as to what I am missing in this game's code. Everything in the game works except for whenever the ball overlaps the board sprite it doesn't detect it and it goes right to the edge of the window, ending the game.

# single player pong
# the player must rebound the ball to avoid it going off screen.

from livewires import games, color

games.init(screen_width = 1152, screen_height = 768, fps = 60)

class board(games.Sprite):
    """The object used to bounce the ball."""
    image = games.load_image("paddle.png")

    def __init__(self):
        """initialize the board"""
        super(board, self).__init__(image = board.image,
                                   y = games.mouse.y,
                                   right = games.screen.width)

    def update(self): 
        """
        :return:move mouse to y position
        """
        self.y = games.mouse.y

        if self.top < 0:
            self.top = 0

        if self.bottom > games.screen.height:
            self.bottom = games.screen.height
        self.check_rebound()


    def check_rebound(self):
        """Check for ball rebound"""
        for ball in self.overlapping_sprites:
            ball.rebound()


class ball(games.Sprite):
    """ A bouncing pizza."""
    image = games.load_image("ball.png")
    def __init__(self):
        """initialize the ball"""
        super(ball, self).__init__(image = ball.image,
                                  x = games.screen.width/2,
                                  y = games.screen.height/2,
                                  dx = 1,
                                  dy = 1)

    def end_game(self):
        """ End the game. """
        end_message = games.Message(value = "Game Over",
                                    size = 90,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 5 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)

    def update(self):
        """ Reverse a velocity component if edge of screen is reached and end game if the right    wall is reached."""
        if  self.left < 0:
            self.dx = -self.dx

        if self.bottom > games.screen.height or self.top < 0:
            self.dy = -self.dy

        if self.right > games.screen.width:
            self.end_game()
            self.destroy()


    def rebound(self):
        "Ball rebounds from board."
        self.dx = -self.dx
        self.dy = -self.dy

def main():
    """ Play the game. """


    the_board = board()
    games.screen.add(the_board)

    the_ball = ball()
    games.screen.add(the_ball)
    games.screen.add(the_ball)

    games.mouse.is_visible = False

    games.screen.event_grab = True
    games.screen.mainloop()

# start it up!
main()

Apologies if the code is sorted a bit wonky, I had to quadruple space it to work on the site.

1

There are 1 answers

0
David On

Remove dy = -dy in the rebound method.