1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is there an accepted architecture for these classes?

Discussion in 'Programming/Internet' started by Cristian Desivo, Oct 8, 2018.

  1. I am coding a game in python and so far so good, but I feel like my classes are not very modular. This is a simplification of what I've done:

    class Game:
    def __init__(self):
    self.characters = []
    self.walls = []
    self.background = None
    self.canvas = None
    self.keyInputs = None

    def update(self, inputs):
    self.keyInputs = inputs
    for character in self.characters:
    if character.died:
    self.characters.remove(character)
    else:
    character.update()

    def draw(self):
    self.background.draw()
    for wall in self.walls:
    wall.draw()
    for character in self.characters:
    character.draw()

    class Character:
    def __init__(self):
    self.horizontalPosition = 0
    self.verticalPosition = 0
    self.game = None
    self.died = False
    self.sprite = None
    ...

    def draw(self):
    self.game.canvas.draw(self.sprite, self.horizontalPosition, self.verticalPosition)

    def interactWith(self, character):
    ...

    def detectCollition(self, wall):
    ...

    def handleInputs(self):
    inputs = self.game.keyInputs
    ...

    def update(self):
    self.handleInputs()
    for character in self.game.characters:
    if character != self:
    self.interactWith(character)
    for wall in self.game.walls:
    self.detectCollition(wall)


    This way Characters and Walls need to have a variable for which Game they are in, and a Game needs to have a variable for each Character or Wall that is in it.

    Is there a better way to define these classes? Are there design patterns for this?

    Login To add answer/comment
     

Share This Page