Refactoring what we have
The existing test program is a bit awkward, especially for me as I have to remember to remove the actual username and passwords when pasting them into the blog.
So, first off create a new file called adventureconnection.py.
import mysql.connector
def make_connection():
# Create a database connection using the host, user name, password and database name
mydb = mysql.connector.connect(
host="<yourusername>.mysql.pythonanywhere-services.com",user="<yourusername>",
passwd="<mysqlpassword>",
database="<yourusername>$adventure"
)
return mydb
Now either modify the testroom.py file or create a new one. I have created a new one called testadventure.py.
Import the Room class, then import the contents of adventure connection. The make _connection function can then be called to make the database connection.
from room import Room
import adventureconnection
mydb = adventureconnection.make_connection()
mycursor = mydb.cursor()
aroom=Room(3,mycursor)
print(aroom.get_details())
Output:
Ballroom ====== A room whose floor is covered in brightly coloured balls. There is a grand door to the east None
First create your cursor for the database access.
Then set the player's initial location (room one)
Loop while the player's location is not 3
Get current room
Print room description
Get command
If command is a direction then
Import the Room class, then import the contents of adventure connection. The make _connection function can then be called to make the database connection.
from room import Room
import adventureconnection
mydb = adventureconnection.make_connection()
mycursor = mydb.cursor()
aroom=Room(3,mycursor)
print(aroom.get_details())
Output:
Movement
At this point there is only one player, so we can store the location in the program.Modification to the Room class
The Room class needs a method that returns the room id in the supplied direction. If there is no link in that direction, it just returns the current room id.
# Actions
def move(self,direction):
if direction in self.linked_rooms:
return self.linked_rooms[direction].get_destinationID()
else:
return self.roomID
Modification to the testadventure code
First create your cursor for the database access.
Then set the player's initial location (room one)
Loop while the player's location is not 3
Get current room
Print room description
Get command
If command is a direction then
Get room number in that direction
If the room number is unchanged then
You cannot go that way
else
Change the location
End loop
Print Exit message
You cannot go that way
else
Change the location
End loop
Print Exit message
from room import Room
import adventureconnection
mydb=adventureconnection.make_connection()
mycursor = mydb.cursor()
player_location=1
while player_location!=3:
current_room=Room(player_location,mycursor)
current_room.get_details()
command=input("> ")
if command in("north","south","east","west"):
print("Go "+command)
new_location=current_room.move(command)
if player_location==new_location:
print("You cannot go " +command)
else:
player_location=new_location
print("And you have left the building...")
Testing the code: