I’m stuck for over two days. When i run my code, the ball is moving forwards and backwards in just one direction which is diagonally, so I’m asking if there’s someone who can help me with that please, so the ball can be moved to hit all the bricks.
***my ball file
from turtle import Turtle
import random
class Ball(Turtle):
direction = [0, 90, 180, 270]
def __init__(self, position_x, position_y):
super().__init__()
self.shape("circle")
self.color("red")
self.shapesize(stretch_len=0.75, stretch_wid=0.75)
self.penup()
self.goto(position_x, position_y)
self.move_speed = 0.1
self.x_move = 10
self.y_move = 10
def move_ball(self):
random_x = self.xcor() + self.x_move
random_y = self.xcor() + self.x_move
self.goto(random_x, random_y)
def bounce_x_position(self):
self.x_move *= -1
self.move_speed *=0.9
def bounce_y_position(self):
self.y_move *= -1
def move_faster(self):
self.move_speed *=0.1
def reset_game_position(self, position_x, position_y):
self.goto(position_x, position_y)
self.move_speed = 0.1
self.bounce_y_position()
def reset_game_speed(self):
self.move_speed = 0.1
bricks file**************
from turtle import Turtle
class Brick:
def __init__(self):
self.list_of_bricks = []
def create_bricks(self, position_x, position_y, colour):
current_brick = Turtle("square")
current_brick.speed("fastest")
current_brick.penup()
current_brick.hideturtle()
current_brick.setposition(position_x, position_y)
current_brick.showturtle()
current_brick.shapesize(1, 3)
current_brick.color(colour)
self.list_of_bricks.append(current_brick)
Paddle file*****
from turtle import Turtle, Screen
import random
screen = Screen()
class Paddle(Turtle):
STARTING_POSITION = (0, -330)
FINISH_RIGHT = 360
FINISH_LEFT = -360
def __init__(self):
super().__init__()
self.speed("fastest")
self.shape("square")
self.color("white")
self.penup()
self.shapesize(1, 10)
self.setposition(0, -330)
self.move = 20
def move_left(self):
new_x = self.xcor() - self.move
self.goto(new_x, self.ycor())
def move_right(self):
new_x = self.xcor() + self.move
self.goto(new_x, self.ycor())
def bounce(self):
self.goto(Paddle.STARTING_POSITION)
def is_at_finish_line(self):
if self.xcor() > Paddle.FINISH_RIGHT or self.xcor() < Paddle.FINISH_LEFT:
return True
else:
return False
***************MAIN FILE
from turtle import Turtle, Screen
import time
from paddle import Paddle
from bricks import Brick
from ball import Ball
import random
turtle = Turtle()
screen = Screen()
screen.title("Breakout Game")
screen.setup(width=920, height=700)
turtle.hideturtle()
screen.bgcolor("black")
screen.tracer(0, 0)
paddle = Paddle()
brick = Brick()
screen.listen()
ball = Ball(0, -320)
screen.onkeypress(paddle.move_left, "Left")
screen.onkeypress(paddle.move_right, "Right")
game_is_on = True
def show_bricks():
position_x = 500
position_y = 400
colors = ["blue", "orange", "purple", "green"]
choose_color = -1
for i in range(1, 97):
color = colors[choose_color]
brick.create_bricks( position_x, position_y, color)
position_x -= 100
if i % 12 == 0:
position_x = 500
position_y -= 50
if i % 24 == 0:
choose_color += 1
show_bricks()
def play_game():
while game_is_on:
screen.update()
time.sleep(ball.move_speed)
ball.move_ball()
if ball.ycor() > 300 or ball.ycor() < -300:
ball.bounce_y_position()
if ball.xcor() > 340 or ball.xcor() < -340:
ball.bounce_x_position()
if ball.distance(paddle) < 40 and ball.xcor() < -285:
ball.bounce_x_position()
if paddle.is_at_finish_line():
paddle.bounce()
play_game()
screen.exitonclick()