I'm programming a text based adventure game in Python, where the player moves on a 5x5 grid and picks up items, but I'm having trouble with changing the co-ordinates of the player. coorx and coory are not incrementing and decrementing within their respective functions.
coorx = 3 #The beginning x coordinate of the player
coory = 3 #The beginning y coordinate of the player
loop = True
#The dimensions of the map are 5x5.
# __ __ __ __ __
#| | | | | |
#|__|__|__|__|__|
#| | | | | |
#|__|__|__|__|__|
#| | |><| | |
#|__|__|__|__|__|
#| | | | | |
#|__|__|__|__|__|
#| | | | | |
#|__|__|__|__|__|
#>< = The player's starting position on the map
def left(coorx):
if coorx != 1: #This checks if the x co-ordinate is not less than 1 so the player does walk off the map.
coorx -= 1 #This function moves the player left by decrementing the x co-ordinate.
def right(coorx):
if coorx != 5: #This checks if the x co-ordinate is not more than 5 so the player does walk off the map.
coorx += 1 #This function moves the player right by incrementing the x co-ordinate.
def back(coory):
if coory != 1: #This checks if the y co-ordinate is not less than 1 so the player does walk off the map.
coory -= 1 #This function moves the player left by decrementing the y co-ordinate.
def forward(coory):
if coory != 5: #This checks if the y co-ordinate is not more than 5 so the player does walk off the map.
coory += 1 #This function moves the player right by incrementing the y co-ordinate.
while loop: #This loops as long as the variable "loop" is True, and since "loop" never changes, this is an infinite loop.
move = input().lower()
if move == "l":
left(coorx)
print("You move left.")
print(coorx, coory)
elif move == "r":
right(coorx)
print("You move right.")
print(coorx, coory)
elif move == "f":
forward(coory)
print("You move forward.")
print(coorx, coory)
elif move == "b":
back(coory)
print("You move backwards.")
print(coorx, coory)
This is what is output.
>f
>You move forward.
>3 3
>f
>You move forward.
>3 3
>l
>You move left.
>3 3
>l
>You move left.
>3 3
>b
>You move backwards.
>3 3
>b
>You move backwards.
>3 3
>r
>You move right.
>3 3
>r
>You move right.
>3 3
As you can see, the co-ordinates do not change from "3 3" throughout. Any assistance with my problem would be greatly appreciated.