The Game Engine Pt 1
The game engine is the logic that manipulates the class objects. While the classes describe types of objects, the game engine will create and manipulate all the specific instances of the classes.
The current classes file runs to around 160 lines. The game engine file will probably run to around 300 lines but many of those will be setting up the class instances.
We need to start by creating a new Python file called game_engine.py and importing the game_classes.py module.
from game_classes import *
Start with the location objects.
Location objects
The quad:
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'])
The quad has no alternate description so none is provided in the initialisation. The coin will be added later.
The canteen:
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.',
['north'])
The cleaner:
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',
['west'])
The Admin office:
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')
The Teacher's office:
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.',
['east'],
'You are standing outside your teachers office but it is locked.\nFrom here you can go east to the quad.')
Item objects
These are all quite simple so here they all are together.
## 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.')
Complex objects
The safe:
safe = Complex('safe',
'The safe is open.',
'The safe is small but sturdy with a combination lock on the door.',
'5 4 94')
The vending machine:
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')
Once the player has bought the Picnic we can remove it from the machine. There is no more money in the game anyway.
The book:
book = Complex('book',
'Your teachers passcode is Trieste1960.',
'The book has Teacher passcodes written on the cover.',
'')
There is no trigger for the book since it cannot be "locked".
The computer:
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:
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 the world
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)
Set up the world
quad.items.append(coin)
safe.contents.append(book)
cleaner.items.append(joe)
admin.open = False
office.open = False
Add the player
player = Player(1, 1)
Start the game
The final task now that the world is created and populated is to give the player their first view of the world.
print(world.grid[player.row][player.col].get_desc()) # print the details
And we now get our first game view of the world we have created.
You are standing in the Quad.
From here you can go:
North to the admin office.
East to the Cleaners office.
South to the Canteen.
West to the Teachers office.
There is a coin here.
Last modified: 07 April 2024