some more config options for tron

This commit is contained in:
uosfz 2026-01-30 18:32:07 +01:00
parent 5ac26307a8
commit 643a25d85f
Signed by: uosfz
SSH key fingerprint: SHA256:FlktuluyhTQg3jHZNLKwxOOC5hbfrUXM0tz3IA3lGJo

View file

@ -22,16 +22,12 @@ enum dir {
#define FIELD_STATE_FREE 255
#define FIELD_STATE_CRASH 254
enum tron_button {
TRON_BUTTON_P1_LEFT = 0,
TRON_BUTTON_P1_RIGHT,
TRON_BUTTON_P2_LEFT,
TRON_BUTTON_P2_RIGHT,
TRON_BUTTON_P3_LEFT,
TRON_BUTTON_P3_RIGHT,
TRON_BUTTON_P4_LEFT,
TRON_BUTTON_P4_RIGHT,
};
#define COLOR_P0 (COLOR_RGB(255, 0, 0))
#define COLOR_P1 (COLOR_RGB(0, 0, 255))
#define COLOR_P2 (COLOR_RGB(0, 255, 0))
#define COLOR_P3 (COLOR_RGB(255, 255, 0))
#define COLOR_CRASH (COLOR_LUMA(255))
#define COLOR_FREE (COLOR_LUMA(20))
// max 4 players
struct tron_game {
@ -52,11 +48,13 @@ struct tron_game {
uint8_t field[];
};
#define TRON_TICK_LENGTH ((Time){ 0, NSEC_PER_SEC/10 })
void tron_reset_timer(struct tron_game *game) {
void tron_step_callback(Timer *timer, void *arg);
Time time = time_current();
time = time_add(time, (Time){ 0, NSEC_PER_SEC/10 });
time = time_add(time, TRON_TICK_LENGTH);
timer_set_absolute(&game->timer, time, tron_step_callback, game);
}
@ -110,12 +108,12 @@ static void draw_tile(struct tron_game *game, uint32_t x, uint32_t y) {
uint8_t state = *field_at_asserted(game, x, y);
struct color col;
switch (state) {
case 0: col = COLOR_RGB(255, 0, 0); break;
case 1: col = COLOR_RGB(0, 0, 255); break;
case 2: col = COLOR_RGB(0, 255, 0); break;
case 3: col = COLOR_RGB(255, 255, 0); break;
case FIELD_STATE_CRASH: col = COLOR_LUMA(20); break;
case FIELD_STATE_FREE: col = COLOR_LUMA(50); break;
case 0: col = COLOR_P0; break;
case 1: col = COLOR_P1; break;
case 2: col = COLOR_P2; break;
case 3: col = COLOR_P3; break;
case FIELD_STATE_CRASH: col = COLOR_CRASH; break;
case FIELD_STATE_FREE: col = COLOR_FREE; break;
default: UNREACHABLE();
}
size_t mult = game->size_mult;
@ -136,7 +134,7 @@ void tron_init(void *self) {
game->winner = -1;
memset(game->buttons, 0, sizeof(game->buttons));
memset(game->field, FIELD_STATE_FREE, game->field_width * game->field_height);
app_draw_rect(self, COLOR_LUMA(50), NULL);
app_draw_rect(self, COLOR_FREE, NULL);
switch (game->num_players) {
case 4:
player_init(game, 3,
@ -167,26 +165,8 @@ void tron_init(void *self) {
tron_reset_timer(game);
}
static void print_field(struct tron_game *game) {
for (uint32_t y = 0; y < game->field_height; y++) {
for (uint32_t x = 0; x < game->field_width; x++) {
char c;
switch (*field_at_asserted(game, x, y)) {
case 0: c = '0'; break;
case 1: c = '1'; break;
case 2: c = '2'; break;
case 3: c = '3'; break;
case FIELD_STATE_CRASH: c = 'X'; break;
case FIELD_STATE_FREE: c = '_'; break;
default: UNREACHABLE();
}
printf("%c", c);
}
printf("\n");
}
}
void tron_step_callback(Timer *timer, void *arg) {
(void)timer;
struct tron_game *game = arg;
for (size_t idx = 0; idx < game->num_players; idx++) {
struct player *p = &game->players[idx];