diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | src/functions.c | 84 | ||||
-rw-r--r-- | src/include/common.h | 2 | ||||
-rw-r--r-- | src/include/functions.h | 1 | ||||
-rw-r--r-- | src/main.c | 42 |
5 files changed, 85 insertions, 47 deletions
@@ -8,11 +8,10 @@ __Compilation__ ``` make all ``` -File named "tetris" is the final binary. Run it. Hit *Return* to start game. +File named "tetris" is the final binary. Run it. Hit *S* to start game. __TODO:__ -- "Game Over" with restart - Levels and scores system with table of records - Three next shapes - Main menu with beauties diff --git a/src/functions.c b/src/functions.c index b4d08ea..bd6856d 100644 --- a/src/functions.c +++ b/src/functions.c @@ -3,8 +3,8 @@ /* Externs from main.c */ -extern sfRectangleShape *fld[20][10]; // Array of field rectangles -extern sfVector2f fldCPos[20][10]; // Array of absolute coordinates of field rectangles +extern sfRectangleShape *fld[25][10]; // Array of field rectangles +extern sfVector2f fldCPos[25][10]; // Array of absolute coordinates of field rectangles extern sfVector2i fldSize; extern sfVector2i fldPos; extern sfVector2f fldCSize; // Field rectangles size variable x/y @@ -12,7 +12,7 @@ extern int fldCOutThick; // Field rectangles outline thickness extern sfVector2f textScore_pos; extern sfText *textScore; -extern sfFont *font; +extern sfFont *fontScore; extern int gameIsStarted; extern uint8_t arrKeys; // arrow keys states byte container @@ -52,6 +52,18 @@ extern uint8_t arrShapeT[4][4]; */ void initAll() { + fontScore = sfFont_createFromFile("dat/arial.ttf"); + if (!fontScore) { + printf("dat/arial.ttf font load failed"); + exit(-1); + } + + textScore_pos.x = 250+10+10; + textScore_pos.y = 485; + textScore = sfText_create(); + sfText_setFont(textScore, fontScore); + sfText_setCharacterSize(textScore, 20); + sfText_setPosition(textScore, textScore_pos); /* * Dimensions of every fld's block @@ -64,12 +76,11 @@ void initAll() fldPos.x = 10; // Field's bottom left corner coordinates fldPos.y = 10+500-25; fldSize.x = 10; // Field size in blocks - fldSize.y = 20; + fldSize.y = 25; srand( time(NULL) ); gameTick = sfClock_create(); mTick = sfClock_create(); - resetActiveShape(); /* Create field */ for (int j = 0; j < fldSize.y; j++) { @@ -89,17 +100,7 @@ void initAll() fldCOutThick); } } - font = sfFont_createFromFile("dat/arial.ttf"); - if (!font) { - printf("dat/arial.ttf font load failed"); - exit(-1); - } - textScore_pos.x = 250+10+10; - textScore_pos.y = 485; - textScore = sfText_create(); - sfText_setFont(textScore, font); - sfText_setCharacterSize(textScore, 20); - sfText_setPosition(textScore, textScore_pos); + resetActiveShape(); } /* @@ -178,14 +179,17 @@ void putShape() } scoreCurrent += linesRmScore()*100; // Remove filled lines and // get score; + for (int i = 0; i < 10; i++) + if (fldCAtt[20][i].a) { + gameover(); + return; + } resetActiveShape(); } void resetActiveShape() { - actiSh.x = 3; - actiSh.y = 16; - actiSh.t = (rand()%7)+1; // Insert new random shape of 7 variants + actiSh.t = (rand()%7)+1; // Insert new random shape of 7 variants switch (actiSh.t) { // Copy cell active/inactive state case 1 : memcpy(&actiSh.c[0][0], @@ -230,6 +234,17 @@ void resetActiveShape() actiSh.fColor = tMagneta; break; } + actiSh.x = 3; + if (actiSh.t == 6) + actiSh.y = 17; + else + actiSh.y = 16; + for (;;) { + if (cellCollisionCheck(0b0010)) + actiSh.y++; + else + break; + } } /* @@ -241,8 +256,8 @@ void tTick() if (sfClock_getElapsedTime(gameTick).microseconds >= lvlLatency) { sfClock_restart(gameTick); // Restart gameTick /* if bottom not reached */ - if ((wallCollisionCheck(0b0010) == 0) && - (cellCollisionCheck(0b0010) == 0)) + if ((wallCollisionCheck(0b0010) == 0) + && (cellCollisionCheck(0b0010) == 0)) actiSh.y--; // Move else putShape(); // Just put the shape @@ -308,16 +323,15 @@ void rotateShape() int cellRotCollisionCheck() { - for (int j = 0; j < 4; j++) { - for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) + for (int i = 0; i < 4; i++) if (actiSh.c[j][i] && fldCAtt[j+actiSh.y][i+actiSh.x].a) return 1; // Collision happens - } - } return 0; // Else it do not } + int wallRotCollisionCheck() { if(actiSh.y < 0) //If shape has crossed Bottom border @@ -335,6 +349,7 @@ int wallRotCollisionCheck() return 0; // If no conditions are met collision is absent } + int cellCollisionCheck(int dir) { for (int j = 0; j < 4; j++) { @@ -491,8 +506,8 @@ void tKeyCtrl() */ void colorizeFld() { - for(int j = 0; j < 20; j++) { - for(int i = 0; i < 10; i++) { + for(int j = 0; j < fldSize.y-5; j++) { + for(int i = 0; i < fldSize.x; i++) { if (fldCAtt[j][i].a) { sfRectangleShape_setFillColor( fld[j][i], @@ -522,7 +537,7 @@ void colorizeActiSh() { for(int j = 0; j < 4; j++) { for(int i = 0; i < 4; i++) { - if (actiSh.c[j][i]) { + if (actiSh.c[j][i] && j+actiSh.y < 20) { sfRectangleShape_setFillColor( fld[j+actiSh.y][i+actiSh.x], actiSh.fColor); @@ -537,7 +552,7 @@ void colorizeActiSh() void menuTick() { - if(sfClock_getElapsedTime(mTick).microseconds >= lvlLatency){ + if(sfClock_getElapsedTime(mTick).microseconds >= lvlLatency) { sfClock_restart(mTick); colorizeRandom(); } @@ -546,11 +561,11 @@ void menuTick() void colorizeRandom() { - for (int j = 0; j < fldSize.y; j++) { + int a; + for (int j = 0; j < fldSize.y-5; j++) { for (int i = 0; i < fldSize.x; i++) { - int a; a = rand()%7+1; - switch (a) {; + switch (a) { case 1 : sfRectangleShape_setFillColor(fld[j][i], tOrange); @@ -584,3 +599,8 @@ void colorizeRandom() } } } + +void gameover() +{ + gameIsStarted = 0; +} diff --git a/src/include/common.h b/src/include/common.h index 4db5941..b61cc5f 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -17,7 +17,7 @@ struct tCell { uint8_t a; // active/empty state of cell sfColor fColor; // fill color }; -struct tCell fldCAtt[20][10];// fld cells attributes +struct tCell fldCAtt[25][10];// fld cells attributes /* diff --git a/src/include/functions.h b/src/include/functions.h index cb5bf09..251e230 100644 --- a/src/include/functions.h +++ b/src/include/functions.h @@ -20,5 +20,6 @@ void colorizeFld(); void colorizeActiSh(); void menuTick(); void colorizeRandom(); +void gameover(); #endif @@ -6,23 +6,27 @@ int gameIsStarted = 0; sfVideoMode mode = {450, 520, 32}; sfRenderWindow* window; sfEvent event; -sfText* textScore; -sfFont* font; + +sfText *textMenu1; +sfVector2f textMenu1_pos; + +sfFont *fontScore; +sfText *textScore; +sfVector2f textScore_pos; char *scoreDisp; +int scoreCurrent = 0; -sfRectangleShape* fld[20][10];// Array of fld rectangles -sfVector2f fldCPos[20][10]; // Array of absolute coordinates of fld +sfRectangleShape* fld[25][10];// Array of fld rectangles +sfVector2f fldCPos[25][10]; // Array of absolute coordinates of fld // rectangles - int fldCOutThick = 1; // Field rectangles outline thickness -sfVector2f fldCSize; // Field rectangles size variable x/y -sfVector2i fldSize, fldPos; -sfVector2f textScore_pos; +sfVector2f fldCSize; // Field rectangles size variable x/y +sfVector2i fldSize; +sfVector2i fldPos; uint8_t arrKeys = 0b00000000; // Arrow keys states byte container int lvlLatency = 500000; -int scoreCurrent = 0; /* --- Variables End --- */ int main() @@ -30,12 +34,25 @@ int main() initAll(); + /* + * Menu texts + * + */ + textMenu1_pos.x = 10+250+30; + textMenu1_pos.y = 100; + textMenu1 = sfText_create(); + sfText_setFont(textMenu1, fontScore); + sfText_setCharacterSize(textMenu1, 36); + sfText_setPosition(textMenu1, textMenu1_pos); + char b[7]; + sprintf(b, "TETRIS"); + sfText_setString(textMenu1, (char *)&b); + /* Create main window */ window = sfRenderWindow_create(mode, windowName_conf, sfResize | sfClose, - NULL - ); + NULL); if (!window) return EXIT_FAILURE; @@ -77,7 +94,6 @@ int main() sfRenderWindow_drawText(window, textScore, NULL); } else { menuTick(); - /* Draw all fld cells */ for (int j=0; j < fldSize.y; j++){ for(int i = 0; i < fldSize.x; i++){ @@ -87,6 +103,8 @@ int main() NULL); } } + sfRenderWindow_drawText(window, textMenu1, NULL); + if (sfKeyboard_isKeyPressed(sfKeyS) == 1) { gameIsStarted = 1; initAll(); |