New to python, so the question might not mean what I think it means, apologies if that is the case.
I wrote an adventure game and ran it in a GUI, I have gotten a lot of feedback (including on a deleted question here) that I was using too many global variables and my code could be better organised using classes. So, reading followed, now I am attempting to do what I have read.
I have a class:
class room:
def __init__(self, x, y):
self.x = x
self.y = y
self.location = (self.x, self.y)
#this displays in a GUI window
def prompt(self):
write('Another, identical, wood-panelled, four-doored room. What now?')
#this updates a dictionary which maintains current location
def where(self):
locale['place'] = self.location
room1 = room(0, 0)
room2 = room(0, 1)
room3 = room(0, 2)
room4 = room(1, 0)
room5 = room(1, 1)
room6 = room(1, 2)
Now I am using a roomfinder function, to produce prompts and update location based on user input in a GUI. Exerpt:
def roomfinder():
#if and nested if statements for every room location
elif locale['place'] == room3.location:
if user_input.lower() == 'n':
messagebox.showinfo(title='Game Over', message='You fall to your doom. There was no room here! \n \n')
clear_console()
startroom()
elif user_input.lower() == 's':
clear_console()
room6.where()
room6.prompt()
elif user_input.lower() == 'e':
messagebox.showinfo(title='Game Over', message='You fall to your doom. There was no room here! \n \n')
clear_console()
startroom()
elif user_input.lower() == 'w':
clear_console()
room2.where()
room2.prompt()
else:
write('Please enter a valid response')
Now with nested elif statements for 6 rooms, and a similar function for performing a search in each location, this is big and untidy (feedback agreed with my feelings on this). I am looking for some code that will do something like (obv this is not real code)
if user_input.lower() == 'n'
locale['place'] = ( current +1, current)
roomX.prompt where roomX = room where location is locale['place']
does such a syntax exist? Is this possible? Does my question make sense?