summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inc/tetromino.h5
-rw-r--r--src/main.c114
2 files changed, 100 insertions, 19 deletions
diff --git a/inc/tetromino.h b/inc/tetromino.h
index ebe5930..45c335b 100644
--- a/inc/tetromino.h
+++ b/inc/tetromino.h
@@ -5,7 +5,8 @@ enum TetrominoTypes {
O,
S,
T,
- Z
+ Z,
+ TETROMINO_COUNT
};
enum Direction {
@@ -21,7 +22,7 @@ struct Tetromino {
int directions[4][4][4];
};
-struct Tetromino Tetrominos[7] = {
+struct Tetromino tetrominos[7] = {
[I] = {
.size = 4,
.color = SKYBLUE,
diff --git a/src/main.c b/src/main.c
index 1761efe..66a222c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,7 @@
#include "raylib.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
@@ -8,45 +11,127 @@
#define BLOCK_SIZE 25
+#define FALL_TICK 0.5
+
+enum Phases {
+ GENERATION,
+ FALL,
+ LOCK,
+ PATTERN,
+ MARK,
+ ITERATE,
+ ANIMATE,
+ ELIMINATE,
+ COMPLETE
+} game_phase;
+
#include "tetromino.h"
+struct {
+ int pos_x;
+ int pos_y;
+ int rotation;
+ int type;
+} current_piece;
+
struct Color matrix[MATRIX_WIDTH][MATRIX_HEIGHT];
+int piece_queue[2][TETROMINO_COUNT] = {
+ {0, 1, 2, 3, 4, 5, 6},
+ {0, 1, 2, 3, 4, 5, 6}
+};
+int current_bag = 0;
+int current_piece_index = 0;
void render_matrix(void);
+void make_bag(int *bag);
void render_matrix(void)
{
- /* top left corner */
+ /* top left matrix corner */
int matrix_start_x =
(SCREEN_WIDTH/2 - (MATRIX_WIDTH * BLOCK_SIZE)) / 2;
int matrix_start_y =
(SCREEN_HEIGHT - (3 * MATRIX_HEIGHT / 2 * BLOCK_SIZE)) / 2;
+ /* top left queue corner */
+ int queue_start_x =
+ (SCREEN_WIDTH/2 + BLOCK_SIZE);
+ int queue_start_y =
+ (SCREEN_HEIGHT - (3 * MATRIX_HEIGHT / 2 * BLOCK_SIZE)) / 2;
+
+
/* draw matrix background */
DrawRectangle(
- matrix_start_x - 1
- ,matrix_start_y + (MATRIX_HEIGHT/2 * BLOCK_SIZE) - 1
- ,MATRIX_WIDTH * BLOCK_SIZE + 2
- ,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
- ,GRAY
+ matrix_start_x - 1
+ ,matrix_start_y + (MATRIX_HEIGHT/2 * BLOCK_SIZE) - 1
+ ,MATRIX_WIDTH * BLOCK_SIZE + 2
+ ,MATRIX_HEIGHT/2 * BLOCK_SIZE + 2
+ ,GRAY
);
/* draw blocks from matrix */
for (int y = 0; y < 40; y++) {
for (int x = 0; x < 10; x++) {
DrawRectangle(
- (x * BLOCK_SIZE) + matrix_start_x + 1
- ,(y * BLOCK_SIZE) + matrix_start_y + 1
+ (x * BLOCK_SIZE) + matrix_start_x + 1
+ ,(y * BLOCK_SIZE) + matrix_start_y + 1
+ ,BLOCK_SIZE - 2
+ ,BLOCK_SIZE - 2
+ ,matrix[x][y]
+ );
+ }
+ }
+
+ /* draw current piece */
+ for (int i = 0; i < tetrominos[current_piece.type].size; i++) {
+ for (int j = 0; j < tetrominos[current_piece.type].size; j++) {
+ if (tetrominos[current_piece.type].directions[current_piece.rotation][i][j]) {
+ DrawRectangle(
+ matrix_start_x + ((current_piece.pos_x + j-2) * BLOCK_SIZE) + 1
+ ,matrix_start_y + ((current_piece.pos_y + i-2) * BLOCK_SIZE) + 1
,BLOCK_SIZE - 2
,BLOCK_SIZE - 2
- ,matrix[x][y]
- );
+ ,tetrominos[current_piece.type].color
+ );
+ }
}
}
+
+ // draw queue
+ // STUB
+ (void)queue_start_x;
+ (void)queue_start_y;
+}
+
+void shuffle_bag(int *bag)
+{
+ for (int i = 0; i < TETROMINO_COUNT; i++) {
+ int r = i + rand() / (RAND_MAX / (TETROMINO_COUNT - i) + 1);
+ int t = bag[r];
+ bag[r] = bag[i];
+ bag[i] = t;
+ }
+}
+
+void generation_phase(void)
+{
+ // bag generation
+ if (current_piece_index > TETROMINO_COUNT) {
+ current_piece_index = 0;
+ shuffle_bag(piece_queue[current_bag]);
+ current_bag = !current_bag;
+ }
+
+ // set current piece
+ current_piece.type = piece_queue[current_bag][current_piece_index++];
+ current_piece.rotation = NORTH;
+ current_piece.pos_x = 5;
+ current_piece.pos_y = 20;
}
int main(void)
{
+ //srand(time(NULL));
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "~turnipGod's Tetris");
// set all blocks to white
@@ -56,13 +141,8 @@ int main(void)
}
}
- // set playarea blocks to purple
- for (int y = 20; y < 40; y++) {
- for (int x = 0; x < 10; x++) {
- matrix[x][y] = PURPLE;
- }
- }
-
+ shuffle_bag(piece_queue[0]);
+ generation_phase();
while (!WindowShouldClose()) {
BeginDrawing();
ClearBackground(WHITE);