mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
rework random shape selection
This commit is contained in:
@@ -13,9 +13,24 @@
|
|||||||
#include "tetromino/placing.h"
|
#include "tetromino/placing.h"
|
||||||
|
|
||||||
|
|
||||||
void set_next_shape(GameData* const game_data) {
|
void next_shape(GameData* const game_data) {
|
||||||
ShapeId id = (rand() % TETROMINO_COUNT) | (rand() % 4 << 3);
|
game_data->curr_idx++; // increase the current shape index
|
||||||
game_data->next_shape = id;
|
game_data->sel_x = COLUMNS / 2 - SHAPE_WIDTH / 2; // move the shape position to the centre
|
||||||
|
game_data->sel_y = 0;
|
||||||
|
|
||||||
|
// return if know which shape is next
|
||||||
|
if (game_data->curr_idx < (TETROMINO_COUNT - 1))
|
||||||
|
return;
|
||||||
|
|
||||||
|
game_data->curr_idx = 0;
|
||||||
|
|
||||||
|
// shuffle the next shapes using a Fisher–Yates shuffle
|
||||||
|
for (uint8_t i = 0; i < (TETROMINO_COUNT - 1); i++) {
|
||||||
|
const uint8_t j = i + rand() % (TETROMINO_COUNT - i);
|
||||||
|
const ShapeId cache = game_data->nxt[i];
|
||||||
|
game_data->nxt[i] = game_data->nxt[j];
|
||||||
|
game_data->nxt[j] = cache;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_init(GameData* const game_data) {
|
void game_init(GameData* const game_data) {
|
||||||
@@ -25,15 +40,18 @@ void game_init(GameData* const game_data) {
|
|||||||
// allocate size for each row
|
// allocate size for each row
|
||||||
for (int8_t i = 0; i < ROWS; i++) {
|
for (int8_t i = 0; i < ROWS; i++) {
|
||||||
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour));
|
game_data->rows[i] = calloc(COLUMNS, sizeof(Colour));
|
||||||
//game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging
|
// game_data->rows[i][0] = (Colour){(uint8_t)((((i + 1) ^ ((i + 1) >> 3)) * 0x27) & 0xFF)}; // for debugging
|
||||||
}
|
}
|
||||||
|
|
||||||
// set a random seed using the system clock
|
// set a random seed using the system clock
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
set_next_shape(game_data);
|
// set the shape data in each slot to it's corrsponding ID
|
||||||
game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0};
|
for (ShapeId i = 0; i < TETROMINO_COUNT; i++)
|
||||||
set_next_shape(game_data);
|
game_data->nxt[i] = i;
|
||||||
|
|
||||||
|
game_data->curr_idx = ~1; // set the current index to the max (well, so it becomes the max after increment)
|
||||||
|
next_shape(game_data); // select the next shape (a refresh is triggered due to the current index being too high)
|
||||||
}
|
}
|
||||||
|
|
||||||
// called every time the game's state is updated
|
// called every time the game's state is updated
|
||||||
|
|||||||
@@ -9,22 +9,18 @@
|
|||||||
#define COLUMNS ((int8_t)10)
|
#define COLUMNS ((int8_t)10)
|
||||||
#define ROWS ((int8_t)(COLUMNS * 2))
|
#define ROWS ((int8_t)(COLUMNS * 2))
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
ShapeId id;
|
|
||||||
int8_t x;
|
|
||||||
int8_t y;
|
|
||||||
} SelectedShape;
|
|
||||||
|
|
||||||
typedef const Colour* CRow;
|
typedef const Colour* CRow;
|
||||||
typedef Colour* Row;
|
typedef Colour* Row;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Row rows[ROWS];
|
Row rows[ROWS];
|
||||||
SelectedShape selected;
|
ShapeId nxt[7]; // the order of the shape ids that they should appear in
|
||||||
ShapeId next_shape;
|
uint8_t curr_idx; // current shape index
|
||||||
|
int8_t sel_x; // selected shape x position
|
||||||
|
int8_t sel_y; // selected shape y position
|
||||||
} GameData;
|
} GameData;
|
||||||
|
|
||||||
void set_next_shape(GameData* game_data);
|
void next_shape(GameData* game_data);
|
||||||
void game_init(GameData* game_data); // initializes the game
|
void game_init(GameData* game_data); // initializes the game
|
||||||
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state
|
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state
|
||||||
void game_free(GameData* game_data); // free all data stored with the game
|
void game_free(GameData* game_data); // free all data stored with the game
|
||||||
|
|||||||
@@ -97,36 +97,39 @@ static inline ShapeId rotate_id(const ShapeId id, const int dir) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void place_update(GameData* const game_data, const InputData move) {
|
void place_update(GameData* const game_data, const InputData move) {
|
||||||
SelectedShape* const selected = &game_data->selected;
|
// store the current index, only changes when placed (which yields no movement) and rotation (which occurs last)
|
||||||
|
ShapeId const curr_idx = game_data->curr_idx;
|
||||||
|
|
||||||
// set the shape if we moved vertically and intersected
|
// set the shape if we moved vertically and intersected
|
||||||
if (move & 4) {
|
if (move & 4) {
|
||||||
const int8_t y = selected->y + 1;
|
const int8_t y = game_data->sel_y + 1;
|
||||||
if (shape_intersects(game_data->rows, selected->id, selected->x, y)) {
|
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, y)) {
|
||||||
set_shape(game_data->rows, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return
|
set_shape(game_data->rows, game_data->nxt[curr_idx], game_data->sel_x, game_data->sel_y); // if the shape intersects vertically, write the shape at the current position and return
|
||||||
clear_rows(game_data->rows); // clear the rows that have been completed
|
clear_rows(game_data->rows); // clear the rows that have been completed
|
||||||
|
|
||||||
game_data->selected = (SelectedShape){game_data->next_shape, COLUMNS / 2 - SHAPE_WIDTH / 2, 0};
|
next_shape(game_data);
|
||||||
set_next_shape(game_data);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, just set Y
|
// otherwise, just set Y
|
||||||
selected->y = y;
|
game_data->sel_y = y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// update shape's X coordinate movement
|
// update shape's X coordinate movement
|
||||||
if ((move & 3) != 3 && (move & 3)) {
|
if ((move & 3) != 3 && (move & 3)) {
|
||||||
const int8_t x = selected->x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
|
const int8_t x = game_data->sel_x + ((move & 3) == 1 ? -1 : 1); // either move along -x or +x
|
||||||
if (shape_intersects(game_data->rows, selected->id, x, selected->y) == false) {
|
if (shape_intersects(game_data->rows, game_data->nxt[curr_idx], x, game_data->sel_y) == false) {
|
||||||
selected->x = x; // set X if the shape does not intersect
|
game_data->sel_x = x; // set X if the shape does not intersect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update the shape's rotation
|
// update the shape's rotation
|
||||||
if (move & 8 || move & 16) {
|
if (move & 8 || move & 16) {
|
||||||
const ShapeId id = move & 8 ? rotate_id(selected->id, -8) : rotate_id(selected->id, 8);
|
const ShapeId id = move & 8 // check which direction we should move
|
||||||
if (shape_intersects(game_data->rows, id, selected->x, selected->y) == false) {
|
? rotate_id(game_data->nxt[curr_idx], -8)
|
||||||
selected->id = id;
|
: rotate_id(game_data->nxt[curr_idx], 8);
|
||||||
|
if (shape_intersects(game_data->rows, id, game_data->sel_x, game_data->sel_y) == false) {
|
||||||
|
game_data->nxt[curr_idx] = id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,19 +44,20 @@ static inline void set_colour(SDL_Renderer* renderer, Colour c) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// draw the selected block
|
// draw the selected block
|
||||||
static void render_selected(SDL_Renderer* renderer, SelectedShape selected) {
|
static void render_selected(SDL_Renderer* renderer, GameData* const game_data) {
|
||||||
Shape selected_shape = shape_from_id(selected.id);
|
const ShapeId id = game_data->nxt[game_data->curr_idx];
|
||||||
set_colour(renderer, colour_from_id(selected.id));
|
const Shape shape = shape_from_id(id);
|
||||||
|
set_colour(renderer, colour_from_id(id));
|
||||||
|
|
||||||
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
for (int8_t y = 0; y < SHAPE_HEIGHT; y++) {
|
||||||
ShapeRow shape_row = shape_get_row(selected_shape, y);
|
const ShapeRow shape_row = shape_get_row(shape, y);
|
||||||
|
|
||||||
if (shape_row == 0)
|
if (shape_row == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
for (int8_t x = 0; x < SHAPE_WIDTH; x++)
|
for (int8_t x = 0; x < SHAPE_WIDTH; x++)
|
||||||
if (is_set(shape_row, x))
|
if (is_set(shape_row, x))
|
||||||
draw_block(renderer, selected.x + x, selected.y + y);
|
draw_block(renderer, game_data->sel_x + x, game_data->sel_y + y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,7 +84,7 @@ void renderer_update(const RenderData* render_data) {
|
|||||||
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
success |= SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
||||||
success |= SDL_RenderClear(renderer);
|
success |= SDL_RenderClear(renderer);
|
||||||
|
|
||||||
render_selected(renderer, render_data->game_data->selected);
|
render_selected(renderer, render_data->game_data);
|
||||||
render_level(renderer, render_data->game_data);
|
render_level(renderer, render_data->game_data);
|
||||||
|
|
||||||
if (success < 0) {
|
if (success < 0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user