OO for my (main) Pacman game

I am trying to create a basic Pacman game in C ++ (I will use the Java syntax in this question, as it is somewhat easier to demonstrate), but I can not find a good design option.

So far I have 4 classes:
- Monster: can be subclassed for monster-specific behavior and contains all the logic for monsters
- Player: Contains the player’s logic
- Map: contains a 2d array representing the map. This array determines which positions are walls or products. Pacman
- Game: contains a player, a map and a list of monsters.

To make it simple:

public class Game {
  Player player;
  Map map;
  ArrayList<Monster> monsters;

  public Game() {
    player = new Player();
    map = new Map();
    monsters = new ArrayList<Monster();
    monsters.add(new ScaryMonster());
    monsters.add(new DumpMonster());
  }

  public void update() {
    player.update();
    map.update();

    for (Monster monster: monsters) {
      monster.update();
    }

  public void draw() {
    map.draw();
    player.draw();

    for (Monster monster: monsters) {
      monster.draw();
  }
}

So now I need to create a Game object and call update () and draw () each time. Very simple. But that does not work.

, update() , ( Pacman of the course) . ( ), 2d-. , , ( ). .

map monster update() draw() . . , , OO.

? Observer ( Game - , , - ), : , , , .

.

:)

+3
1

?

. . , " ", , , ( , , , ), .

, , ... ? , ( BeDeath: P), ... , .

, , , , .

Edit: ( , , )

public void IGameInfo
{
  List<Monster> Monsters {get;}
  Pacman Pacman {get;}
  Map Map {get;}
}

public void ComputeReactions()
{
  foreach (actionChecker in Actions)
  {
    actionChecker.Check(gameInfo);
  }
}

public void ComputeDotEaten(IGameInfo gameInfo)
{
  foreach (dot in gameInfo.Map.Dots)
    if (pacman.location == dot.location)
      dot.MarkEaten();
}

public void ComputeMonsterEaten(IGameInfo gameInfo)
{
  foreach (Monster in gameInfo.monsters)
    if (gameInfo.pacman.location == gameInfo.monster.location &&
        gameInfo.pacman.Invulnerable)
      monster.MarkDeath();
    else
      Game.EndGame();
}

, ,

public void ComputeDotEaten(IGameInfo gameInfo)
{
  foreach (dot in gameInfo.Map.Dots)
    if (pacman.location == dot.location)
      Reactions["DotEaten"].Execute(dot);
}

, , , (.. , )

+4

Source: https://habr.com/ru/post/1793420/


All Articles