This question already has an answer here:
if dirInput == ("up" or "Up" or "UP" or "u"): #up
moveX = 0
moveY = -1
I have this for all 4 directions. typing "up" works fine and moves the player up. same for all 4 directions. But, when I use the alternative ones like "Up" or "UP" or "u" they are unreliable. They'll either sometimes not work but othertimes work or down instead of up or whatever,.idk what's happening because it's a basic IF statement with basic ORs. Anyway, here's the entire code to look at if you need more context:
import os #o used for clear screen
width = input("Please enter grid width:") #grid width (string?)
width = int(width) #make integer
height = input("Please enter grid height:") #grid height (string?)
height = int(height) #make integer
grid = [["o" for x in range(width)] for y in range(height)] #initialize 2D grid matrix
playerX = int(round((width-1.25)/2)) #player X coordinate (center)
playerY = int(round((height-1.25)/2)) #player Y coordinate (center)
moveX = 0 #movement
moveY = 0 #movement
icon = ["o", "x"] #icons: grid(0), player(1)
def initializeGrid(): #create grid function
for y in range(height): #for each row
for x in range(width): #for each point
grid[x][y] = "o" #assign "o" for each point
def drawGrid(): #draw grid function
os.system('cls') #clear screen
drawObjects() #draw objects and players
gridRow = ["" for x in range(width)] #initialize 1D grid row matrix
for y in range(height): #for each row
for x in range(width): #for each point
gridRow[y] += (str(grid[x][y]) + " ") #add all points in a row to grid row
print(gridRow[y]) #print each row, for each collumn
def drawObjects(): #draw objects and players
grid[playerX][playerY] = icon[1] #draw player icon
def move(): #movement
global playerX #import playerX to function
global playerY #import playerY to function
grid[playerX][playerY] = "o" #remove player "x"
playerX += moveX #update player X coordinate
playerY += moveY #update player Y coordinate
#don't go off edges
if playerX < 0:
playerX = 0
if playerX >= width:
playerX = width-1
if playerY < 0:
playerY = 0
if playerY >= height:
playerY = height-1
drawGrid()
step()
def step(): #loop through steps
global moveX #import moveX
global moveY #import moveY
dirInput = "up"
dirInput = input("Please type a direction to move:") #ask for direction input
if dirInput == ("up" or "Up" or "UP" or "u"): #up
moveX = 0
moveY = -1
elif dirInput == ("down" or "Down" or "DOWN" or "d"): #down
moveX = 0
moveY = 1
elif dirInput == ("left" or "Left" or "LEFT" or "l"): #left
moveX = -1
moveY = 0
elif dirInput == ("right" or "Right" or "RIGHT" or "r"): #right
moveX = 1
moveY = 0
else:
print("Incorrect directional input.")
print("Please type: 'up', 'down', 'left', or 'right'.")
move() #movement decided, now move
initializeGrid() #initialize the grid
drawGrid() #draw the grid
step() #repeat movement and grid drawing`
Aucun commentaire:
Enregistrer un commentaire