make bouncing turtle with python
<p>i am a beginner with python
I wrote this code to make bouncing ball with turtle it works but have some erors like ball dissapering</p>

<pre><code>import turtle
turtle.shape("circle")
xdir = 1
x = 1
y = 1
ydir = 1
while True:
     x = x + 3 * xdir
     y = y + 3 * ydir
    turtle.goto(x , y)
    if x &gt;= turtle.window_width():
        xdir = -1
    if x &lt;= -turtle.window_width():
        xdir = 1
    if y &gt;= turtle.window_height():
        ydir = -1
    if y &lt;= -turtle.window_height():
        ydir = 1
    turtle.penup()
turtle.mainloop()
</code></pre>

<p>Although your approach to the problem works (my rework):</p>

<pre><code>import turtle

turtle.shape("circle")
turtle.penup()

x, y = 0, 0
xdir, ydir = 3, 3
xlimit, ylimit = turtle.window_width() / 2, turtle.window_height() / 2

while True:
    x = x + xdir
    y = y + ydir

    if not -xlimit &lt; x &lt; xlimit:
        xdir = -xdir
    if not -ylimit &lt; y &lt; ylimit:
        ydir = -ydir

    turtle.goto(x, y)

turtle.mainloop()
</code></pre>

<p>In the long run, it's the wrong approach to take.  In this case, due to the infinite loop <code>while True</code>, the <code>mainloop()</code> method is never called so no other turtle event handlers are active.  For example, if we wanted to use <code>exitonclick()</code> instead of <code>mainloop()</code>, it wouldn't work.  Instead consider:</p>

<pre><code>import turtle

turtle.shape("circle")
turtle.penup()

x, y = 0, 0
xdir, ydir = 3, 3
xlimit, ylimit = turtle.window_width() / 2, turtle.window_height() / 2

def move():
    global x, y, xdir, ydir

    x = x + xdir
    y = y + ydir

    if not -xlimit &lt; x &lt; xlimit:
        xdir = -xdir
    if not -ylimit &lt; y &lt; ylimit:
        ydir = -ydir

    turtle.goto(x, y)

    turtle.ontimer(move, 5)

turtle.ontimer(move, 5)

turtle.exitonclick()
</code></pre>

<p>Here we've turned control back over to the mainloop and the motion is on an event timer.  Other turtle events can execute so <code>exitonclick()</code> works.  Just something to think about going forward before you program yourself, and your turtle, into a corner.</p>

python turtle-graphics