mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 14:05:45 +01:00
set next block when placing a new block
This commit is contained in:
@@ -12,6 +12,11 @@
|
||||
#include "tetromino/placing.h"
|
||||
|
||||
|
||||
void set_next_shape(GameData* const game_data) {
|
||||
ShapeId id = (rand() % TETROMINO_COUNT) | (rand() % 4 << 3);
|
||||
game_data->next_shape = id;
|
||||
}
|
||||
|
||||
void game_init(GameData* const game_data) {
|
||||
// zero-initialize the game data
|
||||
*game_data = (GameData){0};
|
||||
@@ -20,7 +25,9 @@ void game_init(GameData* const game_data) {
|
||||
for (uint8_t i = 0; i < ROWS; i++)
|
||||
game_data->row[i] = game_data->row_raw[i];
|
||||
|
||||
game_data->selected = (SelectedShape){TETROMINO_L, 0, 0};
|
||||
set_next_shape(game_data);
|
||||
game_data->selected = (SelectedShape){game_data->next_shape, 0, 0};
|
||||
set_next_shape(game_data);
|
||||
|
||||
// set a random seed using the system clock
|
||||
srand(time(NULL));
|
||||
|
||||
@@ -22,7 +22,9 @@ typedef struct {
|
||||
Colour* row[ROWS]; // stores how to interpert the raw level data
|
||||
Colour row_raw[ROWS][COLUMNS]; // stores the raw level data
|
||||
SelectedShape selected;
|
||||
ShapeId next_shape;
|
||||
} GameData;
|
||||
|
||||
void set_next_shape(GameData* game_data);
|
||||
void game_init(GameData* game_data); // initializes the game
|
||||
void game_update(GameData* game_data, const uint8_t* keys); // updates the game's state
|
||||
|
||||
@@ -102,6 +102,8 @@ void place_update(GameData* const game_data, const InputData move) {
|
||||
if (shape_intersects(game_data->row, selected->id, selected->x, y)) {
|
||||
set_shape(game_data->row, selected->id, selected->x, selected->y); // if the shape intersects vertically, write the shape at the current position and return
|
||||
clear_rows(game_data->row); // clear the rows that have been completed
|
||||
game_data->selected = (SelectedShape){game_data->next_shape, 0, 0};
|
||||
set_next_shape(game_data);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user