From ab5a70d3b5769ee1f3e1f1bf380c6839915aaf15 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 23 Apr 2019 23:15:08 -0400 Subject: adjust some stuff from the Tetris guidelines --- core/include/cetris.h | 27 ++++++++++++++------------- core/src/cetris.c | 42 ++++++++++++++++++++++++++++++++++++++---- core/src/matrix.c | 2 +- 3 files changed, 53 insertions(+), 18 deletions(-) (limited to 'core') diff --git a/core/include/cetris.h b/core/include/cetris.h index 1606f2a..4a1c1fa 100644 --- a/core/include/cetris.h +++ b/core/include/cetris.h @@ -6,10 +6,8 @@ #include "types.h" #define CETRIS_BOARD_X 10 -#define CETRIS_BOARD_Y 20 -#define CETRIS_INITIAL_X 3 -#define CETRIS_INITIAL_Y 0 -#define CETRIS_INITIAL_Y_OFFSET 2 +#define CETRIS_BOARD_Y 40 +#define CETRIS_BOARD_VISABLE 20 #define CETRIS_HZ 60 #define CETRIS_DAS_DELAY 11 @@ -17,7 +15,7 @@ #define CETRIS_DROP_PERIOD 2 #define CETRIS_NEXT_PIECE_DELAY 40 #define CETRIS_LINE_CLEAR_DELAY 40 -#define CETRIS_LOCK_DELAY 40 +#define CETRIS_LOCK_DELAY 30 #define CETRIS_WAIT_ON_CLEAR 0 #define CETRIS_STARTING_LEVEL 1 @@ -28,13 +26,13 @@ typedef enum { typedef enum { COLOR_NONE, - COLOR_O, - COLOR_I, - COLOR_S, - COLOR_Z, - COLOR_L, - COLOR_J, - COLOR_T + COLOR_O, // yellow + COLOR_I, // cyan + COLOR_S, // green + COLOR_Z, // red + COLOR_L, // orange + COLOR_J, // blue + COLOR_T // purple } color; typedef enum { @@ -72,6 +70,8 @@ typedef struct { /* current tetrimino */ tetrimino current; + tetrimino held; + bool piece_held; u8 current_index; /* input_manager */ @@ -107,5 +107,6 @@ void lock_current(cetris_game* g); void init_game(cetris_game* g); void update_game_tick(cetris_game* g); -void move_piece(cetris_game* g, input_t move); +void move_piece(cetris_game* g, input_t move, bool hold); +void hold_piece(cetris_game* g); void stop_holding(cetris_game* g, input_t move); diff --git a/core/src/cetris.c b/core/src/cetris.c index 4e404d6..d8eda2f 100644 --- a/core/src/cetris.c +++ b/core/src/cetris.c @@ -21,8 +21,10 @@ static void add_score(cetris_game* g, u8 lines); /* LEVEL DROP SPEED VALUES */ +// https://tetris.fandom.com/wiki/Tetris_Worlds +// TODO: Make this more accurate static const u32 level_drop_delay[20] = { - 48, 43, 38, 33, 28, 23, 18, 13, 8, 6, 5, 5, 5, 4, 4, 4, 3, 3, 3, 3 + 60, 48, 37, 28, 21, 16, 11, 8, 6, 4, 3, 2, 1, 1, 1, 1, 1, 1, 1, 1 }; /* GAME FUNCTIONS */ @@ -57,7 +59,12 @@ void init_piece_queue(cetris_game* g) { g->piece_queue[i].lock_tick = 0; g->piece_queue[i].locked = false; g->piece_queue[i].ghost_y = 0; - g->piece_queue[i].pos = (vec2){CETRIS_INITIAL_X, CETRIS_INITIAL_Y - CETRIS_INITIAL_Y_OFFSET}; + + /* Pieces should spawn so that on the first down + * tick the bottom row will show. Values here are adjusted + * for the default 4x4 matricies for each piece */ + g->piece_queue[i].pos.x = 3; + g->piece_queue[i].pos.y = (i == I) ? 17 : 16; } } @@ -123,6 +130,10 @@ void next_piece(cetris_game* g) { } g->current_index++; + if (!g->game_over) { + move_current(g, DOWN); + } + if (g->current_index >= 7) { g->current_index = 0; shuffle_queue(g); @@ -234,7 +245,7 @@ void add_score(cetris_game* g, u8 lines) { /* MOVEMENT FUNCTIONS */ -void move_piece(cetris_game* g, input_t move) { +void move_piece(cetris_game* g, input_t move, bool hold) { if (!g->held_moves[move]) { switch (move) { case LEFT: @@ -254,7 +265,7 @@ void move_piece(cetris_game* g, input_t move) { break; } } - g->held_moves[move] = true; + if (hold) g->held_moves[move] = true; } void stop_holding(cetris_game* g, input_t move) { @@ -264,3 +275,26 @@ void stop_holding(cetris_game* g, input_t move) { g->das_repeat = 0; } else if (move == USER_DOWN) g->down_move_tick = 0; } + +void reset_tetrimino(tetrimino* t) { + t->r = INIT; + t->pos.x = 3; + t->pos.y = (t->t == I) ? 17 : 16; + t->ghost_y = 0; +} + +void hold_piece(cetris_game* g) { + if (g->piece_held) { + tetrimino tmp = g->current; + g->current = g->held; + g->held = tmp; + } else { + g->held = g->current; + reset_tetrimino(&g->held); + g->piece_held = true; + next_piece(g); + } + update_board(g); +} + + diff --git a/core/src/matrix.c b/core/src/matrix.c index caae107..d4597a7 100644 --- a/core/src/matrix.c +++ b/core/src/matrix.c @@ -255,7 +255,7 @@ void rotate_matrix(cetris_game* g, bool clockwise) { /* check for tspin */ if (g->current.t == T) { bool did_tspin = true; - for (int i = 1; i < 5; i++) { + for (u8 i = 1; i < 5; i++) { g->current.pos.x += basic_movements[i].x; g->current.pos.y += basic_movements[i].y; -- cgit v1.2.3-101-g0448