H
Hans Kamp
I am programming an easy game (not with a commercial purpose, just with
learning purpose), but I doubt whether the following way of programming is
proper or advisable.
The main form (BugEaterForm) that has a module, has the following objects:
- Snake, for drawing and being controlled;
- Player, with player information, such as name, score, number of lives;
- PlayField, containing the matrix on which you are playing;
- BugEaterForm.
But I think I don't have a neat way of realizing the communication with each
other.
For example, if the snake must be moved, or it eats a candy or it is dying.
The trick is passing the classes as a parameter in the constructor of the
other class.
For example:
currentplayer = new Player(gnf, playPanel, timeleftPanel, playField,
timerSnake, timerBonus, "Hans Kamp", scoreLabel, livesLabel);
all this stuff must be passed, because the player and objects that are
dependent of the player must have the ability to do something with the
controls and other variables in the above constructor.
The constructor Player is defined as:
public Player(GetNameForm gnf, Panel pp, Panel tlp, PlayField pf, Timer s,
Timer b, string playerName, Label sl, Label ll)
{
...
playerSnake = new Snake(pp, pf, this, s, b);
...
}
The constructor Snake has 5 parameters. It is defined as:
public Snake(Panel playP, PlayField pf, Player p, Timer s, Timer b)
{
...
playfield.setCell(panel, snakeHead.X, snakeHead.Y, Field.SnakeHead);
}
And setCell needs access to the object PlayField (by playfield) so that the
snake can be drawn initially. But the snake has to do several things, for
example:
- repositioning. It needs access to PlayField;
- completing a level. It needs access to timers on the main Form (stopping
and starting a timer), access to a player (adding bonus to the score,
incrementing the live and level with one, and setting the bonus points to
4000), access to the playfield (redrawing the field);
- removing the snake. It needs access to playField;
- dying. It needs access to the timers, to the player, to playField;
- moving. It needs access to the playField;
- processing keys (being controlled by the user).
The player has the following tasks:
- drawing the time left bar. It needs access to the timeLeftPanel;
- displaying the number of lives. It needs access to the livesLabel of the
form;
- displaying the score. It needs access to the scoreLabel of the form.
The playField has the following tasks:
- drawing a cell (it needs access to the playPanel);
- setting a cell;
- getting a cell;
- redrawing a new level play field (it needs access to the playPanel);
- drawing walls (access to the panel);
- drawing candies (access to the pane).
For the whole source code (related to the above text), see
www.hanskamp.com/csharp/WinForm.cs
What do you think? What is the best way of organizing the communication
between the objects?
Hans Kamp.
learning purpose), but I doubt whether the following way of programming is
proper or advisable.
The main form (BugEaterForm) that has a module, has the following objects:
- Snake, for drawing and being controlled;
- Player, with player information, such as name, score, number of lives;
- PlayField, containing the matrix on which you are playing;
- BugEaterForm.
But I think I don't have a neat way of realizing the communication with each
other.
For example, if the snake must be moved, or it eats a candy or it is dying.
The trick is passing the classes as a parameter in the constructor of the
other class.
For example:
currentplayer = new Player(gnf, playPanel, timeleftPanel, playField,
timerSnake, timerBonus, "Hans Kamp", scoreLabel, livesLabel);
all this stuff must be passed, because the player and objects that are
dependent of the player must have the ability to do something with the
controls and other variables in the above constructor.
The constructor Player is defined as:
public Player(GetNameForm gnf, Panel pp, Panel tlp, PlayField pf, Timer s,
Timer b, string playerName, Label sl, Label ll)
{
...
playerSnake = new Snake(pp, pf, this, s, b);
...
}
The constructor Snake has 5 parameters. It is defined as:
public Snake(Panel playP, PlayField pf, Player p, Timer s, Timer b)
{
...
playfield.setCell(panel, snakeHead.X, snakeHead.Y, Field.SnakeHead);
}
And setCell needs access to the object PlayField (by playfield) so that the
snake can be drawn initially. But the snake has to do several things, for
example:
- repositioning. It needs access to PlayField;
- completing a level. It needs access to timers on the main Form (stopping
and starting a timer), access to a player (adding bonus to the score,
incrementing the live and level with one, and setting the bonus points to
4000), access to the playfield (redrawing the field);
- removing the snake. It needs access to playField;
- dying. It needs access to the timers, to the player, to playField;
- moving. It needs access to the playField;
- processing keys (being controlled by the user).
The player has the following tasks:
- drawing the time left bar. It needs access to the timeLeftPanel;
- displaying the number of lives. It needs access to the livesLabel of the
form;
- displaying the score. It needs access to the scoreLabel of the form.
The playField has the following tasks:
- drawing a cell (it needs access to the playPanel);
- setting a cell;
- getting a cell;
- redrawing a new level play field (it needs access to the playPanel);
- drawing walls (access to the panel);
- drawing candies (access to the pane).
For the whole source code (related to the above text), see
www.hanskamp.com/csharp/WinForm.cs
What do you think? What is the best way of organizing the communication
between the objects?
Hans Kamp.