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?