Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Weird unexpected action in my first project (Python)

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

Answer*

Draft saved
Draft discarded
Cancel

lang-py

AltStyle によって変換されたページ (->オリジナル) /