-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.c
More file actions
596 lines (531 loc) · 16.8 KB
/
game.c
File metadata and controls
596 lines (531 loc) · 16.8 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
/*
* game.c
*
* Author: Peter Sutton
*/
#include "game.h"
#include "ledmatrix.h"
#include "pixel_colour.h"
#include "project.h"
#include "score.h"
#include "countdown.h"
#include "sound.h"
#include "terminalio.h"
#include <stdint.h>
#include <stdio.h>
///////////////////////////////// Global variables //////////////////////
// frog_row and frog_column store the current position of the frog. Row
// numbers are from 0 to 7; column numbers are from 0 to 15.
static int8_t frog_row;
static int8_t frog_column;
// Vehicle data - 64 bits in each lane which we loop continuously. A 1
// indicates the presence of a vehicle, 0 is empty.
// Index 0 to 2 corresponds to lanes 1 to 3 respectively. Lanes 1 and 3
// will move to the right; lane 2 will move to the left.
#define LANE_DATA_WIDTH 64 // must be power of 2
static uint64_t lane_data[4][3] = {
{
0b1100001100011000110000011001100011000011000110001100000110011000,
0b0011100000111000011100000111000011100001110001110000111000011100,
0b0000111100001111000011110000111100001111000001111100001111000111
},
{
0b0000111100001111000011110000111100001111000001111100001111000111,
0b1100001100011000110000011001100011000011000110001100000110011000,
0b0000111100001111000011110000111100001111000001111100001111000111
},
{
0b1111110011111100011000001100110001100001100011111110111011001100,
0b0011100000111000011100000111000011100001110001110000111000011100,
0b0000111100001111000011110000111100001111000001111100001111000111
},
{
0b1111000110011100011110001111100011110001100111000111100011111000,
0b0010010010010010000111101011110010010010010010010001111010111101,
0b0000111100001111000011110000111100001111000001111100001111000111
},
};
// Log data - 32 bits for each log channel which we loop continuously.
// A 1 indicates the presence of a log, 0 is empty.
// Index 0 to 1 corresponds to rows 5 and 6 respectively. Row 5 will move
// to the left; row 6 will move to the right
#define LOG_DATA_WIDTH 32 // must be power of 2
static uint32_t log_data[4][2] = {
{
0b11110001100111000111100011111000,
0b11100110111101100001110110011100
},
{
0b01010101000101010101010001010101,
0b001001001001001000011110101111001
},
{
0b11111001110111000111100011001000,
0b01010101000101010101010001010101
},
{
0b11100110111101100001110110011100,
0b00100100100100100001111010111101
}
};
// Lane positions. The bit position (0 to 63) of the lane_data above that is
// currently in column 0 of the display (left hand side). (Bit position
// 0 is the least significant bit.) For a lane position of N, the display
// will show bits N to N+15 from left to right (wrapping around if N+15
// exceeds 63).
static int8_t lane_position[3];
// Log positions. Same principle as lane positions.
static int8_t log_position[2];
// Colours
#define COLOUR_FROG COLOUR_GREEN
#define COLOUR_DEAD_FROG COLOUR_LIGHT_YELLOW
#define COLOUR_EDGES COLOUR_LIGHT_GREEN
#define COLOUR_WATER COLOUR_BLACK
#define COLOUR_ROAD COLOUR_BLACK
PixelColour colour_logs[4] = {
COLOUR_ORANGE,
COLOUR_YELLOW,
COLOUR_RED,
COLOUR_ORANGE
};
PixelColour vehicle_colours[4][3] = {
{ COLOUR_RED, COLOUR_YELLOW, COLOUR_RED },
{ COLOUR_RED, COLOUR_RED, COLOUR_RED },
{ COLOUR_GREEN, COLOUR_LIGHT_YELLOW, COLOUR_GREEN },
{ COLOUR_RED, COLOUR_GREEN, COLOUR_RED }
}; // by lane
// Rows
#define START_ROW 0 // row position where the frog starts
#define FIRST_VEHICLE_ROW 1
#define SECOND_VEHICLE_ROW 2
#define THIRD_VEHICLE_ROW 3
#define HALFWAY_ROW 4 // row position where the frog can rest
#define FIRST_RIVER_ROW 5
#define SECOND_RIVER_ROW 6
#define RIVERBANK_ROW 7 // row position where the frog finishes
// River bank pattern. Note that the least significant bit in this
// pattern (RHS) corresponds to column 0 on the display (LHS).
#define RIVERBANK 0b1101110111011101
static uint16_t riverbank;
// riverbank_status is a bit pattern similar to riverbank but will
// only have zeroes where there are unoccupied holes. When this is all 1's
// then the game/level is complete
static uint16_t riverbank_status;
/////////////////////////////// Function Prototypes for Helper Functions ///////
// These functions are defined after the public functions. Comments are with the
// definitions.
static uint8_t will_frog_die_at_position(int8_t row, int8_t column);
static void redraw_whole_display(void);
static void redraw_row(uint8_t row);
static void redraw_roadside(uint8_t row);
static void redraw_traffic_lane(uint8_t lane);
static void redraw_river_channel(uint8_t channel);
static void redraw_riverbank(void);
void redraw_frog(void);
/////////////////////////////// Public Functions ///////////////////////////////
// These functions are defined in the same order as declared in game.h
// Reset the game
void initialise_game(void) {
// Initial lane and log positions
lane_position[0] = lane_position[1] = lane_position[2] = 0;
log_position[0] = log_position[1] = 0;
// Initial riverbank pattern
riverbank = RIVERBANK;
riverbank_status = RIVERBANK;
//if (!on_same_game)
redraw_whole_display();
// Add a frog to the roadside - this will redraw the frog
put_frog_in_start_position();
}
// Add a frog to the game
void put_frog_in_start_position(void) {
// Initial starting position of frog (7,0)
frog_row = 0;
frog_column = 7;
// Frog is initially alive
frog_dead = 0;
// Show the frog
redraw_frog();
}
// This function assumes that the frog is not in row 7 (the top row). A frog in row 7 is out
// of the game.
void move_frog_forward(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row+1, frog_column);
if (!frog_dead) {
add_to_score(1);
move_cursor(10,1);
printf("\nYour score is: %9lu\n", get_score());
}
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row++;
redraw_frog();
// If the frog has ended up successfully in row 7 - add it to the riverbank_status flag
if(!frog_dead && frog_row == RIVERBANK_ROW) {
add_to_score(10);
move_cursor(10,1);
printf("\nYour score is: %9lu\n", get_score());
reset_countdown();
riverbank_status |= (1<<frog_column);
}
}
}
void move_frog_backward(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row-1, frog_column);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row--;
redraw_frog();
}
}
void move_frog_to_left(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row, frog_column-1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_column--;
redraw_frog();
}
}
void move_frog_to_right(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row, frog_column+1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_column++;
redraw_frog();
}
}
void move_frog_up_left(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row+1, frog_column-1);
if (!frog_dead)
add_to_score(1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row++;
frog_column--;
redraw_frog();
if(!frog_dead && frog_row == RIVERBANK_ROW) {
add_to_score(10);
reset_countdown();
riverbank_status |= (1<<frog_column);
}
}
}
void move_frog_up_right(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row+1, frog_column+1);
if (!frog_dead)
add_to_score(1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row++;
frog_column++;
redraw_frog();
if(!frog_dead && frog_row == RIVERBANK_ROW) {
add_to_score(10);
reset_countdown();
riverbank_status |= (1<<frog_column);
}
}
}
void move_frog_down_left(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row-1, frog_column-1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row--;
frog_column--;
redraw_frog();
}
}
void move_frog_down_right(void) {
play_sound(100, 200);
if (paused)
paused = !paused;
else {
// Redraw the row the frog is currently on (this will remove the frog)
redraw_row(frog_row);
// Check whether this move will cause the frog to die or not
frog_dead = will_frog_die_at_position(frog_row-1, frog_column+1);
// Move the frog position forward and show the frog.
// We do this whether the frog is alive or not.
frog_row--;
frog_column++;
redraw_frog();
}
}
uint8_t get_frog_row(void) {
return frog_row;
}
uint8_t get_frog_column(void) {
return frog_column;
}
uint8_t is_riverbank_full(void) {
return (riverbank_status == 0xFFFF);
}
uint8_t frog_has_reached_riverbank(void) {
return (frog_row == RIVERBANK_ROW);
}
uint8_t is_frog_dead(void) {
return frog_dead;
}
// Scroll the given lane of traffic. (lane value must be 0 to 2)
void scroll_vehicle_lane(uint8_t lane, int8_t direction) {
uint8_t frog_is_in_this_row = (frog_row == lane + FIRST_VEHICLE_ROW);
// Work out the new lane position.
// Wrap numbers around if they go out of range
// A direction of -1 indicates movement to the left which means we
// start from a higher bit position in column 0
lane_position[lane] -= direction;
if(lane_position[lane] < 0) {
lane_position[lane] = LANE_DATA_WIDTH-1;
} else if(lane_position[lane] >= LANE_DATA_WIDTH) {
lane_position[lane] = 0;
}
// Show the lane on the display
redraw_traffic_lane(lane);
// If the frog is in this row, show it
if(frog_is_in_this_row) {
// Update whether the frog will be alive or not. (The frog hasn't moved but
// it may have been hit by a vehicle.)
frog_dead = will_frog_die_at_position(frog_row, frog_column);
redraw_frog();
}
}
void scroll_river_channel(uint8_t channel, int8_t direction) {
uint8_t frog_is_in_this_row = (frog_row == channel + FIRST_RIVER_ROW);
// Note, if the frog is in this row then it will be on a log
if(frog_is_in_this_row) {
// Check if they're going to hit the edge - don't let the frog
// go beyond the edge
if(direction == 1 && frog_column == 15) {
frog_dead = 1; // hit right edge
} else if(direction == -1 && frog_column == 0) {
frog_dead = 1; // hit left edge
} else {
// Move the frog with the log - they're not going to hit the edge
frog_column += direction;
}
}
// Work out the new log position.
// Wrap numbers around if they go out of range
log_position[channel] -= direction;
if(log_position[channel] < 0) {
log_position[channel] = LOG_DATA_WIDTH-1;
} else if(log_position[channel] >= LOG_DATA_WIDTH) {
log_position[channel] = 0;
}
// Work out the log data to send to the display
redraw_river_channel(channel);
// If the frog is in this row, put them on the log
if(frog_is_in_this_row) {
redraw_frog();
}
}
/////////////////////////////// Private (Helper) Functions /////////////////////
// Return 1 if the frog will die at the given position.
// Return 0 if the frog CAN jump to the given position (i.e. it is not occupied by
// a vehicle), or, if in the river, then it IS occupied by a log, or, if the final
// riverbank then that space is free.
static uint8_t will_frog_die_at_position(int8_t row, int8_t column) {
uint8_t lane, channel, bit_position;
if(column < 0 || column > 15) {
return 1;
}
switch(row) {
case 0: // always safe
case 4: // always safe
return 0;
break;
case 1:
case 2:
case 3:
lane = row - 1;
bit_position = lane_position[lane] + column;
if(bit_position >= LANE_DATA_WIDTH) {
bit_position -= LANE_DATA_WIDTH;
}
return (lane_data[current_level % 4][lane] >> bit_position) & 1;
break;
case 5:
case 6:
channel = row - 5;
bit_position = log_position[channel] + column;
if(bit_position >= LOG_DATA_WIDTH) {
bit_position -= LOG_DATA_WIDTH;
}
return !((log_data[current_level % 4][channel] >> bit_position) & 1);
break;
case 7:
return (riverbank_status >> column) & 1;
break;
}
// Any row outside the valid range means the frog will die
return 1;
}
// Redraw the rows on the game field. The frog is not redrawn.
static void redraw_whole_display(void) {
// Clear the display
ledmatrix_clear();
// Start with the starting and halfway rows
redraw_roadside(START_ROW);
redraw_roadside(HALFWAY_ROW);
// Redraw traffic lanes
for(uint8_t lane=0; lane<=2; lane++) {
redraw_traffic_lane(lane);
}
// Redraw river
for(uint8_t channel=0; channel<=1; channel++) {
redraw_river_channel(channel);
}
// Redraw riverbank
redraw_riverbank();
}
// Redraw the row with the given number (0 to 7). The frog is not redrawn.
static void redraw_row(uint8_t row) {
// Remove frog from current position (we need to update the display
// so it shows the right colour pixel in its place). We know the frog
// must be either on a road edge, on the road or on a log.
switch(row) {
case START_ROW:
case HALFWAY_ROW:
redraw_roadside(row);
break;
case FIRST_VEHICLE_ROW:
case SECOND_VEHICLE_ROW:
case THIRD_VEHICLE_ROW:
redraw_traffic_lane(row-1);
break;
case FIRST_RIVER_ROW:
case SECOND_RIVER_ROW:
redraw_river_channel(row-5);
break;
case RIVERBANK_ROW:
redraw_riverbank();
break;
default:
// Invalid row - ignore
break;
}
}
// Redraw the given roadside row (0 or 4). The frog is not redrawn.
static void redraw_roadside(uint8_t row) {
MatrixRow row_display_data;
uint8_t i;
for(i=0;i<=15;i++) {
row_display_data[i] = COLOUR_EDGES;
}
ledmatrix_update_row(row, row_display_data);
}
// Redraw the given traffic lane (0, 1, 2). The frog is not redrawn.
static void redraw_traffic_lane(uint8_t lane) {
MatrixRow row_display_data;
uint8_t i;
uint8_t bit_position = lane_position[lane];
for(i=0; i<=15; i++) {
if((lane_data[current_level % 4][lane] >> bit_position) & 1) {
row_display_data[i] = vehicle_colours[current_level % 4][lane];
} else {
row_display_data[i] = COLOUR_ROAD;
}
bit_position++;
if(bit_position >= LANE_DATA_WIDTH) {
// Wrap around in our lane data
bit_position = 0;
}
}
ledmatrix_update_row(lane+FIRST_VEHICLE_ROW, row_display_data);
}
// Redraw the given river channel (0 or 1). The frog is not redrawn.
static void redraw_river_channel(uint8_t channel) {
MatrixRow row_display_data;
uint8_t i;
uint8_t bit_position = log_position[channel];
for(i=0; i<=15; i++) {
if((log_data[current_level % 4][channel] >> bit_position) & 1) {
row_display_data[i] = colour_logs[current_level % 4];
} else {
row_display_data[i] = COLOUR_WATER;
}
bit_position++;
if(bit_position >= LOG_DATA_WIDTH) {
bit_position = 0;
}
}
ledmatrix_update_row(channel+FIRST_RIVER_ROW, row_display_data);
}
// Redraw the riverbank (top row). Previous frogs which have made it to a hole
// at the top are shown.
static void redraw_riverbank(void) {
MatrixRow row_display_data;
uint8_t i;
// Blank out spaces in our rowdata where there are holes in the riverbank
for(i=0; i<= 15; i++) {
if((riverbank >> i) & 1) {
// Riverbank edge
row_display_data[i] = COLOUR_EDGES;
} else if ((riverbank_status >> i) & 1) {
// Frog occupying a hole
row_display_data[i] = COLOUR_FROG;
} else {
// Empty hole
row_display_data[i] = 0;
}
}
// Output our riverbank to the display
ledmatrix_update_row(RIVERBANK_ROW, row_display_data);
}
// Redraw the frog in its current position.
void redraw_frog(void) {
if(frog_dead) {
ledmatrix_update_pixel(frog_column, frog_row, COLOUR_DEAD_FROG);
} else {
ledmatrix_update_pixel(frog_column, frog_row, COLOUR_FROG);
}
}