So I'm trying to create a breakout game, and the major problem I have is if the ball hits the edges of the paddle, it gets 'stuck' inside the paddle and phases through until the end.
Is there a way to make it so it only runs the check once every X seconds or something?
It also appears to behave oddly when it hits the edges of the bricks as well.
from turtle import Screen
import time
from paddle import Paddle
from tile import Tile
from ball import Ball
screen = Screen()
screen.setup(width=1000, height=600)
screen.bgcolor("black")
screen.tracer(0)
screen.title("Breakout Game")
paddle_position = (0, -250)
paddle = Paddle(paddle_position)
x_values = [-400, -200, 0, 200, 400]
y_values = [250, 225, 200, 175, 150, 125, 100]
color = ["red", "orange", "yellow", "green", "blue", "purple", "teal"]
brick_list = []
for x in x_values:
for y in y_values:
color_choice = color[y_values.index(y)]
tile_position = (x, y)
tile = Tile(tile_position, color_choice)
brick_list.append(tile)
ball = Ball()
left_keys = ["a", "A", "Left"]
right_keys = ["d", "D", "Right"]
screen.listen()
for key in left_keys:
screen.onkeypress(paddle.go_left_start, key)
screen.onkeyrelease(paddle.go_left_end, key)
for key in right_keys:
screen.onkeypress(paddle.go_right_start, key)
screen.onkeyrelease(paddle.go_right_end, key)
game_is_on = True
while game_is_on:
ball.move()
if paddle.move_left:
if paddle.xcor() - 100 > -500:
x = paddle.xcor()
x -= 2
paddle.setx(x)
if paddle.move_right:
if paddle.xcor() + 100 < 500:
x = paddle.xcor()
x += 2
paddle.setx(x)
if ball.xcor() > 480 or ball.xcor() < -480:
ball.bounce_x()
for item in brick_list:
if item.ycor() - 20 <= ball.ycor() <= item.ycor() + 20 and item.xcor() - 100 <= ball.xcor() <= item.xcor() + 100:
ball.bounce_y()
item.hideturtle()
brick_list.remove(item)
## THIS IS MY PADDLE COLLISION
if paddle.xcor() - 100 <= ball.xcor() <= paddle.xcor() + 100 and ball.ycor() <= paddle.ycor() + 20:
print(ball.xcor() - paddle.xcor())
ball.bounce_y()
if ball.ycor() > 280:
ball.bounce_y()
screen.update()
I'm not sure what I can do, it only happens if it hits the edges of the paddle.