So, I made this BeakOut game in classbut the ball doesn’t bounce off when it touches the paddle… do you maybe know what went wrong?
Here is the program till now:
import turtle
import random
import time
wn = turtle.Screen()
wn.bgcolor(“black”)
wn.title(“Breakout @byParrot”)
wn.setup(width = 800, height = 600)
wn.tracer(0)
colors = [“red”,“yellow”,“orange”,“green”,“blue”,“purple”,“white”]
block_list = #blank list
#Blocks
y = 280
x = -335
for j in range(5):
for i in range(7):
block_1 = turtle.Turtle()
block_1.color(random.choice(colors))
block_1.shape(“square”)
block_1.shapesize(stretch_wid = 1, stretch_len = 5)
block_1.penup()
block_1.goto(x,y)
block_list.append(block_1) #The list with all blocks
x = x + 110
y = y - 30
x = -335
#Name of the 1st block → [0]
#Name of the 2st block → [1]
#Ball
ball = turtle.Turtle()
ball.color(“white”)
ball.shape(“circle”)
ball.penup()
ball.goto(0,0)
ball.speed(0)
ball.dx = 2
ball.dy = 2
#Paddle
paddle = turtle.Turtle()
paddle.color(“white”)
paddle.shape(“square”)
paddle.shapesize(stretch_wid = 1, stretch_len = 5)
paddle.penup()
paddle.goto(0,-250)
paddle.speed(0)
def go_right():
x_p = paddle.xcor()
x_p = x_p + 20
if x_p < 350:
paddle.setx(x_p)
def go_left():
x_p = paddle.xcor()
x_p = x_p - 20
if x_p > -350:
paddle.setx(x_p)
#Key bindings
wn.listen()
wn.onkeypress(go_right,“Right”)
wn.onkeypress(go_left,“Left”)
#Main Game Loop
while True:
wn.update()
#Move the Ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dx)
#Collide with walls
if ball.xcor() > 390:
ball.setx(390)
ball.dx = -2
if ball.ycor() > (290):
ball.sety(290)
ball.dy = -2
if ball.xcor() < -390:
ball.setx(-390)
ball.dx = 2
if ball.ycor() < -290:
time.sleep(3)
ball.goto(0,0)
#Collide with Paddle
if ball.dy < 0 and ball.ycor() <= -260:
if (paddle.xcor() - 50) < ball.xcor() < (paddle.xcor() + 50):
ball.dy = 2