-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameEngine.cs
192 lines (152 loc) · 6.34 KB
/
GameEngine.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Simon_3
{
public class GameEngine
{
private PiAndBash.Driver pnbDriver;
private PiAndBash.Display pnbDisplay;
private PiAndBash.ButtonCatcher buttonCatcher;
private bool isPlaying;
private PiAndBash.LedController leds;
private NotePlayer notePlayer;
public GameEngine(PiAndBash.Driver PiAndBashDriver, NotePlayer NotePlayer)
{
pnbDriver = PiAndBashDriver;
pnbDisplay = new PiAndBash.Display(pnbDriver);
leds = new PiAndBash.LedController(pnbDriver);
buttonCatcher = new PiAndBash.ButtonCatcher(pnbDriver);
notePlayer = NotePlayer;
// set up the Notes and associations
notes = new Dictionary<int, Note>();
notes.Add(1, new Note() { Color = PiAndBash.LedController.LightColor.Green, Button = PiAndBash.ButtonCatcher.ButtonType.Down, Frequency = 349 });
notes.Add(2, new Note() { Color = PiAndBash.LedController.LightColor.Yellow, Button = PiAndBash.ButtonCatcher.ButtonType.Enter, Frequency = 440 });
notes.Add(3, new Note() { Color = PiAndBash.LedController.LightColor.Red, Button = PiAndBash.ButtonCatcher.ButtonType.Up, Frequency = 523 });
}
void buttonCatcher_ButtonEvent(object sender, PiAndBash.ButtonCatcher.ButtonArgs e)
{
if (isPlaying && e.ButtonEvent == PiAndBash.ButtonCatcher.ButtonEventType.Down)
{
if (e.Button == beeps[currentPress].Button) // correct button
{
leds.SetLight(beeps[currentPress].Color, true);
notePlayer.PlayNote(beeps[currentPress].Frequency, 300);
leds.SetLight(beeps[currentPress].Color, false);
currentPress++;
if (currentPress == beeps.Count) // all notes pressed
{
pnbDisplay.BottomLine = "Score = " + beeps.Count.ToString();
Sleep(250);
nextRound();
}
}
else
{
isPlaying = false;
pnbDisplay.TopLine = "YOU LOSE!";
notePlayer.Volume = 100;
for (int i = 0; i < 5; i++)
{
foreach (var n in notes.OrderByDescending(n=>n.Key))
{
leds.SetLight(n.Value.Color, true);
notePlayer.PlayNote(n.Value.Frequency*0.7, 70);
leds.SetLight(n.Value.Color, false);
}
}
notePlayer.Volume = 50;
// wait for one second then reset
Sleep(1000);
pnbDisplay.TopLine = "< Press to exit";
pnbDisplay.BottomLine = "< Press to start";
buttonCatcher.Start();
}
}
else
{
if (e.ButtonEvent == PiAndBash.ButtonCatcher.ButtonEventType.Up)
{
if (e.Button == PiAndBash.ButtonCatcher.ButtonType.BotSel)
{
buttonCatcher.Stop();
isPlaying = true;
newGame();
}
if (e.Button == PiAndBash.ButtonCatcher.ButtonType.TopSel)
{
pnbDisplay.SetBacklight(false);
pnbDisplay.TopLine = "";
pnbDisplay.BottomLine = "";
buttonCatcher.Stop();
Console.WriteLine("Exiting");
}
}
}
}
public void Start()
{
// setup game parameters
// show welcome message and start instruction
pnbDisplay.TopLine = "Play Simon!";
pnbDisplay.BottomLine = "< Press to start";
// attach handler for buttons
buttonCatcher.ButtonEvent += buttonCatcher_ButtonEvent;
//start the button listener
buttonCatcher.Start();
}
private List<Note> beeps = new List<Note>();
private Dictionary<int, Note> notes;
private int currentPress = 0;
private class Note
{
public PiAndBash.LedController.LightColor Color { get; set; }
public PiAndBash.ButtonCatcher.ButtonType Button { get; set; }
public double Frequency { get; set; }
}
private void newGame()
{
pnbDisplay.TopLine = "Ready ...";
pnbDisplay.BottomLine = "";
// flash 3 leds
leds.SetLight(PiAndBash.LedController.LightColor.Green, true);
leds.SetLight(PiAndBash.LedController.LightColor.Red, true);
leds.SetLight(PiAndBash.LedController.LightColor.Yellow, true);
// wait a bit
Sleep(1500);
// turn them off
leds.SetLight(PiAndBash.LedController.LightColor.Green, false);
leds.SetLight(PiAndBash.LedController.LightColor.Red, false);
leds.SetLight(PiAndBash.LedController.LightColor.Yellow, false);
// game variables
beeps.Clear();
// round 1
pnbDisplay.TopLine = "GO!!!";
pnbDisplay.BottomLine = "";
nextRound();
}
private void nextRound()
{
buttonCatcher.Stop();
Random rnd = new Random();
int newBeep = rnd.Next(1, 4);
beeps.Add(notes[newBeep]);
foreach (Note n in beeps)
{
// delay before next note
Sleep(200);
leds.SetLight(n.Color, true);
notePlayer.PlayNote(n.Frequency, 500);
leds.SetLight(n.Color, false);
}
currentPress = 0;
buttonCatcher.Start();
}
private void Sleep(int duration)
{
System.Threading.Thread.Sleep(duration);
}
}
}