From 0f23a894e51fe11cecb6fd762a6360224717912c Mon Sep 17 00:00:00 2001 From: Oxore Date: Sat, 9 Dec 2017 11:18:57 +0300 Subject: Extend playfield to 22, change game over condition, shrink fld from 25 to 22 --- include/common.h | 4 +-- include/functions.h | 4 ++- src/functions.c | 73 ++++++++++++++++++++++++++++++++++------------------- src/main.c | 17 +++++-------- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/include/common.h b/include/common.h index 4452bf0..49c15d3 100644 --- a/include/common.h +++ b/include/common.h @@ -28,7 +28,7 @@ typedef struct Shape { char c[4][4]; // array of logic shape cells sfRectangleShape *p[4][4]; // array of physical shape cells sfVector2f cSize; // shape rectangles size variable x/y -} Shape; +} Shape; typedef struct Field { sfVector2i pos; @@ -38,7 +38,7 @@ typedef struct Field { int cOutThick; // Field rectangles outline thickness sfVector2f cSize; // shape rectangles size variable x/y sfVector2i size; -} Field; +} Field; typedef struct Window { sfVideoMode mode; diff --git a/include/functions.h b/include/functions.h index 4f35bab..0e6e26a 100644 --- a/include/functions.h +++ b/include/functions.h @@ -8,9 +8,11 @@ void tKeyCtrl(); void initFld(); void tTick(); -void resetActiveShape(); +void resetActiveShape(Shape *active); void putShape(); +int outOfFieldCheck(Field *fld, Shape *active); void checkLevelUp(Game *game); +int cellCollisionCheckHere(Field *fld, Shape *active); int cellCollisionCheck(int dir); int wallCollisionCheck(); int cellRotCollisionCheck(); diff --git a/src/functions.c b/src/functions.c index 98b5890..2ea72ea 100644 --- a/src/functions.c +++ b/src/functions.c @@ -39,14 +39,14 @@ extern char arrShapeT[4][4]; /* Field init routine */ void initFld() { - sfVector2f fldCPos[25][10]; + sfVector2f fldCPos[22][10]; /* Create field */ for (int j = 0; j < fld.size.y; j++) { for (int i = 0; i < fld.size.x; i++) { fld.c[j][i].a = 0; // Inactive = empty - fldCPos[j][i].x + fldCPos[j][i].x = fld.pos.x + (i * (fld.cSize.x + 2 * fld.cOutThick)); - fldCPos[j][i].y + fldCPos[j][i].y = fld.pos.y - (j * (fld.cSize.y + 2 * fld.cOutThick)); fld.p[j][i] = sfRectangleShape_create(); sfRectangleShape_setFillColor(fld.p[j][i], UIBGCOLOR); @@ -69,7 +69,7 @@ void initFld() } } genNextShape(); - resetActiveShape(); + resetActiveShape(&active); } @@ -103,14 +103,14 @@ int linesRmScore() { int k = 0; // "Filled line" indicator int s = 0; - for (int j = 0; j < 20; j++) { + for (int j = 0; j < 22; j++) { for (int i = 0; i < 10; i++) if (fld.c[j][i].a != 0) k++; if (k >= 10) { // If line is full s++; // Give scores for line - for (int n = j; n < 20; n++) { // Drop all lines down - if (n == 19) { + for (int n = j; n < 22; n++) { // Drop all lines down + if (n == 21) { for (int m = 0; m < 10; m++) { fld.c[n][m].a = 0; fld.c[n][m].fColor = UIBGCOLOR; @@ -119,8 +119,7 @@ int linesRmScore() } for (int m = 0; m < 10; m++) { fld.c[n][m].a = fld.c[n+1][m].a; - fld.c[n][m].fColor - = fld.c[n+1][m].fColor; + fld.c[n][m].fColor = fld.c[n+1][m].fColor; } } j--; // Do not let loop to go to next line because @@ -138,6 +137,10 @@ int linesRmScore() */ void putShape() { + if (outOfFieldCheck(&fld, &active)) { + gameover(&game); + return; + } for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) if (active.c[j][i]) { @@ -146,15 +149,23 @@ void putShape() fld.c[j+active.y][i+active.x].fColor = active.fColor; } game.scoreCurrent += linesRmScore()*RM_LINE_SCORE; // Remove filled lines and get score; - for (int i = 0; i < 10; i++) - if (fld.c[20][i].a) { - gameover(&game); - return; - } - resetActiveShape(); + resetActiveShape(&active); checkLevelUp(&game); } +int outOfFieldCheck(Field *fld, Shape *active) +{ + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (active->c[i][j] && active->y+i >= fld->size.y) { + gameover(&game); + return 1; + } + } + } + return 0; +} + void checkLevelUp(Game *game) { if (game->level < 15) @@ -162,18 +173,18 @@ void checkLevelUp(Game *game) game->level++; } -void resetActiveShape() +void resetActiveShape(Shape *active) { genNextShape(); - copyShape(&active); - active.x = 3; - if (active.t == 6) - active.y = 17; + copyShape(active); + active->x = 3; + if (active->t == 6) + active->y = 19; else - active.y = 16; + active->y = 18; for (;;) { - if (cellCollisionCheck(DOWN)) - active.y++; + if (cellCollisionCheckHere(&fld, active)) + active->y++; else break; } @@ -288,6 +299,16 @@ int wallRotCollisionCheck() return 0; // If no conditions are met collision is absent } +int cellCollisionCheckHere(Field *fld, Shape *active) +{ + for (int i = 0; i < 4; i++) + for (int j = 0; j < 4; j++) + if (active->y + i < fld->size.y) + if (active->c[i][j] && fld->c[i+active->y][j+active->x].a) + return 1; + return 0; +} + int cellCollisionCheck(int dir) { for (int j = 0; j < 4; j++) { @@ -450,7 +471,7 @@ void tKeyCtrl() */ void colorizeFld() { - for (int j = 0; j < fld.size.y-5; j++) + for (int j = 0; j < fld.size.y; j++) for (int i = 0; i < fld.size.x; i++) if (fld.c[j][i].a) { sfRectangleShape_setFillColor(fld.p[j][i], fld.c[j][i].fColor); @@ -470,7 +491,7 @@ void colorizeActive() { for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) - if (active.c[j][i] && j+active.y < 20) { + if (active.c[j][i] && j+active.y < 22) { sfRectangleShape_setFillColor( fld.p[j+active.y][i+active.x], active.fColor); @@ -496,7 +517,7 @@ void drawFld(sfRenderWindow *window) void colorizeRandom(Field *fld) { int a; - for (int j = 0; j < fld->size.y-5; j++) { + for (int j = 0; j < fld->size.y; j++) { for (int i = 0; i < fld->size.x; i++) { a = rand()%7+1; switch (a) { diff --git a/src/main.c b/src/main.c index 7608cb1..5b4b931 100644 --- a/src/main.c +++ b/src/main.c @@ -12,8 +12,7 @@ #include "text.h" #include "tet_conf.h" -/* --- Variables --- */ -Window w = {.mode = {450, 520, 32}}; +Window w = {.mode = {450, 570, 32}}; Game game = {.isStarted = 0, .scoreCurrent = 0, .level = 1}; List *texts; sfFont *fontScore; @@ -21,7 +20,6 @@ Shape active, next; Field fld; char arrKeys = 0b00000000; // Arrow keys states byte container -/* --- Variables End --- */ sfClock *gameTick; sfClock *putTick; @@ -47,8 +45,8 @@ void prepare() { */ fld.cSize = (sfVector2f){.x = 23, .y = 23}; //Fld's cell size in pixels fld.cOutThick = 1; - fld.pos = (sfVector2i){.x = 10, .y = 10+500-24}; // Fld bot left corner - fld.size = (sfVector2i){.x = 10, .y = 25}; // Field's size in blocks + fld.pos = (sfVector2i){.x = 10, .y = 10+550-24}; // Fld bot left corner + fld.size = (sfVector2i){.x = 10, .y = 22}; // Field's size in blocks next = (Shape){.x = 250+10+20, .y = 200, .cSize = {.x = 23, .y = 23}}; @@ -106,6 +104,7 @@ void menuLoop() { game.isStarted = 1; freeFld(); initFld(); + sfClock_restart(gameTick); } } @@ -113,11 +112,10 @@ void mainLoop() { while (sfRenderWindow_isOpen(w.window)) { handleWindowEvents(); sfRenderWindow_clear(w.window, UIBGCOLOR); - if (game.isStarted) { + if (game.isStarted) gameLoop(); - } else { + else menuLoop(); - } sfRenderWindow_display(w.window); } } @@ -127,11 +125,8 @@ int main() prepare(); colorizeRandom(&fld); mainLoop(); - /* Just senseless printf */ - printf("%d\n", game.scoreCurrent); freeFld(); sfRenderWindow_destroy(w.window); ListOfText_free(&texts); - return EXIT_SUCCESS; } -- cgit v1.2.3