Why does this code have the "ball" seem to slide along the edge
<p>My son asked me if I could write a small program to have a ball bounce around the screen then have me explain it.
Spotting a neat father-son opportunity I said "Yes!, no problem". So I dug out my python skills and wrote this..</p>

<pre><code>#!/usr/bin/python

#
# We have to tell python what stuff we're
# going to use. We do this by asking to import
# the code we'll be making use of
#
import curses
import random
import time

#
# We'll be using a screen handling
# package called "curses" so we need
# to initialise the screen
stdscr = curses.initscr()

# We need to make sure that
# keys we type will not show up
# on the screen spoiling the
# stuff we're drawing
curses.noecho()

# We need to tell curses that we don't
# want to wait when we ask for keys from
# the user, if there's non there then 
# we want curses to carry on
stdscr.nodelay( True )

# This ones a bit tricky to explain.
# Basically it puts the keyboard into
# a mode which allows curses to do
# things with it...
curses.cbreak()

# Ok, now we can do the fun stuff

# First thing we need to do is to
# find out how big our screen is
max_y, max_x = stdscr.getmaxyx()

stdscr.box()

min_x = 1
min_y = 1

max_y = max_y - 2
max_x = max_x - 2

stdscr.addstr( min_y, min_x, "1" )
stdscr.addstr( min_y, max_x, "2" )
stdscr.addstr( max_y, min_x, "3" )
stdscr.addstr( max_y, max_x, "4" )

dir_x = 1
dir_y = 1

# Then we can pick a random point on the
# screen in both the y (up-down) and x
# (left-right) directions
y = random.randint( 0, max_y )
x = random.randint( 0, max_x )

# Ok, this basically says, while trying to
# get a key from the user keeps coming back
# with an error, meaning there aren't any keys
# to return, do the code inside the loop
while( stdscr.getch() == curses.ERR ):
    # Ok, at the location we got, put a "0"
    stdscr.addstr( y, x, "O" )
    # stdscr.addstr( str( (y, x) ) )

    # this just moves the cursor to a new location so we can see the "O" better
    stdscr.move( 0, 0 )

    # have the screen update
    stdscr.refresh()

    # now choose a new location
    x = x + dir_x
    y = y + dir_y

    # have we gone too far
    if x &gt; max_x or x &lt; min_x:
        # change direction
        dir_x = -dir_x
        x = x + dir_x

    if y &gt; max_y or y &lt; min_y:
        # change direction
        dir_y = -dir_y
        y = y + dir_y

    # wait a bit so we can see the screen we've drawn.
    time.sleep( 0.05 )

# Ok, we're finished. Tidy up
curses.nocbreak()
stdscr.keypad( False )
curses.echo()
curses.endwin()
</code></pre>

<p>I ran the program and was quite pleased. However, when the ball hits the edge, it seems to "slide" and not bounce cleanly.</p>

<p>It's quite late in the evening so I couldn't get my head around it and I thought I'd throw it out there and see if anyone could explain why. 
I'm not after fixing the code, I'm pretty sure that with &lt;= or >= the tests would work. I just can't understand what it does what it does...</p>

<p>Thx
Mark.</p>
