msvcrt function does not seem to work as intended in game

  ascii, game-engine, msvcrt, python, windows

From my research msvcrt function should allow you to input a single command without the ‘enter’ being pressed. How come it is not working in my game ? For example if I click ‘w’ the character should in theory move 1 space north without me clicking enter. The games work fine with input() but you will need to click enter after every move

import os
import msvcrt


#map
dungeonMap = [["0","0","0","0","0","0","0","0","0"],
              ["0",".",".","0",".",".",".",".","0"],
              ["0",".",".",".",".",".",".","0","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".","0",".",".",".","0"],
              ["0",".","0","0",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0","0","0","0","0","0","0","0","0"]]

playerMap  = [["0","0","0","0","0","0","0","0","0"],
              ["0",".",".","0",".",".",".",".","0"],
              ["0",".",".",".",".",".",".","0","0"],
              ["0","S",".",".",".",".",".",".","0"],
              ["0",".",".",".","0",".",".",".","0"],
              ["0",".","0","0",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0",".",".",".",".",".",".",".","0"],
              ["0","0","0","0","0","0","0","0","0"]]


x = 1
y = 3

def displayMapAround(maps,x,y):
  for dy in (-1,0,1):
    print( maps[y+dy][x-1:x+2] )



#Displaying the map
def displayMap(maps):
    for y in range(0,9):
        print(maps[y])

#selecting a map
mapChoice = dungeonMap

displayMapAround(playerMap,x,y)

#initialising the players position
position = mapChoice[0][0]



print(mapChoice[y][x])
while position != "E":
    os.system('cls' if os.name == 'nt' else 'clear')
    displayMapAround(playerMap,x,y)
    displayMap(playerMap)
    previousX = x
    previousY = y
    playerMap[y][x] = "."
    print("W,S,D,A,MAP")
    movement = msvcrt.getch()
    movement=movement.upper()

    if movement == "W":
        y = y-1
        position = mapChoice[y][x]
        playerMap[y][x] = "S"
        

    if movement == "S":
        y = y+1
        position = mapChoice[y][x]
        playerMap[y][x] = "S"
        

    if movement == "D":
        x = x+1
        position = mapChoice[y][x]
        playerMap[y][x] = "S"
        

    if movement == "A":
        x = x-1
        position = mapChoice[y][x]
        playerMap[y][x] = "S"


    position = mapChoice[y][x]
    playerMap[y][x] = "S"
    
    if position == "0" or position == "1":
        print("You hit a wall, you stumble in the darkness back to your previous position...")
        playerMap[y][x] = "0"
        x = previousX
        y = previousY
        playerMap[y][x] = "S"``

Source: Windows Questions

LEAVE A COMMENT