-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameWindow.cs
307 lines (265 loc) · 10.9 KB
/
GameWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Media;
namespace SimonSaysGame
{
public partial class GameWindow : Form
{
// Private data members
int score; // Increments with each successful sequence entry
bool difficult; // If true, speed of sequence display increases as score increases
bool arrowKeys; // Use arrow keys if true; WASD keys if false
List<int> sequence; // Contains the sequence to display and verify
Random randomizer; // Our random number generator
int currentStep; // Where currently iterating in sequence
//Timer inputTimer; // Timer for input delay (not affected by difficulty)
string upSoundFile = @"resource\up.wav"; // Holds path for the up sound effect
string leftSoundFile = @"resource\left.wav"; // Holds path for the left sound effect
string rightSoundFile = @"resource\right.wav"; // Holds path for the right sound effect
string downSoundFile = @"resource\down.wav"; // Holds path for the down sound effect
SoundPlayer upSound; // Controls playback of up sound
SoundPlayer leftSound; // Controls playback of left sound
SoundPlayer rightSound; // Controls playback of right sound
SoundPlayer downSound; // Controls playback of down sound
//Need a path to the file to store options
//Need a path to the file to store high scores
// Constants
const int UP = 0; // Sequence items for which button is used
const int LEFT = 1;
const int RIGHT = 2;
const int DOWN = 3;
const int EASY_TIME = 3000; // Milliseconds for pause between note display on standard difficulty
const int MAX_HARD_TIME = 500;
const int HARD_TIME_RATE = 200;
// Properties
public int Score
{
get
{
return score;
}
set
{
score = value;
}
}
public bool Difficult
{
get
{
return difficult;
}
set
{
difficult = value;
}
}
public bool ArrowKeys
{
get
{
return arrowKeys;
}
set
{
arrowKeys = value;
}
}
// Constructors
public GameWindow()
{
// Use the constructor to initialize the game
// Note that this does not start a game, just prepares it
// Initialize the score
score = 0;
// Set currentStep to zero as well
currentStep = 0;
// Since the difficulty radio buttons are on Easy by default...
difficult = false; // TODO: Modify based on options file
// TODO: Update the radio button based on the settings
// Initialize the buttons to be arrow keys by default
arrowKeys = true; // TODO: Modify based on options file
// Prepare the random number generator
randomizer = new Random();
// Prepare the timers
//inputTimer = new Timer();
// Initialize the list to start at 25 items in the sequence
sequence = new List<int>(25);
// Initialize SoundPlayers
upSound = new SoundPlayer(upSoundFile);
leftSound = new SoundPlayer(leftSoundFile);
rightSound = new SoundPlayer(rightSoundFile);
downSound = new SoundPlayer(downSoundFile);
// TODO: Load Options file.
// Show all the things
InitializeComponent();
}
private async void btnNewGame_Click(object sender, EventArgs e)
{
// Starts up a new game, resetting everything
int delayTime = EASY_TIME;
// Reset the score to 0 and display it
score = 0;
lblScore.Text = score.ToString();
// Reset the currentStep to 0
currentStep = 0;
// Load sounds
upSound.Load();
leftSound.Load();
rightSound.Load();
downSound.Load();
// Check the difficulty radio buttons and set the flag
difficult = radioHardDiff.Checked; // If false, easy is checked
// Develop the initial sequence
// Must reset sequence.Capacity to 25 or it will run away!
sequence.Clear();
sequence.TrimExcess();
sequence.Capacity = 25;
for (int i = 0; i < sequence.Capacity; i++)
{
sequence.Add(randomizer.Next(UP, DOWN+1));
}
// Disable the side controls
btnNewGame.Enabled = false;
radioHardDiff.Enabled = false;
radioStandardDiff.Enabled = false;
btnViewScores.Enabled = false;
btnOptions.Enabled = false;
// Initialize timers with Easy difficulty timing first
//inputTimer.Interval = EASY_TIME;
// Begin the game loop...
// Need to quit thinking like sequential programming and start
// using events to do all of this!
while (true)
{
/* Here is a breakdown of the cycle:
* 1. Display the sequence up to the current iteration
* a. Use score to know where we are stepping to
* b. Use currentStep to mark spot in sequence to display/check
* 2. Accept user input
* a. A timer checks for how long user hasn't made an input
* b. If time goes over, the game is over.
* c. If input made in time, check input versus sequence.
* d. If incorrect, the game is over.
* e. If correct, move to the next item in sequence.
* f. If reached score, increase score/start loop over again.
* g. If score > sequence capacity, get new direction.
* 3. Handle a game over state (outside of game loop)
* a. Display the score
* b. If a new high score, take user name
* c. Update/display high scores
* */
// Set display delay
if (difficult == true)
{
if (score >= 5)
{
// Adjust the timer after five rounds
delayTime -= HARD_TIME_RATE;
}
if (delayTime < MAX_HARD_TIME)
{
delayTime = MAX_HARD_TIME;
}
}
do
{
// Display the sequence up to the current iteration
switch (sequence[currentStep])
{
case UP: // Red
btnUp.BackColor = Color.Red;
upSound.Play();
await Task.Delay(delayTime);
btnUp.BackColor = DefaultBackColor;
break;
case LEFT: // Blue
btnLeft.BackColor = Color.Blue;
leftSound.Play();
await Task.Delay(delayTime);
btnLeft.BackColor = DefaultBackColor;
break;
case RIGHT: // Yellow
btnRight.BackColor = Color.Yellow;
rightSound.Play();
await Task.Delay(delayTime);
btnRight.BackColor = DefaultBackColor;
break;
case DOWN: // Green
btnDown.BackColor = Color.Green;
downSound.Play();
await Task.Delay(delayTime);
btnDown.BackColor = DefaultBackColor;
break;
default:
// ERROR!!!
MessageBox.Show("BUG: Invalid direction in sequence", "BUG!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
// Ensure all sounds have stopped
upSound.Stop();
leftSound.Stop();
rightSound.Stop();
downSound.Stop();
// Increment currentStep
currentStep++;
// Tiny delay between displaying next color
await Task.Delay(75);
} while (currentStep <= score);
// Accept user input
// TODO: Initiate timer to check for too long of a delay
// TODO: Check the input from the keyboard or button press
break;
}
// Restore controls
this.AcceptButton = btnNewGame;
btnNewGame.Enabled = true;
radioHardDiff.Enabled = true;
radioStandardDiff.Enabled = true;
btnViewScores.Enabled = true;
btnOptions.Enabled = true;
// Free sounds from memory
upSound.Dispose();
leftSound.Dispose();
rightSound.Dispose();
downSound.Dispose();
}
private void btnOptions_Click(object sender, EventArgs e)
{
// Displays the Options form as a modal dialog box
OptionsDialog optionsDialog = new OptionsDialog(this);
optionsDialog.ShowDialog(this);
// Set the options based on the selections in the dialog
if (optionsDialog.RadioArrows.Checked == true)
{
arrowKeys = true;
}
else if (optionsDialog.RadioWasd.Checked == true)
{
arrowKeys = false;
}
// Clean up when done
optionsDialog.Dispose();
// Update the game window with changes based on new options
if (arrowKeys == true)
{
// Set the button text to show arrow keys
btnUp.Text = "↑";
btnDown.Text = "↓";
btnLeft.Text = "←";
btnRight.Text = "→";
}
else
{
// Set the button text to show WASD
btnUp.Text = "W";
btnDown.Text = "S";
btnLeft.Text = "A";
btnRight.Text = "D";
}
}
}
}