Search code examples
pythoncurses

Get string from window in curses


I am making a console game in python using curses BUT I can not find a function to get a string from the screen.

Python:

import curses

############
# Game Map #
############
gameMap = """
##########
#________#
#________#
##########
"""

gameWin = curses.initscr()
curses.cbreak()
curses.curs_set(0)
gameWin.keypad(1)

gameWin.clear()
gameWin.addstr(gameMap)
gameWin.addstr(playerY, playerX, playerIcon)
game_over = False
while game_over == False:
    c = gameWin.getch()

    if c == curses.KEY_RIGHT ## and gameWin. some function (playerY,playerX+1) != "#":
                                       #### this line ^^^^
        playerX += 1
    gameWin.clear()
    gameWin.addstr(gameMap)
    gameWin.addstr(playerY, playerX, playerIcon)
curses.nocbreak(); gameWin.keypad(0); curses.echo()
curses.endwin()

As you can see I want to get the string at playerY, playerX in gameWin I have tried getstr


Solution

  • You should be able to do this with instr().

    Personally, I suggest using the screen only as output and not as storage, so you'd keep an internal representation of the game board that was more suited to the logic of the game itself. But that's up to you.