diff options
| author | not_a_robot <hashimm06@outlook.com> | 2023-11-21 19:25:27 +0000 |
|---|---|---|
| committer | not_a_robot <hashimm06@outlook.com> | 2023-11-21 19:25:27 +0000 |
| commit | 0aeb6c484a3c37fa466003106e490e311adbc699 (patch) | |
| tree | 90ab3b352727931714f5fb4f51bcb3f2e501374b | |
| parent | bee57c12a4582d56f2eb18ea632e416cbffe5528 (diff) | |
| download | retris-0aeb6c484a3c37fa466003106e490e311adbc699.tar.gz retris-0aeb6c484a3c37fa466003106e490e311adbc699.tar.bz2 retris-0aeb6c484a3c37fa466003106e490e311adbc699.zip | |
add initial tetromino generation and control
| -rw-r--r-- | src/retris.c | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/src/retris.c b/src/retris.c index 7335b9e..6779ab8 100644 --- a/src/retris.c +++ b/src/retris.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <time.h> +#include <stdbool.h> #include "raylib.h" /* @@ -15,6 +16,8 @@ * and right, due to how often it polls */ #define FPS 30 +#define CYAN CLITERAL(Color){ 0, 255, 255, 255 } + typedef struct Vector2Int { int x, y; } Vector2Int; @@ -31,37 +34,63 @@ main(void){ int const width = WIDTH * FACT, height = HEIGHT * FACT, vis_height = VIS_HEIGHT * FACT; Color grid[WIDTH][HEIGHT]; // if colour is WHITE, the position is empty - Vector2Int cur_cube = {0,0}; int interval = FPS, count, i, j; + Tetromino cur_trmno; // trnmo is faster and easier to write than tetromino + bool gen_trmno = true; srand(time(NULL)); for (i = 0; i < WIDTH; i++) for (j = 0; j < HEIGHT; j++) grid[i][j] = WHITE; - grid[0][0] = RED; InitWindow(width, height, "retris"); SetTargetFPS(FPS); for (count = 0; !WindowShouldClose(); count++) { - if (count == (interval - (IsKeyDown(KEY_DOWN) ? interval/2 : 0))) { + if (gen_trmno) { + switch (rand() % 1) { + case 0: + cur_trmno.colour = CYAN; + for (i = 0; i < 4; i++) { + cur_trmno.pos[i].x = WIDTH / 2; + // 3 - i so that tetrominoes are listed from bottom up; if drawn + // from top to bottom on line 69, then they will overlap and just + // be 1 cube + cur_trmno.pos[i].y = 3 - i; + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = + cur_trmno.colour; + } + } + gen_trmno = false; + } + + if (count >= (IsKeyDown(KEY_DOWN) ? FPS/30 : interval)) { count = 0; - grid[cur_cube.x][cur_cube.y] = WHITE; - cur_cube.y++; - grid[cur_cube.x][cur_cube.y] = RED; + for (i = 0; i < 4; i++) { + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = WHITE; + cur_trmno.pos[i].y++; + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = cur_trmno.colour; + } } if (IsKeyDown(KEY_RIGHT)) { - grid[cur_cube.x][cur_cube.y] = WHITE; - cur_cube.x++; - grid[cur_cube.x][cur_cube.y] = RED; + for (i = 0; i < 4; i++) { + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = WHITE; + cur_trmno.pos[i].x++; + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = cur_trmno.colour; + } } else if (IsKeyDown(KEY_LEFT)) { - grid[cur_cube.x][cur_cube.y] = WHITE; - cur_cube.x--; - grid[cur_cube.x][cur_cube.y] = RED; + for (i = 0; i < 4; i++) { + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = WHITE; + cur_trmno.pos[i].x--; + grid[cur_trmno.pos[i].x][cur_trmno.pos[i].y] = cur_trmno.colour; + } } + if (IsKeyDown(KEY_G)) + gen_trmno = true; + BeginDrawing(); Draw2DGrid(grid); EndDrawing(); |
