I started learning Python not long ago and I decided to create an old school ascii game in (almost) pure Python, here's the code:
import keyboard
top=['╔','═','═','═','═','═','═','═','═','╗']
map1=['║','.','.','.','.','.','.','.','.','║']
map2=['║','@','.','.','.','.','.','.','.','║']
map3=['║','.','.','.','.','.','.','.','.','║']
bot=['╚','═','═','═','═','═','═','═','═','╝']
place=1
was_pressed = False
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
while True:
try:
if keyboard.is_pressed('D') and place<=7:
if not was_pressed:
if '@' in map1:
map1[place], map1[place+1]=map1[place+1],map1[place]
place=place+1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed=True
if '@' in map2:
map2[place], map2[place+1]=map2[place+1],map2[place]
place=place+1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed=True
if '@' in map3:
map3[place], map3[place+1]=map3[place+1],map3[place]
place=place+1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed=True
elif keyboard.is_pressed('A') and place >=2:
if not was_pressed:
if '@' in map1:
map1[place], map1[place - 1] = map1[place - 1], map1[place]
place = place - 1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
if '@' in map2:
map2[place], map2[place -1] = map2[place - 1], map2[place]
place = place - 1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
if '@' in map3:
map3[place], map3[place -1] = map3[place - 1], map3[place]
place = place - 1
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
elif keyboard.is_pressed('W'):
if not was_pressed:
if '@' in map2:
map2[place], map1[place]=map1[place],map2[place]
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed=True
if '@' in map3:
map3[place], map2[place] = map2[place], map3[place]
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
elif keyboard.is_pressed('S'):
if not was_pressed:
if '@' in map1:
map1[place], map2[place] = map2[place], map1[place]
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
if '@' in map2:
map2[place], map3[place] = map3[place], map2[place]
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
else:
was_pressed=False
except:
break
TLDR; the code is basically a map where the player (@) can walk around with WASD. Now, my problem is that whenever I press S so the player can go downwards, it goes down twice instead of once, so it goes from the top of the map to the bottom with just one press of the S key, this doesn't happen with the other keys, so I have no idea what the problem could be.
--Edit-- An example of this would be:
╔════════╗
║@.......║
║........║
║........║
╚════════╝
I would press S and the output would be this:
╔════════╗
║........║
║@.......║
║........║
╚════════╝
But instead the output is this:
╔════════╗
║........║
║@.......║
║........║
╚════════╝
╔════════╗
║........║
║........║
║@.......║
╚════════╝
I'm unsure how else I could explain it because I'm not sure how simpler I could make the code, but I'll try
3 Answers 3
The problem comes from not testing small parts of your code, and failing to isolate your logic branches. Look at the logic trail for a simple move:
if '@' in map1:
# Switch rows 1 & 2
# Print the board
was_pressed = True
if '@' in map2:
# Switch rows 2 & 3
# Print the board
was_pressed = True
The problem is that, even after you've moved from row 1 to row 2, you immediately check again to see whether you're in row 2 ... which you are, if you started on row 1. Since your checks move from the top row down, you will propagate any downward movement, X from wherever the player started, straight to the bottom row.
For a simple fix, change the lower checks to elif. FOr a general fix, learn to keep track of your position, and simply insert the @ into the proper place in your game board.
1 Comment
elif definitely worked, thank you so much, and yes if I can I'll definitely try to find a way to keep track of my @ with a simple coordinate system, thank youChange the last if statement in the file to an elif. Currently the check for the keypressed(S) is running twice. So from:
...
elif keyboard.is_pressed('S'):
if not was_pressed:
if '@' in map1:
map1[place], map2[place] = map2[place], map1[place]
print(f"Moved {map1[place]}, {map2[place]} to {map2[place]} {map1[place]}")
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
if '@' in map2: # Bad because it runs twice
map2[place], map3[place] = map3[place], map2[place]
print(f"Moved {map2[place]}, {map3[place]} to {map3[place]} {map2[place]}")
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
...
to
...
elif keyboard.is_pressed('S'):
if not was_pressed:
if '@' in map1:
map1[place], map2[place] = map2[place], map1[place]
print(f"Moved {map1[place]}, {map2[place]} to {map2[place]} {map1[place]}")
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
elif '@' in map2: # Now only runs once
map2[place], map3[place] = map3[place], map2[place]
print(f"Moved {map2[place]}, {map3[place]} to {map3[place]} {map2[place]}")
print(*top, sep='')
print(*map1, sep='')
print(*map2, sep='')
print(*map3, sep='')
print(*bot, sep='')
was_pressed = True
...
1 Comment
elif worked perfectly, now it's finally working as intended, thank you so muchIn between if not was_pressed:, the code will run twice because once the character moves down, it checks if it is in the spot where it moved to. You need to change the second if statement to elif, so it won't check when it's not supposed to. I made something similar before, and this has worked for me.
map1when moving down, you move it tomap2. You then check if the @ is inmap2, and find it again - moving it tomap3this time. You either need to perform the two checks in the downward case in the opposite order, or make the secondifanelifinstead.ifanelifworked perfectly, thank you so much! I appreciate it!