The commented part is where I believe the issue is at.

from livewires import games, color
import random

games.init(screen_width = 640, screen_height = 480, fps = 50)

class Paddle(games.Sprite):

    image = games.load_image("paddle.bmp")

    def __init__(self):
        super(Paddle, self).__init__(image = Paddle.image,
                                     x = 10,
                                     y = games.mouse.y,
                                     bottom = games.screen.height)
    def update(self):
        self.y = games.mouse.y

        if self.left < 0:
            self.left = 0

        if self.right > games.screen.width:
            self.right = games.screen.width

        self.check_hit()

    def check_hit(self):
        for ball in self.overlapping_sprites:
            ball.handle_hit()
            
class Ball(games.Sprite):

    image = games.load_image("ball.bmp")
    speed = 5

    def __init__(self):
        super(Ball, self).__init__(image = Ball.image,
                                   x = 630,
                                   y = random.randrange(480),
                                  # dx = random.randrange(-2, 4),
                                  # dy = random.randrange(-5, 5))
        self.score = games.Text(value = 0, size = 25, color = color.blue,
                                top = 14, right = games.screen.width - 25)
        games.screen.add(self.score)
                    
    def update(self):
        if self.right > games.screen.width or self.left < 0:
            self.dx = -self.dx

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

        if self.left < 0:
            self.end_game()
            self.destroy()

    def handle_hit(self):
        self.dx = -self.dx
        self.dy = -self.dy
        for ball in self.overlapping_sprites:
            self.score.value += 5
            self.score.right = games.screen.width - 10
            new_ball = Ball()
            games.screen.add(new_ball)

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

def main():
    wall_image = games.load_image("background.bmp", transparent = False)
    games.screen.background = wall_image

    the_paddle = Paddle()
    games.screen.add(the_paddle)
    
    the_ball = Ball()
    games.screen.add(the_ball)
    
    games.mouse.is_visible = False
    games.screen.event_grab = True

    games.screen.mainloop()

main()

Occasionally, the ball that appears will just bounce up and down the screen in the same spot or left to right in the same spot, as if either the dx or dy value is not being set. Is this because it randomly is set to 0? If so, what is a good way to make sure it doesn't set the value to 0?

Recommended Answers

Could you throw in an if statement or while loop that reassigns dx/dy if they are 0?

Jump to Post

All 4 Replies

Could you throw in an if statement or while loop that reassigns dx/dy if they are 0?

Thanks! I thought it was something along those lines, but I couldn't figure out where to put it. I inserted it in the update method of Ball() and it works fine now.

However, I still have a tiny kink where the it will sometimes insert dozens of balls on the screen at once, instead of the normal one ball appearing. I'm thinking that it has something to do with the ball still being at the top of the paddle when it hits and bounces off, but I'm not sure.

Any ideas?

You forgot to attach the images to your post, so we can not test the code (easily).

Could you throw in an if statement or while loop that reassigns dx/dy if they are 0?

You can also use this or-trick of making it 1 if you do not mind twisting odds to 1's favour (or any other number).

>>> import random
>>> for i in range(100):
	if random.randrange(-2, 4) == 0 : print 0,

	
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>>> 
>>> for i in range(100):
	dx=random.randrange(-2, 4) or 1
	if dx==0:print dx
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.