GameControl // winform project main form
Button btnPlay;
Button btnGetScore;
TextBox tbxScoreReport;
CheckBox checkBox1;
CheckBox checkBox2;
1. flow-of control: app launch
a. the 'GameControl instance is loaded as the app starts
a.1. an instance of 'GamePlay, 'Game, is created.
a.2 an event handler for the FormClosing Enent of 'Game is installed
a.2.a this tests whether the Game is closing because the application is closing or whether it's closing because the user closed this form: if the app is closing then nothing is done; if not, then if there is any game data, a report of game stats is written to the TextBox in the main form.
b. when the Play button is clicked
b.1 the ConfigureGame method of 'Game is called to set initial game parameters
b.2 the Play button is disabled
b.3 the Game form is shown
2. flow-of control: Game form shown
a. a new instance of ScoreData is created, 'Data
b. the Shown event handler sets the game start-time
c. a KeyDown handler is installed that will close if alt and q keys are both held down
c.1 when the Game form closes this will trigger the FormClosing Event defined in the GameControl instance
d. the btnAddPoints and btnAddPoints buttons increment and decrement the score
e. the GetGameStatus method is only called by the GameControl instance
code example:
public partial class GameControl : Form
{
private static int gameCounter = 1;
private GamePlay Game;
public GameControl()
{
InitializeComponent();
}
private void GameProtoType_Load(object sender, EventArgs e)
{
Game = new GamePlay();
Game.Owner = this;
Game.FormClosing += GameOnFormClosing;
}
private void GameOnFormClosing(object sender, FormClosingEventArgs e)
{
if (Game.Data != null && e.CloseReason != CloseReason.FormOwnerClosing)
{
Game.Data.GameEnd = DateTime.Now;
tbxScoreReport.Text = Game.GetGameStatus();
btnPlay.BackColor = SystemColors.Control;
btnPlay.Enabled = true;
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
tbxScoreReport.Clear();
Game.ConfigureGame(gameCounter++, "The Player", checkBox1.CheckState, checkBox2.CheckState);
btnPlay.BackColor = Color.Firebrick;
btnPlay.Enabled = false;
Game.Show();
}
private void btnGetScore_Click(object sender, EventArgs e)
{
if (Game.Data == null) return;
tbxScoreReport.Text = Game.GetGameStatus();
}
}
GamePlay // winform owned by 'GameControl
TextBox tbxCurrentScore;
Button btnAddPoints;
Button btnSubPoints;
public partial class GamePlay : Form
{
public GamePlay()
{
InitializeComponent();
}
public ScoreData Data { set; get; }
public void ConfigureGame(int id, string plname = "", CheckState chk1 = CheckState.Unchecked,
CheckState chk2 = CheckState.Unchecked)
{
Data = new ScoreData(id, plname);
if (chk1 == CheckState.Checked)
{
}
if (chk2 == CheckState.Checked)
{
}
}
private void GamePlay_Shown(object sender, EventArgs e)
{
Data.GameStart = DateTime.Now;
}
private void btnAddPoints_Click(object sender, EventArgs e)
{
Data.ChangeScore(100);
tbxCurrentScore.Text = Data.Score.ToString();
}
private void btnSubPoints_Click(object sender, EventArgs e)
{
Data.ChangeScore(-100);
tbxCurrentScore.Text = Data.Score.ToString();
}
public string GetGameStatus()
{
var duration = Data.GameEnd - Data.GameStart;
return
$"Player {Data.PlayerName} : Id {Data.Id} : Score {Data.Score}\r\nStart : {Data.GameStart} : End {Data.GameEnd}\r\nDuration : hours {duration.Hours} | minutes: {duration.Minutes} | seconds: {duration.Seconds} | ms: {duration.Milliseconds}";
}
private void GamePlay_KeyDown(object sender, KeyEventArgs e)
{
if (ModifierKeys == Keys.Alt && e.KeyCode == Keys.Q)
{
this.Close();
}
}
}
public class ScoreData
{
public ScoreData(int id, string plname = "")
{
Id = id;
PlayerName = plname;
Score = 0;
GameStart = DateTime.Now;
}
public int Id { set; get; }
public string PlayerName { set; get; }
public int Score { set; get; }
public DateTime GameStart { set; get; }
public DateTime GameEnd { set; get; }
public void ChangeScore(int adjust)
{
Score += adjust;
}
}
Not implemented here: a way to pause the game, and re-start it, keeping track of actual game-play time ... should be easy.
... end edit ...
First, you need to make some strategic choices:
1. what Form do you want the user to see when the application launches ?
a. splash screen ?
b. configuration screen ?
c. log-in screen
d. other main screen ?
e. are there any circumstances under which the game-play window appears first ?
2. do your Forms require interaction, or need to send, or receive, data ? In your game, clearly, the score form must have the data from game-play.
3. use of Forms shown modal ?
a. do you want the game-screen to lock your app until it is closed ?
4. for each Form what visual settings do you want ?
a. FormBorderStyle, ShowinTaskBar, ShowIcon, StartPosition, etc.
b. fixed or variable size ?
Tell me your choices, and I;ll be haooy to ost a code example.