NoahSonic123 NoahSonic123 - 2 months ago 19
Python Question

Character Movement inside of a 5x5 Grid

So, i'm making a game, where your a little character, and you move around by typing directions. I have gotten all of that working, but now I want some visualization. I made two grids like this:

["╔═══════════════════╗",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"╚═══════════════════╝"]
["╔═══════════════════╗",
"║0,4|1,4|2,4|3,4|4,4║",
"║-------------------║",
"║0,3|1,3|2,3|3,3|4,3║",
"║-------------------║",
"║0,2|1,2|2,2|3,2|4,2║",
"║-------------------║",
"║0,1|1,1|2,1|3,1|4,1║",
"║-------------------║",
"║0,0|1,0|2,0|3,0|4,0║",
"╚═══════════════════╝"]


I have a little guy that looks like this: ツ. I have set up code that will represent a sort of 'coordinates' that are in the form of two ints that are in a tuple. Every time those two ints change, I want to character to appear on the corresponding square to the ints. The only way I can think of to do this is making 25 different grids with the guy on them, and the a bunch of if then statements. That's not too bad, but i'm also going to add another character, and I don't want to make 625 different grids. For example, if the tuple was (4, 3), i would want the character to appear there. it would need to update to other squares as he moves. I already have clearing statements. So, i'm open to any and all thoughts and ideas. Thanks in advance!

def basicIdea(playerCoordinates):
if playerCoordinates == (0, 0):
print("╔═══════════════════╗",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ | | | | ║",
"║-------------------║",
"║ツ | | | | ║",
"╚═══════════════════╝"]


I would just do something along those lines 25 times, but with two characters, there's roughly 625 possibilities, and I don't wan't to set up 625 different grids manually.

Answer Source

There's two ways to tackle this:

  1. Replacing the characters in the rendered grid. For this you save your grid either as a list of lines or all in one string with newline characters inside. That doesn't really matter as long as you have the grid saved in a variable somehow. From there you will have to calculate where your special character (e.g. smiley) needs to go. Keep in mind that this is about the location in the output, not the logical grid. For example, 10th row, 2nd column. If your grid is split in rows, it's row rows[9] and the character at position 1. You can split the string on that position, add the smiley in between, and save the concatenated string back. Then print. If your grid is all in one string, you go by 9 times the row length plus 2, e.g. Replace char at 9 * 22 + 2.

  2. The second way is probably more flexible and better extendible. It involves creating the visible grid from a logical grid/table. Example code:

.

# small 3x3 grid, implemented as a 2-dimensional list.
rows = [[" ", " ", " "],
        [" ", " ", " "],
        ["ツ", " ", " "]]    

output = ""
# draw first row
output += "╔"
output += "═══" * num_of_columns + "═" * (num_of_columns - 1)
output += "╗\n"

for row in rows:
    for cell in row:
        output += "║"
        if cell == " ":
            output += "   " # empty cell
        elif cell == "ツ":
            output += " ツ "
        output += "║\n"
    # TODO: draw lines between rows

# TODO: draw last line

print(output)