-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtron.c
535 lines (507 loc) · 13.1 KB
/
tron.c
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
/**
* Tron light cycle game in Consolite C.
* Input is taken from the SNES controllers.
*
* @author Robert Fotino, 2016
*/
/**
* Define global constants.
*/
uint16 true = 1;
uint16 false = 0;
uint16 MIN_PLAYERS = 2;
uint16 MAX_PLAYERS = 8;
uint16 SCREEN_WIDTH = 256;
uint16 SCREEN_HEIGHT = 192;
uint16 DIR_UP = 0;
uint16 DIR_DOWN = 1;
uint16 DIR_LEFT = 2;
uint16 DIR_RIGHT = 3;
/**
* Bitmaps for digits 0-9 in 3x5 format.
* For instance 0x7b6f == 0b111101101101111 ==
* 111
* 101
* 101
* 101
* 111
*/
uint16[10] DIGIT_BITMAPS = { 0x7b6f,
0x2c97,
0x73e7,
0x72cf,
0x5bc9,
0x79cf,
0x79ef,
0x7249,
0x7bef,
0x7bcf };
/**
* Global variables.
*/
uint16 nplayers = MIN_PLAYERS;
uint16[MAX_PLAYERS] player_alive;
uint16[MAX_PLAYERS] player_x;
uint16[MAX_PLAYERS] player_y;
uint16[MAX_PLAYERS] player_dir;
uint16[MAX_PLAYERS] player_color = {
0b00000011,
0b11100000,
0b00011100,
0b11111100,
0b00011111,
0b11100011,
0b10010011,
0b11110010
};
uint16[SCREEN_WIDTH*SCREEN_HEIGHT/4] arena;
// DRAWING FUNCTIONS
/**
* Sets the color register to the closest displayable color based on the given
* RGB values.
*/
void set_color(uint16 r, uint16 g, uint16 b) {
COLOR((r & 0b11100000) | ((g >> 3) & 0b00011100) | ((b >> 6) & 0b00000011));
}
/**
* Fills a rectangle with the given x, y, width, and height, with the color
* already set in the color register.
*/
void draw_rect(uint16 x, uint16 y, uint16 width, uint16 height) {
uint16 i;
uint16 j;
uint16 xmax = x + width;
uint16 ymax = y + height;
for (j = y; j < ymax; j = j + 1) {
for (i = x; i < xmax; i = i + 1) {
PIXEL(i, j);
}
}
}
/**
* Paints the whole screen black (not a quick operation).
*/
void clear_screen() {
COLOR(0);
draw_rect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
}
/**
* Draws a 3x5 bitmap stored in the lower 15 bits of bitmap, starting
* at the given (x, y) and with the given scale factor.
*/
void draw_bitmap_3x5(uint16 bitmap, uint16 x, uint16 y, uint16 scale) {
uint16 i;
uint16 j;
uint16 xmax = x + (3 * scale);
uint16 ymax = y + (5 * scale);
for (j = y; j < ymax; j = j + scale) {
for (i = x; i < xmax; i = i + scale) {
bitmap = bitmap << 1;
if (bitmap & 0x8000) {
draw_rect(i, j, scale, scale);
}
}
}
}
/**
* Draws the given digit d at the (x, y) coords with the given scale
* factor. Uses the current color register.
*/
void draw_digit(uint16 d, uint16 x, uint16 y, uint16 scale) {
draw_bitmap_3x5(DIGIT_BITMAPS[d], x, y, scale);
}
// ARRAY FUNCTIONS
/**
* Fills an array with the given value.
*/
void array_fill(uint16 arr, uint16 val, uint16 len) {
uint16 i;
for (i = 0; i < len; i = i + 1) {
arr[i] = val;
}
}
/**
* Copy an array "src" of length "len" into the array "dest".
*/
void array_copy(uint16 src, uint16 dest, uint16 len) {
uint16 i;
for (i = 0; i < len; i = i + 1) {
dest[i] = src[i];
}
}
/**
* Populates an array with the state of a controller. Controllers have 12
* buttons, so "state" must be an array of at least length 12.
*/
void get_controller_state(uint16 controller_id, uint16 state) {
uint16 i;
for (i = 0; i < 12; i = i + 1) {
// Multiply controller_id by 12
// c*12 = (c*4)+(c*8) = (c<<log2(4))+(c<<log2(8)) = (c<<2)+(c<<3)
state[i] = INPUT(46 + (controller_id << 2) + (controller_id << 3) + i);
}
}
// GAME-SPECIFIC LOGIC
/**
* Draws TRON in big letters on top of the screen
*/
void draw_tron() {
uint16[4] bitmaps = {
0b111010010010010, // T
0b111101111110101, // R
0b111101101101111, // O
0b111101101101101 // N
};
uint16 i;
uint16 j;
// Draw drop shadow/offset thing for 3D effect
set_color(0, 50, 100);
for (j = 0; j < 4; j = j + 1) {
for (i = 0; i < 4; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], 75 + (25 * i) - j, 60 - j, 6);
}
}
// Draw top layer in brighter color
set_color(0, 100, 200);
for (i = 0; i < 4; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], 75 + (25 * i), 60, 6);
}
}
/**
* Erases and redraws the number of players when choosing before
* starting the game.
*/
void draw_nplayers() {
uint16 scale = 4;
uint16 x = 60;
uint16 y = 106;
// Erase
set_color(0, 0, 0);
draw_rect(x, y, 3 * scale, 5 * scale);
// Redraw
set_color(255, 255, 255);
draw_digit(nplayers, x, y, scale);
}
/**
* Draws the string "PLAYERS" after the number of players when
* choosing before the game starts.
*/
void draw_nplayers_str() {
uint16 i;
uint16[7] bitmaps = {
0b111101111100100, // P
0b100100100100111, // L
0b111101111101101, // A
0b101101111010010, // Y
0b111100110100111, // E
0b111101111110101, // R
0b111100111001111 // S
};
for (i = 0; i < 7; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], 90 + (15 * i), 106, 4);
}
}
/**
* Present a prompt for the user to select the number of players
* (between 2 and 8). The controller in the player 1 position must
* be used to select.
*/
uint16 STATE_LENGTH = 12 * MAX_PLAYERS;
void select_nplayers() {
uint16 i;
// Whether the underline is currently blinking or not
uint16 blink_on = false;
// Store the button state of all players' controllers in an array.
// Have current and previous states to prevent double presses.
uint16[STATE_LENGTH] prev_state_all;
uint16[STATE_LENGTH] btn_state_all;
uint16[12] prev_state;
uint16[12] btn_state;
array_fill(prev_state_all, 1, STATE_LENGTH);
array_fill(btn_state_all, 0, STATE_LENGTH);
clear_screen();
// Draw the number of players UI
draw_nplayers();
draw_nplayers_str();
draw_tron();
// Listen for input from any player
while (true) {
prev_state = prev_state_all;
btn_state = btn_state_all;
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
get_controller_state(i, btn_state);
// B pressed, return so we can start the game
if (btn_state[0] && !prev_state[0]) {
return;
}
// Up pressed, increment the number of players
if (btn_state[4] && !prev_state[4]) {
if (nplayers < MAX_PLAYERS) {
nplayers = nplayers + 1;
draw_nplayers();
}
}
// Down pressed, decrement the number of players
if (btn_state[5] && !prev_state[5]) {
if (MIN_PLAYERS < nplayers) {
nplayers = nplayers - 1;
draw_nplayers();
}
}
btn_state = btn_state + 12;
prev_state = prev_state + 12;
}
// Check for blink
if (500 < TIME()) {
TIMERST();
blink_on = !blink_on;
if (blink_on) {
COLOR(255);
} else {
COLOR(0);
}
draw_rect(60, 130, 12, 2);
}
// Copy current button state to previous button state
array_copy(btn_state_all, prev_state_all, STATE_LENGTH);
}
}
/**
* Returns true if only one player is remaining (meaning the game
* is over). Returns false otherwise.
*/
uint16 one_player_remaining() {
uint16 n = 0;
uint16 i;
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
if (player_alive[i]) {
n = n + 1;
}
}
return n <= 1;
}
/**
* Returns the color of the arena pixel at the given (x, y) location.
* Used for collision and redrawing.
*/
uint16 get_color_arena_pixel(uint16 x, uint16 y) {
uint16 lower_bits = x & 0b11;
uint16 arena_index = (y << 6) + (x >> 2);
uint16 val = arena[arena_index];
uint16 packed_id = (val >> (lower_bits << 2)) & 0xf;
if (packed_id) {
return player_color[packed_id - 1];
} else {
return 0;
}
}
/**
* Draws a pixel to the frame buffer and updates the arena array
* for collision purposes.
*/
void color_arena_pixel(uint16 player_id, uint16 x, uint16 y) {
uint16 lower_bits;
uint16 arena_index;
uint16 prev_val;
uint16 packed_id = (player_id + 1) & 0x000f;
// Draw the pixel to the frame buffer
COLOR(player_color[player_id]);
PIXEL(x, y);
// Update the arena. The arena is an array of 16-bit values, but
// we only want to update 4 bits. Use masking depending on the LSBs
// of x
lower_bits = x & 0b11;
arena_index = (y << 6) + (x >> 2);
prev_val = arena[arena_index];
if (0 == lower_bits) {
arena[arena_index] = (prev_val & 0xfff0) | packed_id;
} else if (1 == lower_bits) {
arena[arena_index] = (prev_val & 0xff0f) | (packed_id << 4);
} else if (2 == lower_bits) {
arena[arena_index] = (prev_val & 0xf0ff) | (packed_id << 8);
} else if (3 == lower_bits) {
arena[arena_index] = (prev_val & 0x0fff) | (packed_id << 12);
}
}
/**
* The main game loop, handles user input and the drawing/updating of
* the game.
*/
void game_loop() {
uint16 i;
uint16 dir;
uint16 x;
uint16 y;
uint16[12] btn_state;
while (true) {
TIMERST();
// Move all live players in their current directions
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
if (!player_alive[i]) {
continue;
}
x = player_x[i];
y = player_y[i];
color_arena_pixel(i, x, y);
// Change direction on player input
get_controller_state(i, btn_state);
dir = player_dir[i];
if (btn_state[4] && DIR_DOWN != dir) {
dir = DIR_UP;
} else if (btn_state[5] && DIR_UP != dir) {
dir = DIR_DOWN;
} else if (btn_state[6] && DIR_RIGHT != dir) {
dir = DIR_LEFT;
} else if (btn_state[7] && DIR_LEFT != dir) {
dir = DIR_RIGHT;
}
player_dir[i] = dir;
// Move the player, wrapping around if necessary
if (DIR_UP == dir) {
if (0 == y) {
player_y[i] = SCREEN_HEIGHT - 1;
} else {
player_y[i] = y - 1;
}
} else if (DIR_DOWN == dir) {
if (SCREEN_HEIGHT - 1 == y) {
player_y[i] = 0;
} else {
player_y[i] = y + 1;
}
} else if (DIR_LEFT == dir) {
if (0 == x) {
player_x[i] = SCREEN_WIDTH - 1;
} else {
player_x[i] = x - 1;
}
} else if (DIR_RIGHT == dir) {
if (SCREEN_WIDTH - 1 == x) {
player_x[i] = 0;
} else {
player_x[i] = x + 1;
}
}
// If the player moved into a line, set them to dead
if (get_color_arena_pixel(player_x[i], player_y[i])) {
player_alive[i] = false;
}
}
// If there is one player remaining, the game is over so we can exit
// the game loop
if (one_player_remaining()) {
break;
}
// Wait until this frame is over (16 milliseconds)
while (TIME() < 16);
}
}
/**
* Show the winning player's number on the screen. Return when 1st
* player presses B.
*/
void show_winner() {
uint16 pressed;
uint16[12] btn_state;
uint16[19] bitmaps = {
0b111010010010010, // T
0b101101111101101, // H
0b111100110100111, // E
0b101101111101101, // H
0b111101101101111, // O
0b101101101101111, // U
0b111100111001111, // S
0b111100110100111, // E
0b111101111100100, // P
0b100100100100111, // L
0b111101111101101, // A
0b101101111010010, // Y
0b111100110100111, // E
0b111101111110101, // R
0b101101111111101, // W
0b111010010010111, // I
0b111101101101101, // N
0b111100111001111, // S
0b010010010000010 // !
};
// Get the winner (the player left alive)
uint16 alive = -1;
uint16 i;
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
if (player_alive[i]) {
alive = i;
break;
}
}
COLOR(255);
if (-1 == alive) {
// Draw "THE"
for (i = 0; i < 3; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], 27 + (i * 15), 86, 4);
}
// Draw "HOUSE"
for (i = 3; i < 8; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], 37 + (i * 15), 86, 4);
}
// Draw "WINS!"
for (i = 14; i < 19; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], -44 + (i * 15), 86, 4);
}
} else {
// Draw "PLAYER"
for (i = 8; i < 14; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], -90 + (i * 15), 86, 4);
}
// Draw the player's number
draw_bitmap_3x5(DIGIT_BITMAPS[alive + 1], 132, 86, 4);
// Draw "WINS!"
for (i = 14; i < 19; i = i + 1) {
draw_bitmap_3x5(bitmaps[i], -54 + (i * 15), 86, 4);
}
}
// Wait until a player presses the continue button
pressed = false;
while (!pressed) {
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
get_controller_state(i, btn_state);
if (btn_state[0]) {
pressed = true;
break;
}
}
}
}
/**
* Initializes variables to start the game with the selected number
* of players.
*/
void init() {
uint16 i;
for (i = 0; i < MAX_PLAYERS; i = i + 1) {
player_alive[i] = i < nplayers;
player_x[i] = (i + 1) * SCREEN_WIDTH / (nplayers + 1);
player_y[i] = RND() % 192;
if (player_y[i] < 96) {
player_dir[i] = DIR_DOWN;
} else {
player_dir[i] = DIR_UP;
}
}
for (i = 0; i < SCREEN_WIDTH*SCREEN_HEIGHT/4; i = i + 1) {
arena[i] = 0;
}
}
/**
* The entry point of the program. Have player 1 select the number
* of players, then start the game loop, then show the winner, then
* repeat indefinitely.
*/
void main() {
while (true) {
select_nplayers();
init();
clear_screen();
game_loop();
show_winner();
}
}