Search code examples
pythonturtle-graphicspython-turtle

Python : Using Turtle-graphics, having issue with Screen.tracer() and Screen.update(). Placement of’ update() ‘ makes my code run / not-run properly


I am trying to make a Pong game and as you can see in the screenshot ( at the end of this question ), In the right hand side I have created a paddle.

Everything works fine in following code, when I press Up and Down arrows, the paddle moves up and down.

Kindly note placement of statement “ screen.update()”

from turtle import Turtle,Screen
import time

screen = Screen()
screen.setup(width=800,height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)

paddle = Turtle()
paddle.shape("square")
paddle.color("white")
paddle.shapesize(stretch_wid=5,stretch_len=1)
paddle.penup()
paddle.goto(350,0)


def go_up():
    new_y = paddle.ycor() + 20
    paddle.goto(paddle.xcor(),new_y)

def go_down():
    new_y = paddle.ycor() - 20
    paddle.goto(paddle.xcor(),new_y)

screen.listen()
screen.onkey(go_up,"Up")
screen.onkey(go_down,"Down")


game_is_on = True

while game_is_on:
    screen.update()




screen.exitonclick()


But when I take out statement “ screen.update()” from while look , delete while loop and place the statement “ screen.update()” after paddle.goto(350,0) statement , the movement of paddle stops working , the changed code ( non-working code ) is as below:

(Kindly note placement of statement “ screen.update()” again below)

from turtle import Turtle,Screen
import time

screen = Screen()
screen.setup(width=800,height=600)
screen.bgcolor("black")
screen.title("Pong")
screen.tracer(0)

paddle = Turtle()
paddle.shape("square")
paddle.color("white")
paddle.shapesize(stretch_wid=5,stretch_len=1)
paddle.penup()
paddle.goto(350,0)
screen.update()


def go_up():
    new_y = paddle.ycor() + 20
    paddle.goto(paddle.xcor(),new_y)

def go_down():
    new_y = paddle.ycor() - 20
    paddle.goto(paddle.xcor(),new_y)

screen.listen()
screen.onkey(go_up,"Up")
screen.onkey(go_down,"Down")





screen.exitonclick()

I want to know :-

  1. Why is my code not working in SECOND CASE
  2. Why makes my code work in FIRST CASE

ThankYou.

Screenshot of the turtle canvas


Solution

  • Why is my code not working in SECOND CASE

    Because you haven't put a screen.update() everywhere you make a change that you want the user to see. Specifically the go_up() and go_down() methods:

    from turtle import Turtle, Screen
    
    def go_up():
        paddle.sety(paddle.ycor() + 20)
        screen.update()
    
    def go_down():
        paddle.sety(paddle.ycor() - 20)
        screen.update()
    
    screen = Screen()
    screen.setup(width=800, height=600)
    screen.bgcolor('black')
    screen.title("Pong")
    screen.tracer(0)
    
    paddle = Turtle()
    paddle.shape('square')
    paddle.color('white')
    paddle.shapesize(stretch_wid=5, stretch_len=1)
    paddle.penup()
    paddle.setx(350)
    
    screen.update()
    
    screen.onkey(go_up, 'Up')
    screen.onkey(go_down, 'Down')
    screen.listen()
    
    screen.exitonclick()
    

    Why makes my code work in FIRST CASE

    Because you call screen.update() continuously, i.e. way too often and so it happens to catch all the changes.