Question

trying to get the ball to move along the y coordinate, it wont work, more explanation at bottom

from livewires import games, color
games.init(screen_width = 640, screen_height = 480, fps = 50)
points = games.Text(value = 0, size = 25, color = color.green,
                                bottom = games.screen.height - 5, left = 10)
games.screen.add(points)

class Paddle(games.Sprite):
    image = games.load_image("paddle.bmp")
    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.image, y = games.mouse.y, right = games.screen.width)

    def update(self):
        """ Move to mouse x 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_bounce()

    def check_bounce(self):
        for bouncingBall in self.overlapping_sprites:
            bouncingBall.handle_bounce()

class BouncingBall(games.Sprite):
    image = games.load_image("ball.bmp")
    def __init__(self, x, y, dx, dy):
        super(BouncingBall, self).__init__(image = BouncingBall.image, x = x, y = y, dx = dx, dy = dy)

    def update(self):
        """ Check if bottom edge has reached screen bottom. """
        if self.top > 0 or self.bottom < games.screen.height:
            self.dy = -self.dy
        if self.left < 0:
            self.dx = -self.dx
        if self.right > games.screen.width:
            self.end_game()

    def handle_bounce(self):
        global points
        points.value += 10
        points.left = 10
        self.dx = -self.dx

    def end_game(self):
        end_message = games.Message(value = "GAME OVER",
                                    size = 90,
                                    color = color.red,
                                    x = games.screen.width/2,
                                    y = games.screen.height/2,
                                    lifetime = 10 * games.screen.fps,
                                    after_death = games.screen.quit)
        games.screen.add(end_message)


def main():
    background_image = games.load_image("background.png", transparent = False)
    games.screen.background = background_image
    the_paddle = Paddle()
    games.screen.add(the_paddle)
    games.mouse.is_visible = False
    new_ball = BouncingBall(x = games.screen.width/2, y = games.screen.height/2, dx = 2, dy = 2) #<-believe it is right here im messing up
    games.screen.add(new_ball)
    games.screen.mainloop()

main()

I am having a horrible time at getting my ball to correctly follow the y coordinate, I believe I am doing it wrong when i create instance new_ball, (main function) but i have no idea lol, anyone see what im doing wrong?

Was it helpful?

Solution

Make sure that this line

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

Isn't constantly evaluating to True. If so, your y-velocity will constantly toggle and the ball will never appear to change y coordinate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top