Search code examples
pythonturtle-graphicsrestartpython-turtlepong

Little black square in center of screen after each restart in turtle graphics pong game


I tried to make a pong game on the turtle extension on Python and everything went smoothly.

I wanted to restart the game when a player reached an certain amount of points. The game restarts properly and the windows clears itself, except from a dark square in the center which stays during the next games. How can I get rid of that square?

Here is the the code where I restart the game:

import turtle

def run_game():
    wn = turtle.Screen()
    wn.title("Ping Pong Pour le Nuls")
    wn.bgcolor("green")
    wn.setup(width=800, height=600)
    wn.tracer(0)

    Player_1 = wn.textinput("Player 1", "Name of the player: ")
    Player_2 = wn.textinput("Player 2", "Name of the player: ")

    # Score
    score_a = 0
    score_b = 0
    # Paddle A
    paddle_a = turtle.Turtle()
    paddle_a.speed(0)
    paddle_a.shape("square")
    paddle_a.color("white")
    paddle_a.shapesize(stretch_wid=5, stretch_len=1)
    paddle_a.penup()
    paddle_a.goto(-350, 0)

    # Paddle B
    paddle_b = turtle.Turtle()
    paddle_b.speed(0)
    paddle_b.shape("square")
    paddle_b.color("white")
    paddle_b.shapesize(stretch_wid=5, stretch_len=1)
    paddle_b.penup()
    paddle_b.goto(350, 0)

    # Ball
    ball = turtle.Turtle()
    ball.speed(0)
    ball.shape("square")
    ball.color("white")
    ball.penup()
    ball.goto(0, 0)
    ball.dx = 0.4
    ball.dy = -0.4

    # Pen
    pen = turtle.Turtle()
    pen.speed(0)
    pen.color("white")
    pen.penup()
    pen.hideturtle()
    pen.goto(0, 260)
    pen.write(str(Player_1) + " : 0    " + str(Player_2) + " : 0    ", align="center", font=("Courier", 14, "normal"))


    # Funtion

    def paddle_a_up():
        y = paddle_a.ycor()
        y += 20
        paddle_a.sety(y)

    def paddle_a_down():
        y = paddle_a.ycor()
        y += -20
        paddle_a.sety(y)

    def paddle_b_up():
        y = paddle_b.ycor()
        y += 20
        paddle_b.sety(y)

    def paddle_b_down():
        y = paddle_b.ycor()
        y += -20
        paddle_b.sety(y)


    def pen_2():
        pen_2 = turtle.Turtle()
        pen_2.speed(0)
        pen_2.color("white")
        pen_2.penup()
        pen_2.hideturtle()
        pen_2.goto(0, 0)
        pen_2.write("Player 2 Wins", align="center", font=("Courier", 24, "normal"))

    # Keyboard binding

    wn.listen()
    wn.onkeypress(paddle_a_up, "w")
    wn.onkeypress(paddle_a_down, "x")
    wn.onkeypress(paddle_b_up, "Up")
    wn.onkeypress(paddle_b_down, "Down")

    # Main Game Loop
    while True:
        wn.update()

        # Move the ball
        ball.setx(ball.xcor() + ball.dx)
        ball.sety(ball.ycor() + ball.dy)


        # Border checking
        if ball.ycor() > 290:
            ball.sety(290)
            ball.dy *= -1

        if ball.ycor() < -290:
            ball.sety(-290)
            ball.dy *= -1

        if ball.xcor() > 390:
            ball.goto(0, 0)
            ball.dx *= -1
            score_a += 1
            pen.clear()
            pen.write(str(Player_1) + " : {}    ".format(score_a) + str(Player_2) + " : {}    ".format(score_b),
                      align="center", font=("Courier", 14, "normal"))


        if ball.xcor() < -390:
            ball.goto(0, 0)
            ball.dx *= -1
            score_b += 1
            pen.clear()
            pen.write(str(Player_1) + " : {}    ".format(score_a) + str(Player_2) + " : {}    ".format(score_b),
                      align="center", font=("Courier", 14, "normal"))



        # Paddle and ball collisions

        if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
            ball.setx(340)
            ball.dx *= -1
        if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
            ball.setx(-340)
            ball.dx *= -1

        # Paddles can't go beyond screen
        if paddle_a.ycor() >= 260:
            paddle_a_down()
        if paddle_a.ycor() <= -260:
            paddle_a_up()
        if paddle_b.ycor() >= 260:
            paddle_b_down()
        if paddle_b.ycor() <= -260:
            paddle_b_up()

        if score_a >= 3:
            restart = wn.textinput("Game result", "Well done " + Player_1 + ", you won ! \nDo you want to restart ? (y/n)")
            if restart == "y":
                wn.reset()
                run_game()
            else:
                break
        if score_b >= 3:
            restart = wn.textinput("Game result", "Well done " + Player_2 + ", you won ! \nDo you want to restart ? (y/n)")
            if restart == "y":
                wn.reset()
                run_game()
            else:
                break


run_game()

Thank you so much for your help and have a great day. Game Output


Solution

  • Here. Instead of using wn.reset(), I used wn.clearscreen(). It did the trick!!

    Code:

    import turtle
    
    def run_game():
    
        wn = turtle.Screen()
        wn.title("Ping Pong Pour le Nuls")
        wn.bgcolor("green")
        wn.setup(width=800, height=600)
        wn.tracer(0)
    
        Player_1 = wn.textinput("Player 1", "Name of the player: ")
        Player_2 = wn.textinput("Player 2", "Name of the player: ")
    
        # Score
        score_a = 0
        score_b = 0
        # Paddle A
        paddle_a = turtle.Turtle()
        paddle_a.speed(0)
        paddle_a.shape("square")
        paddle_a.color("white")
        paddle_a.shapesize(stretch_wid=5, stretch_len=1)
        paddle_a.penup()
        paddle_a.goto(-350, 0)
    
        # Paddle B
        paddle_b = turtle.Turtle()
        paddle_b.speed(0)
        paddle_b.shape("square")
        paddle_b.color("white")
        paddle_b.shapesize(stretch_wid=5, stretch_len=1)
        paddle_b.penup()
        paddle_b.goto(350, 0)
    
        # Ball
        ball = turtle.Turtle()
        ball.speed(0)
        ball.color("black")
        ball.shape("circle")
        ball.penup()
        ball.goto(0, 0)
        ball.dx = 0.4
        ball.dy = -0.4
    
        # Pen
        pen = turtle.Turtle()
        pen.speed(0)
        pen.color("white")
        pen.penup()
        pen.hideturtle()
        pen.goto(0, 260)
        pen.write(str(Player_1) + " : 0    " + str(Player_2) + " : 0    ", align="center", font=("Courier", 14, "normal"))
    
    
        # Funtion
    
        def paddle_a_up():
            y = paddle_a.ycor()
            y += 20
            paddle_a.sety(y)
    
        def paddle_a_down():
            y = paddle_a.ycor()
            y += -20
            paddle_a.sety(y)
    
        def paddle_b_up():
            y = paddle_b.ycor()
            y += 20
            paddle_b.sety(y)
    
        def paddle_b_down():
            y = paddle_b.ycor()
            y += -20
            paddle_b.sety(y)
    
    
        def pen_2():
            pen_2 = turtle.Turtle()
            pen_2.speed(0)
            pen_2.color("white")
            pen_2.penup()
            pen_2.hideturtle()
            pen_2.goto(0, 0)
            pen_2.write("Player 2 Wins", align="center", font=("Courier", 24, "normal"))
    
        # Keyboard binding
    
        wn.listen()
        wn.onkeypress(paddle_a_up, "w")
        wn.onkeypress(paddle_a_down, "x")
        wn.onkeypress(paddle_b_up, "Up")
        wn.onkeypress(paddle_b_down, "Down")
    
        # Main Game Loop
        while True:
            wn.update()
    
            # Move the ball
            ball.setx(ball.xcor() + ball.dx)
            ball.sety(ball.ycor() + ball.dy)
    
    
            # Border checking
            if ball.ycor() > 290:
                ball.sety(290)
                ball.dy *= -1
    
            if ball.ycor() < -290:
                ball.sety(-290)
                ball.dy *= -1
    
            if ball.xcor() > 390:
                ball.goto(0, 0)
                ball.dx *= -1
                score_a += 1
                pen.clear()
                pen.write(str(Player_1) + " : {}    ".format(score_a) + str(Player_2) + " : {}    ".format(score_b),
                          align="center", font=("Courier", 14, "normal"))
    
    
            if ball.xcor() < -390:
                ball.goto(0, 0)
                ball.dx *= -1
                score_b += 1
                pen.clear()
                pen.write(str(Player_1) + " : {}    ".format(score_a) + str(Player_2) + " : {}    ".format(score_b),
                          align="center", font=("Courier", 14, "normal"))
    
    
    
            # Paddle and ball collisions
    
            if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddle_b.ycor() + 40 and ball.ycor() > paddle_b.ycor() -40):
                ball.setx(340)
                ball.dx *= -1
            if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddle_a.ycor() + 40 and ball.ycor() > paddle_a.ycor() -40):
                ball.setx(-340)
                ball.dx *= -1
    
            # Paddles can't go beyond screen
            if paddle_a.ycor() >= 260:
                paddle_a_down()
            if paddle_a.ycor() <= -260:
                paddle_a_up()
            if paddle_b.ycor() >= 260:
                paddle_b_down()
            if paddle_b.ycor() <= -260:
                paddle_b_up()
    
            if score_a >= 3:
                restart = wn.textinput("Game result", "Well done " + Player_1 + ", you won ! \nDo you want to restart ? (y/n)")
                if restart == "y":
                    wn.clearscreen()
                    run_game()
                else:
                    break
            if score_b >= 3:
                restart = wn.textinput("Game result", "Well done " + Player_2 + ", you won ! \nDo you want to restart ? (y/n)")
                if restart == "y":
                    wn.clearscreen()
                    run_game()
                else:
                    break
    
    
    run_game()
    

    Hope this helps!