-
Notifications
You must be signed in to change notification settings - Fork 1
/
SpaceKnife.ino
224 lines (183 loc) · 4.99 KB
/
SpaceKnife.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
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
// Space Knife game for Arduboy
// https://github.com/streetalchemist/SpaceKnife
// Alex Porter 2016
#include "Arduboy.h"
#include "bitmaps.h"
#include "Stars.h"
#include "Bullets.h"
#include "Enemies.h"
//#include "Squawk.h"
//#include "tunes.h"
#include <EEPROM.h>
#include "localEEPROM.h"
//SQUAWK_CONSTRUCT_ISR(SQUAWK_PWM_PIN5)
Arduboy arduboy;
//AbPrinter text(arduboy);
Stars stars;
Bullets bullets;
Enemies enemies;
ArduboyEeprom savedData;
int retVal;
int x=5; //Ship x
int y=20; //Ship y
int shipSpeed=3;
int score=0;
int gameState = 1;
uint8_t bulletCounter = 0;
bool canShoot = true;
//uint8_t frameCount = 1;
String scoreString = "Score: ";
String highScoreString = "High Score: ";
uint8_t savedScore;
#define X_MAX (WIDTH - 20)
#define Y_MAX (HEIGHT - 10)
void setup() {
// Set up Squawk to generate samples at 32kHz.
// Squawk always steals Timer1 for sample crunching.
//Squawk.begin(32000);
// Begin playback of melody.
//Squawk.play(TheOriginalSquawk);
// Tune the song to something more suitable for a piezo
//Squawk.tune(2.0);
// Lower the tempo ever so slightly
//Squawk.tempo(48);
// Setup High Score Saving
// if ((retVal = savedData.EEPROM_BEGIN) != 0) {
// if (retVal == EEPROM_ALLOCATED) {
// //EMPTY REGISTER
// //savedData.write(0, score);
// savedScore = savedData.read(0);
// }
// else {
// text.print("Allocate Error: ");
// text.println(retVal);
// }
// }
// else {
// //savedScore = savedData.read(0);
// savedData.write(0, score);
// }
// put your setup code here, to run once:
arduboy.begin();
arduboy.setFrameRate(60);
arduboy.display();
intro();
enemies.setup();
}
void loop() {
// put your main code here, to run repeatedly:
if (!(arduboy.nextFrame())) {
return;
}
//Show Score
arduboy.setCursor(40, 5);
String scoreValue = scoreString+score;
arduboy.print(scoreValue);
//active background starz
stars.activate(&arduboy);
if(gameState == 1) {
bullets.activate(&arduboy);
// if the right button is pressed move 1 pixel to the right every frame
if(arduboy.pressed(RIGHT_BUTTON) && (x < X_MAX)) {
x+= shipSpeed;
}
// if the left button is pressed move 1 pixel to the left every frame
if(arduboy.pressed(LEFT_BUTTON) && (x > 0)) {
x-= shipSpeed;
}
// if the up button or B button is pressed move 1 pixel up every frame
if(arduboy.pressed(UP_BUTTON) && (y > 0)) {
y-= shipSpeed;
}
// if the down button or A button is pressed move 1 pixel down every frame
if(arduboy.pressed(DOWN_BUTTON) && (y < Y_MAX)) {
y+= shipSpeed;
}
if(arduboy.pressed(A_BUTTON)) {
if(canShoot) {
shoot();
}
}
if(arduboy.notPressed(A_BUTTON)) {
canShoot = true;
}
//Check Bullet Collisions
checkCollisions();
arduboy.drawBitmap(x, y, bitmap_ship, 20, 10, WHITE);
}
//Enemy Movement
enemies.activate(&arduboy);
//Game Over Screen Game State
if(gameState == 2) {
arduboy.setCursor(37, 30);
arduboy.print("GAME OVER");
//Print High Score
//arduboy.setCursor(25, 40);
//String highScoreValue;
//highScoreValue = highScoreString+savedScore;
//arduboy.print(highScoreValue);
arduboy.setCursor(11, 53);
arduboy.print("press A to restart");
if(arduboy.pressed(B_BUTTON)) {
restart();
}
}
arduboy.display();
arduboy.clear();
}
void restart() {
score = 0;
x = 5;
y = 20;
gameState = 1;
enemies.setup();
}
void shoot() {
canShoot = false;
bullets.bullets[bulletCounter].x = x+10;
bullets.bullets[bulletCounter].y = y+4;
bulletCounter++;
if(bulletCounter > 16) {
bulletCounter = 0;
}
}
void checkCollisions() {
for(int enemyIndex = 0; enemyIndex < 8; enemyIndex++) {
for(int bulletIndex = 0; bulletIndex < 16; bulletIndex++) {
if(doesIntersect(bullets.bullets[bulletIndex].x,bullets.bullets[bulletIndex].y, 3,1,enemies.enemiesArr[enemyIndex].x,enemies.enemiesArr[enemyIndex].y,enemies.enemyWidth,enemies.enemyHeight)) {
enemies.reset(enemyIndex);
score++;
bullets.bullets[bulletIndex].x = -50;
}
}
if(doesIntersect(x,y,20,10,enemies.enemiesArr[enemyIndex].x, enemies.enemiesArr[enemyIndex].y, enemies.enemyWidth, enemies.enemyHeight)) {
x = -100;
//setHighScore();
gameState = 2;
}
}
}
bool doesIntersect(int x1, int y1,int width1,int height1,int x2,int y2,int width2,int height2) {
if(x1 < x2 + width2 && x1 + width1 > x2 && y1 < y2 + height2 && y1 + height1 > y2) {
return true;
}
return false;
}
//void setHighScore() {
// if(score > savedScore) {
// savedData.write(0, score);
// savedScore = score;
// }
//}
void intro()
{
arduboy.clear();
arduboy.drawBitmap(0, 0, bitmap_streetalchemist, 128, 64, WHITE);
arduboy.display();
delay(3000);
arduboy.clear();
arduboy.drawBitmap(0, 0, bitmap_intro_screen, 128, 64, WHITE);
arduboy.display();
delay(3000);
}