1

So I have start working with Curses in Python. I have got this source code to start with and slowly I will make some updates to it:

    #!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Testing out the curses lib.
"""

import curses


def main(scr):
    """
    Draw a border around the screen, move around using the cursor and leave a mark
    of the latest pressed character on the keyboard.

    Perhaps you could make a nice painting using asciart?

    Quit using 'q'.
    """

    # Clear the screen of any output
    scr.clear()

    # Get screen dimensions
    y1, x1 = scr.getmaxyx()
    y1 -= 1
    x1 -= 1

    y0, x0 = 0, 0

    # Get center position
    yc, xc = (y1-y0)//2, (x1-x0)//2

    # Draw a border
    scr.border()

    # Move cursor to center
    scr.move(yc, xc)

    # Refresh to draw out
    scr.refresh()

    # Main loop
    x = xc
    y = yc
    ch = 'o'

    while True:
        key = scr.getkey()
        if key == 'q':
            break
        elif key == 'KEY_UP':
            y -= 1
        elif key == 'KEY_DOWN':
            y += 1
        elif key == 'KEY_LEFT':
            x -= 1
        elif key == 'KEY_RIGHT':
            x += 1
        else:
            ch = key

        # Draw out the char at cursor position
        scr.addstr(ch)

        # Move cursor to new position
        scr.move(y, x)

        # Redraw all items on the screen
        scr.refresh()



if __name__ == "__main__":
    print(__doc__)
    print(main.__doc__)
    input("Press enter to begin playing...")
    curses.wrapper(main)

The thing I want to do now is to make sure that when I can't hit the border of the screen. But I'm not sure what function in the this I can use for that. I have read in the python docs but can't find anything I think will work.

1 Answer 1

2

You know the valid range. From 0 to to y1 inclusive. (0 to x1 respectively). So just add tests to ensure the coordinates stay within the range:

    elif key == 'KEY_UP':
      if y > 0:
        y -= 1
    elif key == 'KEY_DOWN':
      if y < y1:
        y += 1

and similar for the x.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.