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