-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAvoid_Obstacles.ino
154 lines (133 loc) · 3.76 KB
/
Avoid_Obstacles.ino
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
#include <Adafruit_CircuitPlayground.h>
// Game states
enum GameState {
START,
GAME_RUNNING,
GAME_OVER
};
// Constants
const int CHARACTER_SPEED = 1; // Adjust character movement speed as desired
const int VICTORY_DURATION = 10000; // Duration to stay alive for victory (in milliseconds)
const int OBSTACLE_INTERVAL = 3000; // Interval for obstacle position update (in milliseconds)
// Variables
int characterPosition;
int obstaclePosition;
unsigned long gameTimer;
unsigned long victoryTimer;
unsigned long obstacleTimer;
float midi[128];
int A_four = 440;
GameState gameState;
volatile bool ending;
// MIDI notes for winning and losing tunes
int victoryTune[8][2] = {
{60, 100},
{61, 100},
{62, 100},
{63, 100},
{65, 100},
{67, 100},
{69, 100},
{71, 300},
};
int lossTune[4][2] = {
{60, 100},
{58, 100},
{57, 100},
{54, 300},
};
void setup() {
CircuitPlayground.begin();
CircuitPlayground.clearPixels();
gameState = START;
generateMIDI();
}
void loop() {
switch (gameState) {
case START:
startGame();
break;
case GAME_RUNNING:
updateGame();
break;
case GAME_OVER:
endGame();
break;
}
}
void startGame() {
characterPosition = 4; // Start in the middle of the LED grid
obstaclePosition = random(10); // Spawn the obstacle at a random position
gameTimer = millis();
victoryTimer = 0;
obstacleTimer = millis();
CircuitPlayground.clearPixels();
CircuitPlayground.setPixelColor(characterPosition, 0, 255, 0);
CircuitPlayground.setPixelColor(obstaclePosition, 255, 0, 0);
gameState = GAME_RUNNING;
}
void updateGame() {
unsigned long currentMillis = millis();
// Move character based on button input
if (CircuitPlayground.leftButton() && characterPosition > 0) {
CircuitPlayground.setPixelColor(characterPosition, 0);
characterPosition -= CHARACTER_SPEED;
delay(200);
}
else if (CircuitPlayground.leftButton() && characterPosition == 0) {
CircuitPlayground.setPixelColor(characterPosition, 0);
characterPosition = 9;
delay(200);
}
if (CircuitPlayground.rightButton() && characterPosition < 9) {
CircuitPlayground.setPixelColor(characterPosition, 0);
characterPosition += CHARACTER_SPEED;
delay(200);
}
else if (CircuitPlayground.rightButton() && characterPosition == 9) {
CircuitPlayground.setPixelColor(characterPosition, 0);
characterPosition = 0;
delay(200);
}
// Update obstacle position periodically
if (currentMillis - obstacleTimer >= OBSTACLE_INTERVAL) {
CircuitPlayground.setPixelColor(obstaclePosition, 0);
obstaclePosition = random(10);
obstacleTimer = currentMillis;
}
// Check for collision
if (characterPosition == obstaclePosition) {
gameState = GAME_OVER; // Collision with obstacle, game over
ending = false;
}
if (currentMillis - gameTimer >= VICTORY_DURATION) {
victoryTimer = currentMillis;
ending = true;
}
CircuitPlayground.setPixelColor(characterPosition, 0, 255, 0);
CircuitPlayground.setPixelColor(obstaclePosition, 255, 0, 0);
}
void endGame() {
CircuitPlayground.clearPixels();
// Play sound based on game outcome
if (ending) {
for(int i = 0; i < sizeof(victoryTune) / sizeof(victoryTune[0]); i++) {
CircuitPlayground.playTone(midi[victoryTune[i][0]], victoryTune[i][1]);
delay(1);
} // Victory sound
} else {
for(int i = 0; i < sizeof(lossTune) / sizeof(lossTune[0]); i++) {
CircuitPlayground.playTone(midi[lossTune[i][0]], lossTune[i][1]);
delay(1);
} // Loss sound
}
// Wait for a short duration before restarting the game
delay(500);
gameState = START;
}
void generateMIDI() {
for (int x = 0; x < 128; ++x) {
midi[x] = (A_four / 32.0) * pow(2.0, ((x - 9.0) / 12.0));
Serial.println(midi[x]);
}
}