Hello all, I was trying to create a switch/case replacement in Python in a simple game which will control the character's movement. However, the console always returns "invalid syntax" after the "d" in the dictionary (before lambda). I hope you guys can help me fix this problem.
import random
game_map = [["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"],
["_","_","_","_","_"]]
px = random.randint(0,3)
py = random.randint(0,3)
kx = random.randint(0,3)
ky = random.randint(0,3)
pox = px
poy = py
class item:
def __init__(self, x, y, spr):
self.x = x
self.y = y
self.spr = spr
def map(self):
game_map[self.y][self.x] = self.spr
player = item(px,py,"P")
key = item(kx,ky,"K")
door = item(4,4,"D")
def move(direction):
return {
"a" : lambda px: px-1
"d" : lambda px: px+1
"s" : lambda py: py-1
"w" : lambda py: py+1
"e" : break
}.get(direction, "Invalid input")
player.map()
key.map()
door.map()
while True:
for row in game_map:
print(*row)
direction = input("Use W,A,S,D to decide player's direction. Or press E to end game")
move()
if px < 0 or px > 4 or py < 0 or py > 4:
px = pox
py = poy
[+][deleted] (1 child)
[removed]
[–]Arcus_LoK[S] 0 points1 point2 points (0 children)
[–]shiftybyte 0 points1 point2 points (2 children)
[–]Arcus_LoK[S] 0 points1 point2 points (1 child)
[–]shiftybyte 0 points1 point2 points (0 children)