172 lines
3.2 KiB
C
172 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <unistd.h>
|
|
|
|
enum {
|
|
HEIGHT = 20,
|
|
WIDTH = 10,
|
|
};
|
|
|
|
struct position {
|
|
int x;
|
|
int y;
|
|
};
|
|
|
|
struct rotation {
|
|
struct position tiles[4];
|
|
};
|
|
|
|
struct brick {
|
|
struct rotation *states;
|
|
int state;
|
|
struct position position;
|
|
};
|
|
|
|
/************************
|
|
* Templates for bricks *
|
|
************************/
|
|
|
|
/* T Brick */
|
|
struct rotation t_brick_rotations[4] = {
|
|
{ /* first rotation */
|
|
.tiles = {
|
|
{.x = 2, .y = 0},
|
|
{.x = 1, .y = 1},
|
|
{.x = 2, .y = 1},
|
|
{.x = 2, .y = 2}
|
|
}
|
|
},
|
|
{ /* second rotation */
|
|
.tiles = {
|
|
{.x = 2, .y = 0},
|
|
{.x = 1, .y = 1},
|
|
{.x = 2, .y = 1},
|
|
{.x = 3, .y = 1}
|
|
}
|
|
},
|
|
{ /* third rotation */
|
|
.tiles = {
|
|
{.x = 2, .y = 0},
|
|
{.x = 2, .y = 1},
|
|
{.x = 3, .y = 1},
|
|
{.x = 2, .y = 2}
|
|
}
|
|
},
|
|
{ /* fourth rotation */
|
|
.tiles = {
|
|
{.x = 1, .y = 1},
|
|
{.x = 2, .y = 1},
|
|
{.x = 3, .y = 1},
|
|
{.x = 2, .y = 2}
|
|
}
|
|
},
|
|
|
|
};
|
|
|
|
struct game_state {
|
|
char running;
|
|
char game_grid[WIDTH][HEIGHT];
|
|
struct brick current_brick;
|
|
};
|
|
|
|
enum input {
|
|
I_QUIT,
|
|
I_LEFT,
|
|
I_RIGHT,
|
|
I_DROP,
|
|
I_ROTATE,
|
|
};
|
|
|
|
struct game_state construct_game_state();
|
|
|
|
struct brick spawn_brick();
|
|
|
|
/* gravitate brick (darf der das?) */
|
|
|
|
/* (optional) drop brick */
|
|
|
|
/* should brick fall? */
|
|
|
|
/* rotate brick */
|
|
|
|
/* check for full line */
|
|
|
|
/* remove full line */
|
|
|
|
/* move brick */
|
|
|
|
/* get input */
|
|
|
|
/* evaluate input */
|
|
|
|
/* brick to grid */
|
|
|
|
/* loose condition */
|
|
|
|
void draw_game_state(struct game_state game_state);
|
|
|
|
int brick_on_position(int x, int y, struct brick brick);
|
|
|
|
int main(void) {
|
|
struct game_state game_state = construct_game_state();
|
|
while (game_state.running) {
|
|
draw_game_state(game_state);
|
|
//get input
|
|
//evaluate input
|
|
//game logic
|
|
game_state.current_brick.position.y++;
|
|
sleep(1);
|
|
}
|
|
}
|
|
|
|
struct game_state construct_game_state() {
|
|
struct game_state game_state = {
|
|
.running = 1,
|
|
.game_grid = {0},
|
|
.current_brick = spawn_brick()
|
|
};
|
|
return game_state;
|
|
}
|
|
|
|
struct brick spawn_brick() {
|
|
struct brick brick = {
|
|
.states = t_brick_rotations,
|
|
.state = 0,
|
|
.position = {.x = 3, .y=0}
|
|
};
|
|
return brick;
|
|
}
|
|
|
|
void draw_game_state(struct game_state game_state) {
|
|
system("clear");
|
|
for (int y = 0; y < HEIGHT; ++y) {
|
|
printf("|");
|
|
for (int x = 0; x < WIDTH; ++x) {
|
|
if (game_state.game_grid[x][y]
|
|
|| brick_on_position(x, y, game_state.current_brick)) {
|
|
printf("O");
|
|
} else {
|
|
printf(" ");
|
|
}
|
|
}
|
|
printf("|\n");
|
|
}
|
|
printf("|");
|
|
for (int x = 0; x < WIDTH; ++x) {
|
|
printf("-");
|
|
}
|
|
printf("|\n");
|
|
}
|
|
|
|
int brick_on_position(int x, int y, struct brick brick) {
|
|
x -= brick.position.x;
|
|
y -= brick.position.y;
|
|
struct rotation *rotation = &brick.states[brick.state];
|
|
for (int i = 0; i < 4; ++i) {
|
|
if (rotation->tiles[i].x == x && rotation->tiles[i].y == y) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|