-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialascii.ino
269 lines (232 loc) · 7.13 KB
/
initialascii.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
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
#include <TVout.h>
#include <fontALL.h>
TVout TV;
uint8_t h_char, v_char;
uint32_t score, best;
// structure for the obstacle on the "road" and the player
struct character
{
char sprite;
int v_char;
int h_char;
};
uint8_t const NB_OBSTACLE = 8;
character obstacles[NB_OBSTACLE];
character player;
void setup() {
// Setup the random number generator
Serial.begin(9600);
randomSeed(analogRead(0));
Serial.end();
TV.begin(NTSC, 128, 96);
TV.select_font(font4x6);
h_char = TV.hres() / 4;
v_char = TV.vres() / 6;
best = 0; // highscore
init_game();
}
void init_game() {
// Create race track
TV.clear_screen();
draw_border(35);
// init score
score = 0;
// reset player position + draw player
player.h_char = h_char / 2;
player.v_char = v_char - 1;
player.sprite = 88;
draw_char(player.h_char, player.v_char, player.sprite);
// generating initials obstacles
generate_all_obstacles();
draw_all_obstacles();
}
void draw_char(uint8_t h_char_pos, uint8_t v_char_pos, char c) {
TV.print_char(h_char_pos*4, v_char_pos * 6, c);
}
void clear_char(uint8_t h_char_pos, uint8_t v_char_pos) {
TV.print_char(h_char_pos*4, v_char_pos * 6, 32);
}
void move_player_up() {
if (player.v_char > 1) {
clear_char(player.h_char, player.v_char);
player.v_char -= 1;
draw_char(player.h_char, player.v_char, player.sprite);
}
}
void move_player_down() {
if (player.v_char < v_char - 1) {
clear_char(player.h_char, player.v_char);
player.v_char += 1;
draw_char(player.h_char, player.v_char, player.sprite);
}
}
void move_player_left() {
if (player.h_char > 11) {
clear_char(player.h_char, player.v_char);
player.h_char -= 1;
draw_char(player.h_char, player.v_char, player.sprite);
}
}
void move_player_right() {
if (player.h_char < 20) {
clear_char(player.h_char, player.v_char);
player.h_char += 1;
draw_char(player.h_char, player.v_char, player.sprite);
}
}
void generate_all_obstacles() {
for (int i = 0; i < NB_OBSTACLE; i++){
obstacles[i] = generate_obstacle(random(h_char / 2 - 5, h_char / 2 + 5), random(0, v_char - 1));
}
}
void draw_all_obstacles() {
for (int i = 0; i < NB_OBSTACLE; i++){
draw_char(obstacles[i].h_char, obstacles[i].v_char, obstacles[i].sprite);
}
}
void scroll_screen() {
for (int i = 0; i < NB_OBSTACLE; i++){
clear_char(obstacles[i].h_char, obstacles[i].v_char);
obstacles[i].v_char += 1;
draw_char(obstacles[i].h_char, obstacles[i].v_char, obstacles[i].sprite);
}
}
character generate_obstacle(uint8_t h_char_pos, uint8_t v_char_pos) {
character obs;
obs.sprite = random(33, 48);
obs.h_char = h_char_pos;
obs.v_char = v_char_pos;
return obs;
}
// Generate new obstacle if one goes offscreen
void renew_obstacle() {
for (int i = 0; i < NB_OBSTACLE; i++) {
if (obstacles[i].v_char >= v_char) {
//delete *obstacles[i];
obstacles[i] = generate_obstacle(random(h_char / 2 - 5, h_char / 2 + 5), 0);
draw_char(obstacles[i].h_char, obstacles[i].v_char, obstacles[i].sprite);
}
}
}
// move the "player" automatically, sicne this is a self playing game.
void move_player() {
bool moved = false;
bool obs_front = false;
bool obs_far_front = false; // 2 char in front
bool obs_front_left = false;
bool obs_front_right = false;
bool obs_left = false;
bool obs_right = false;
bool obs_behind = false;
// Checking if obstacles are near the player
for (int i = 0; i < NB_OBSTACLE; i++) {
// Check if there is an obstacle on the line in front of player (2 char ahead)
if (obstacles[i].v_char == player.v_char - 2) {
// Check if this obstacle is directly in front of player
if (obstacles[i].h_char == player.h_char) {
obs_far_front = true;
}
}
// Check if there is an obstacle on the line in front of player
if (obstacles[i].v_char == player.v_char - 1) {
// Check if this obstacle is directly in front of player
if (obstacles[i].h_char == player.h_char) {
obs_front = true;
}
if (obstacles[i].h_char == player.h_char - 1) {
obs_front_left = true;
}
if (obstacles[i].h_char == player.h_char + 1) {
obs_front_right = true;
}
}
// Check if there is an obstacle on the same line as player
if (obstacles[i].v_char == player.v_char) {
if (obstacles[i].h_char == player.h_char - 1) {
obs_left = true;
}
if (obstacles[i].h_char == player.h_char + 1) {
obs_right = true;
}
}
// Check if there is an obstacle behind player
if (obstacles[i].v_char == player.v_char + 1) {
if (obstacles[i].h_char == player.h_char) {
obs_behind = true;
}
}
}
// Deciding the next move
if (obs_front) {
if (! obs_front_left and ! obs_left and player.h_char > 11) {
move_player_left();
}
else if (! obs_front_right and ! obs_right and player.h_char < 21) {
move_player_right();
}
else if (! obs_behind and player.v_char < v_char - 1) {
move_player_down();
}
}
// basically we do what we want
else if (! obs_front and ! obs_far_front and ! obs_front_left and ! obs_front_right and ! obs_left and ! obs_right and ! obs_behind) {
switch (random(0,4)) {
case 0:
move_player_right();
break;
case 1:
move_player_left();
break;
case 2:
move_player_up();
break;
case 3:
move_player_down();
break;
}
}
}
void check_colision() {
for (int i = 0; i < NB_OBSTACLE; i++) {
if (obstacles[i].v_char == player.v_char) {
if (obstacles[i].h_char == player.h_char){
TV.set_cursor(11 * 4, 0); // 11 * 4 = 11 char from left side
TV.write("Game");
TV.set_cursor(11 * 4, 6); // 11 * 4 = 11 char from left side
TV.write("Over");
TV.delay(2000);
if (score > best) {
best = score;
}
init_game();
}
}
}
}
void increase_score() {
char str[10];
sprintf(str, "%u", ++score);
TV.set_cursor(0, 0);
TV.write(str);
TV.set_cursor(0, 6);
sprintf(str, "%u", best);
TV.write(str);
}
void draw_border(char c) {
for(int y=0;y < v_char; y++){
for(int x=0; x < h_char; x++){
if (x < h_char / 2 - 5 || x > h_char / 2 + 4) {
TV.print_char(x*4,y*6, c); //#
}
}
}
}
void loop() {
increase_score();
TV.delay(75);
move_player();
check_colision();
scroll_screen();
renew_obstacle();
check_colision();
}