The left side of the game works fine and when the ball misses the paddle the score is updated and so is the ball's position, however the entire right side is acting like a paddle and the ball bounces off the wall regardless of the paddle's collision with the wall. (please Ignore type h in the code)
from turtle import Screen, Turtle
from paddle import Paddle
from ball import Ball
import time
from scoreboard import Scoreboard
screen = Screen()
screen.setup(800, 600)
screen.bgcolor("black")
screen.title("My PONGIE")
screen.tracer(0)
r_paddle = Paddle()
r_paddle.goto((350, 0))
l_paddle = Paddle()
l_paddle.goto((-350, 0))
the_ball = Ball()
the_ball.penup()
score = Scoreboard()
screen.listen()
screen.onkey(r_paddle.go_up, "Up")
screen.onkey(r_paddle.go_down, "Down")
screen.onkey(l_paddle.go_up, "w")
screen.onkey(l_paddle.go_down, "s")
game_is_on = True
while game_is_on:
time.sleep(0.1)
screen.update()
the_ball.move()
#collision with wall
if the_ball.ycor() > 280 or the_ball.ycor() < -280:
the_ball.y_bounce()
#collsion with paddle
if the_ball.distance(r_paddle) and the_ball.xcor() > 320 or the_ball.distance(l_paddle) < 50 and the_ball.xcor() < -320:
the_ball.x_bounce()
#miss the right paddle
if the_ball.xcor() > 380:
the_ball.reset_position()
score.left_point()
#miss the left paddle
elif the_ball.xcor() < -380:
the_ball.reset_position()
score.right_point()
screen.exitonclick()
--------------------class scoreboard:
from turtle import Turtle
class Scoreboard(Turtle):
def __init__(self):
super().__init__()
self.color("white")
self.penup()
self.hideturtle()
self.l_score = 0
self.r_score = 0
self.update_score()
def update_score(self):
self.clear()
self.goto(-100, 200)
self.write(self.l_score, False, "center", ("courier", 80, "normal"))
self.goto(100, 200)
self.write(self.r_score, False, "center", ("courier", 80, "normal"))
def left_point(self):
self.l_score += 1
self.update_score()
def right_point(self):
self.r_score += 1
self.update_score()
------------------------------class ball:
from turtle import Turtle
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shape("circle")
self.shapesize(1, 1)
self.color("white")
self.x_move = 10
self.y_move = 10
def move(self):
new_x = self.xcor() + self.x_move
new_y = self.ycor() + self.y_move
self.goto(new_x, new_y)
def y_bounce(self):
self.y_move *= -1
def x_bounce(self):
self.x_move *= -1
def reset_position(self):
self.goto(0, 0)
self.x_bounce()
-------------------------------class paddle:
from turtle import Turtle
class Paddle(Turtle):
def __init__(self):
super().__init__()
self.shape("square")
self.color("white")
self.shapesize(5, 1)
self.penup()
def go_up(self):
new_y = self.ycor() + 20
self.goto(self.xcor(), new_y)
def go_down(self):
new_y = self.ycor() - 20
self.goto(self.xcor(), new_y)
I checked and coded it similar to the left side but after multiple trial and errors, the issue exists.
This expression:
treats the return value of the first
distance()call as a boolean instead of a distance in pixels and was probably meant to read:Below is my rework of your code (consolidated into a single file) with this fix and a number of other small changes: