-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpaceFighter.ino
More file actions
147 lines (138 loc) · 3.92 KB
/
SpaceFighter.ino
File metadata and controls
147 lines (138 loc) · 3.92 KB
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
/*
* SpaceFighter
* Copyright (C) 2015
* Maicon Hieronymus <mhierony@students.uni-mainz.de>
* All rights reserved.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
* -----------------------------------------------------------------------------
*
* @file SpaceFighter.ino
* @date 25.02.2018
* @version 1.0
* @author Maicon Hieronymus <mhierony@students.uni-mainz.de>
*
* @brief This program is a shooter game set in space where you control a
* more or less cool spaceship.
* It is coded with Arduboy in mind using the Arduboy2 library.
*/
#include <Arduboy2.h>
#include "variables.h"
#include "draw.h"
#include "movement.h"
#include "collision.h"
#include "generator.h"
Arduboy2 arduboy;
byte frameCounter;
Star stars[MAXSTARS];
byte numberOfStars = 0;
byte SCREEN_HEIGHT = 64;
byte SCREEN_WIDTH = 128;
Player player;
Enemy enemies[MAXENEMIES];
byte numberOfEnemies = 0;
Bullet bullets[MAXBULLETS];
byte numberOfBullets = 0;
Supply supplies[MAXSUPPLY];
byte noOfSupplies = 0;
bool gameStarted = false;
Explosion explosions[MAXENEMIES+1];
bool boss_coming = false;
byte extra_tick = 0;
byte which_extra;
byte last_boss = 0;
unsigned int high_score = 0;
void gameOver() {
drawGameOver();
delay(4000);
if(player.score > high_score) {
high_score = player.score;
EEPROM.put(EEPROM_SCORE, high_score);
}
bool draw_score = true;
while(draw_score) {
drawHighscore(true);
delay(200);
if(arduboy.pressed(A_BUTTON) || arduboy.pressed(B_BUTTON))
draw_score = false;
}
}
/*
* @brief Checks if EEPROM is initialised. See
* https://github.com/filmote/Steve/blob/master/EEPROMUtils.h
*/
void initEEPROM() {
byte c1 = EEPROM.read(EEPROM_START_C1);
byte c2 = EEPROM.read(EEPROM_START_C2);
// I just take two numbers in front of the actual highscore to check, if
// it is a highscore from my game.
if(c1 != 83 || c2 != 77) {
EEPROM.update(EEPROM_START_C1, 83);
EEPROM.update(EEPROM_START_C2, 77);
EEPROM.put(EEPROM_SCORE, high_score);
}
EEPROM.get(EEPROM_SCORE, high_score);
}
void setup() {
initEEPROM();
arduboy.begin();
arduboy.setFrameRate(60);
arduboy.initRandomSeed();
}
void loop() {
// No rendering needed until the next frame is needed. Duh!
if(!(arduboy.nextFrame()))
return;
if(gameStarted) {
if(player.alive) {
generateStar();
if(player.score/100 > last_boss) {
last_boss++;
boss_coming = true;
}
if(boss_coming) {
generateBoss();
} else {
generateEnemy();
}
moveGame();
checkCollision();
checkAlive();
drawGame();
if(arduboy.pressed(A_BUTTON)) {
delay(200);
drawPause();
}
} else {
if(drawGame()) {
gameStarted = false;
delay(1000);
} else {
moveGame();
}
}
if(!gameStarted) {
if(player.lives == 0) {
gameOver();
} else {
initGame(false);
gameStarted = true;
}
}
} else {
showTitle();
gameStarted = true;
initGame(true);
}
}