Game engine source code
This is a complete dump of the game engine code.
from game_classes import *
## Locations
quad = Location('quad',
'You are standing in the Quad.\nFrom here you can go:\nNorth to the admin office.\n'
'East to the Cleaners office.\nSouth to the Canteen.\nWest to the Teachers office.\n',
['north', 'east', 'south', 'west'])
cleaner = Location('cleaner',
'You are standing in the cleaners room. There are shelves of chemicals.\nJoe is in here filling a bucket with water.\nFrom here you can go west to the quad.\n',
['west'])
canteen = Location('canteen',
'You are standing in the canteen. There is a vending machine here.\nFor $1 you can buy a choc-chip cookie, a picnic or chips.\nFrom here you can go north to the quad.\n',
['north'])
admin = Location('admin',
'You are standing in the Admin office.\nThere is a small safe on the shelves behind the desk.\nThere is a photo of Kurt Cobain on the wall with the letters RIP under it.\nFrom here you can go south to the quad.\n',
['south'],
'You are standing outside the Admin office but it is locked.\nFrom here you can go south to the quad.\n')
office = Location('office',
'You are standing in your teachers office. Surprisingly there is nothing on the desk except his computer.\nFrom here you can go east to the quad.\n',
['east'],
'You are standing outside your teachers office but it is locked.\nFrom here you can go east to the quad.\n')
## Items ##
coin = Item('coin',
'The coin is gold with $1 stamped on it.')
picnic = Item('picnic',
'The picnic bar has been in production since 1959.')
key = Item('key',
'The key is made of brass and has MK stamped on it.')
## Containers ##
safe = Complex('safe',
'The safe is open.',
'The safe is small but sturdy with a combination lock on the door.',
'5 4 94')
vending = Complex('vending machine',
'The vending machine has cookies, picnic bars and chips all for $1.',
'The vending machine has cookies and chips for $1.',
'coin')
book = Complex('book',
'Your teachers passcode is Trieste1960.',
'The book has Teacher passcodes written on the cover.',
'')
computer = Complex('computer',
'Your teacher left a message for you.\nIt says "Too late, I already phoned your parents!"\nGAME OVER.',
'The teachers computer is the only thing on the desk.\nThere is a login screen that says passcode:',
'Trieste1960')
## Actors ##
joe = Actor('Joe',
'Joe is a cleaner. He is dressed in overalls and carrying a mop.',
['I will lend you my key if you give me a picnic bar.',
'You will need to get me a picnic bar if you want to borrow my key.',
'Thank you for the picnic bar. Here is my key.'],
'picnic')
## Create a world and put the pieces in it. ##
world = World(3, 3)
world.add_location(0, 1, canteen)
world.add_location(1, 0, office)
world.add_location(1, 1, quad)
world.add_location(1, 2, cleaner)
world.add_location(2, 1, admin)
quad.items.append(coin)
safe.contents.append(book)
cleaner.items.append(joe)
admin.items.append(safe)
office.items.append(computer)
admin.open = False
office.open = False
# Add the player
player = Player(1, 0) # TODO Put this back to 1,1
def parse_input(input_str):
str_parts = input_str.split(' ')
if len(str_parts) == 1:
action = str_parts[0]
target = None
else:
action = str_parts[0]
target = str_parts[1]
return action, target
relocated = True # We are in a new location
player.items.append(key) #TODO remove this when done.
while True:
if relocated: # If we are in a new location ...
print(world.grid[player.row][player.col].get_desc()) # print the details
relocated = False # Now that we are here we don't need the details again until we move
user_input = input('What would you like to do? ')
action, target = parse_input(user_input)
match action:
case 'go':
if target in world.grid[player.row][player.col].exits:
match target:
case 'north':
player.do_move('north')
case 'south':
player.do_move('south')
case 'east':
player.do_move('east')
case 'west':
player.do_move('west')
relocated = True # We have moved to a new location.
else:
print('You cannot go that way.')
case 'take':
taken = False # Assume the item is not there or cannot be taken
for item in world.grid[player.row][player.col].items:
if item.name == target and item.collectable == True:
taken = True # The player can take that item
print(f'You take the {target}.')
world.grid[player.row][player.col].items.remove(item)
player.items.append(item)
if not taken:
print(f'You cannot take the {target}.')
case 'buy':
if world.grid[player.row][player.col].name == 'canteen': # If the player is in the canteen
if coin in player.items:
match target:
case 'picnic':
player.items.append(picnic)
player.items.remove(coin)
vending.open = False # This swaps the desc to the one without the picnic
print(f'You take the Picnic.')
case 'cookie' | 'chips':
player.items.append(picnic)
player.items.remove(coin)
vending.open = False
print(f'The machine gave you a Picnic instead. Must be faulty.')
case _: # This is a wildcard and will match all other inputs
print(f'There are no {target}\'s available.')
else:
print('You do not have any coins.')
else:
print('You cannot buy anything here.')
case 'inventory':
if len(player.items) == 0:
print('You are not carrying anything.')
else:
for item in player.items:
print(f'You have a {item.name}.')
case 'look':
if target == None:
print(world.grid[player.row][player.col].get_desc())
else:
described = False # use this to see if the player was given a description
for item in world.grid[player.row][player.col].items:
if item.name == target:
print(item.get_desc())
described = True
for item in player.items:
if item.name == target:
print(item.get_desc())
described = True
if not described:
print(f'The {target} is not here to look at.')
case 'talk':
if target == 'Joe' and world.grid[player.row][player.col].name == 'cleaner':
# The player can only talk to Joe in the cleaner's office.
print(joe.get_response())
else:
print(f'{target} is not here to talk to.')
case 'give':
if target == 'picnic' and world.grid[player.row][player.col].name == 'cleaner':
# The player can only give the picnic to Joe in the cleaner's office.
player.items.remove(picnic)
joe.satisfied = True
print(joe.get_response())
player.items.append(key)
print('You take the key.')
case 'use':
location = world.grid[player.row][player.col].name # Just to make the next bit easier.
if target == 'key' and key in player.items:
if location == 'admin':
admin.open = True
print(world.grid[player.row][player.col].get_desc())
elif location == 'office':
office.open = True
print(world.grid[player.row][player.col].get_desc())
else:
print('You cannot use the key here.')
else:
print('You cannot unlock anything without the key.')
case 'open':
match target:
case 'safe':
location = world.grid[player.row][player.col]
if location.name == 'admin': # have to be where the safe is.
combo = input('What is the combination (x x x)? ')
if combo == safe.trigger:
safe.open = True
print(safe.get_desc())
location.items.append(book)
else:
print('That is not the combination. The safe is still locked.')
case 'book':
location = world.grid[player.row][player.col]
if book in location.items or book in player.items:
book.open = True
print(book.get_desc())
case 'type':
location = world.grid[player.row][player.col]
if location.name == 'office':
if target == computer.trigger:
computer.open = True
print(computer.get_desc())
else:
print('Passcode incorrect.')
else:
print('There is nothing to type on here.')
Last modified: 07 April 2024