Andrew Hansen Help

The World class

The object classes will all be kept in a single file called game_classes.py. Each class will be added to the bottom of the file and tested / demonstrated in the "if name == 'main':" block.

The World class is actually very simple and only creates a 2D list into which the location objects will be placed.

The class diagram is:

World class

  • grid

  • add_location()

And the Python code is:

class World: ''' Class to define the World within the game. This class is exceedingly simple having only one attribute: grid, and one method: add_location() which is only being used for simplicity. Attributes: grid: list Methods: add_location(): ''' def __init__(self, rows, cols): self.grid = [] # create an empty grid for r in range(rows): # for each row ... row = [] # create an empty list for c in range(cols): # for each column row.append([]) # add an empty list to row self.grid.append(row) # Stick the whole row in the grid def add_location(self, row, col, location): self.grid[row][col] = location

We are not going to test this as it is simple and straight forward.

Last modified: 07 April 2024