How can I get a character that's already printed out on the terminal into a variable?

114 Views Asked by At

So I'm currently writing a roguelike game using only ascii characters, and I can't figure out how to check if there are walls in front of my characters (So ideally whatever method I use returns a '#'). My idea was to check after each move if the character is trying to move into a wall, but I can't find code that lets me check the specific character already printed out in the terminal at an (x, y) location.

Image

Any help would be appreciated! I'm currently using

sys.stdout.write("\x1b7\x1b[%d;%df%s\x1b8" % (x, y, text)) 

to print characters at specific locations.

1

There are 1 best solutions below

0
On

you need to store you "map" in a 2d array

consider

map = [[1,1,1,1,1,1,1],
       [1,0,0,0,0,0,1],
       [1,0,0,0,0,0,1],
       [1,1,1,1,1,1,1]]

in this map 1 would be a wall ... 0 is a walkable tile... now as long as you know the X,Y of the target where the character will move... you can tell if you are walking into a wall