summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOxore <oxore@protonmail.com>2017-12-09 11:18:57 +0300
committerOxore <oxore@protonmail.com>2017-12-09 11:18:57 +0300
commit0f23a894e51fe11cecb6fd762a6360224717912c (patch)
tree5b57e64d4bfb773ea3c82e1535eba3174163d881 /src
parent9e7ca39812cf43df65044c2329ec747060a3d372 (diff)
Extend playfield to 22, change game over condition, shrink fld from 25 to 22
Diffstat (limited to 'src')
-rw-r--r--src/functions.c73
-rw-r--r--src/main.c17
2 files changed, 53 insertions, 37 deletions
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;
}