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.

1

There are 1 best solutions below

1
cdlane On

This expression:

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()

treats the return value of the first distance() call as a boolean instead of a distance in pixels and was probably meant to read:

if the_ball.distance(r_paddle) < 50 and the_ball.xcor() > 320 or the_ball.distance(l_paddle) < 50 and the_ball.xcor() < -320:
        the_ball.x_bounce()

Below is my rework of your code (consolidated into a single file) with this fix and a number of other small changes:

from turtle import Screen, Turtle

class Scoreboard(Turtle):

    def __init__(self):
        super().__init__()

        self.hideturtle()
        self.color('white')
        self.penup()

        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, align='center', font=('Courier', 80, 'normal'))
        self.goto(100, 200)
        self.write(self.r_score, align='center', font=('Courier', 80, 'normal'))
        screen.update()

    def left_point(self):
        self.l_score += 1
        self.update_score()

    def right_point(self):
        self.r_score += 1
        self.update_score()

class Ball(Turtle):

    def __init__(self):
        super().__init__()

        self.shape('circle')
        self.color('white')
        self.x_move = 10
        self.y_move = 10

    def move(self):
        x, y = self.position()
        self.goto(x + self.x_move, y + self.y_move)
        screen.update()

    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()
        screen.update()

class Paddle(Turtle):

    def __init__(self):
        super().__init__()

        self.shape('square')
        self.color('white')
        self.shapesize(1, 5)
        self.setheading(90)
        self.penup()

    def go_up(self):
        self.forward(20)
        screen.update()

    def go_down(self):
        self.backward(20)
        screen.update()

screen = Screen()
screen.setup(800, 600)
screen.bgcolor('black')
screen.title("My PONGIE")
screen.tracer(0)

r_paddle = Paddle()
r_paddle.setx(350)
l_paddle = Paddle()
l_paddle.setx(-350)

the_ball = Ball()
the_ball.penup()

score = Scoreboard()

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')
screen.listen()

def play():
    the_ball.move()
    # collision with wall
    if not -280 < the_ball.ycor() < 280:
        the_ball.y_bounce()
    # collsion with paddle
    elif the_ball.distance(r_paddle) < 50 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
    elif 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.ontimer(play, 100)  # in milliseconds

screen.update()

play()

screen.mainloop()