cursors, etc

Gerhard Häring gh_pythonlist at gmx.de
Sun Jun 2 23:09:43 EDT 2002


* BCBOOKER <bcbooker at aol.com> [2002-06-03 02:14 +0000]:
> Is there any way for a user to actively move a cursor or some other marker on a
> plane?  That is, if there is a 4x4 square with the cursor at (1,1), can the
> user press up, n, 1 or whatever and make the x increase?  I tried making
> something along the lines of this, but i had to go type in every situation -
> that is, make the map for (2, 3) or (1, 2).
> 
> I'm a newbie to programming as a whole, so please excuse any nievete that
> this message may exude...

You don't need to hard-code all possibilities. I'd suggest an approach like the
following Python pseudo-code:

def valid_position(coordinates):
    """Coordinates is a tuple of (x,y) coordinates.
    This function returns 1 if these are valid coordinates, 0 otherwise."""
    # Fill in code here ...

coordinates = (0, 0)
while 1:
    # Assume we have a function get_key() to get the a key from keyboard
    # and constants for the various cursor keys.
    x, y = coordinates
    key = get_key()
    if key == CURSOR_UP:
        y -= 1
    elif key == CURSOR_DOWN:
        y += 1
    elif key == CURSOR_LEFT:
        x -= 1
    elif key == CURSOR_RIGHT:
        x += 1
    else:
        # handle other keys here
    
    new_coordinates = (x, y)
    if valid_position(new_coordinates):
        coordinates = new_coordinates
    else:
        # Beep, display error message or something similar.


HTH,

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 10.8 °C      Wind: 2.1 m/s





More information about the Python-list mailing list