1

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
  • Please provide the expected MRE. Show where the intermediate results deviate from the ones you expect. We should be able to paste a single block of your code into file, run it, and reproduce your problem. You've displayed no output, no, tracing, and virtually all of your posted code deals with unrelated game display, rather than positioning. Commented Oct 4, 2020 at 3:07
  • 1
    If the @ was in map1 when moving down, you move it to map2. You then check if the @ is in map2, and find it again - moving it to map3 this time. You either need to perform the two checks in the downward case in the opposite order, or make the second if an elif instead. Commented Oct 4, 2020 at 3:14
  • Ah, making the second if an elif worked perfectly, thank you so much! I appreciate it! Commented Oct 4, 2020 at 3:24

3 Answers 3

2

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.

answered Oct 4, 2020 at 3:23
Sign up to request clarification or add additional context in comments.

1 Comment

Changing it to 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 you
0

Change 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
...
answered Oct 4, 2020 at 3:20

1 Comment

Thank you! Changing to elif worked perfectly, now it's finally working as intended, thank you so much
0

In 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.

answered Oct 4, 2020 at 3:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.