feat(game): add timer functionality to gameplay

This commit is contained in:
2025-09-23 03:08:34 +03:30
parent 6d101c9730
commit 1ea3f01414
+11 -1
View File
@@ -11,6 +11,7 @@ const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int ntail;
int timer; // Variable to store elapsed time in seconds
enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;
@@ -22,6 +23,7 @@ void setup() {
fruitX = rand() % width;
fruitY = rand() % height;
score = 0;
timer = 0; // Initialize timer
}
void draw() {
@@ -50,6 +52,7 @@ void draw() {
for(int i = 0; i < width + 2; i++) cout << "#";
cout << "\n";
cout << "score:" << score << "\n";
cout << "Time: " << timer << "s\n"; // Display timer
}
@@ -93,7 +96,7 @@ void logic() {
tailX[i] = prevX;
tailY[i] = prevY;
prevX = prev2X;
prevY = prev2Y;
prevY = prev2Y;
}
switch(dir){
@@ -130,11 +133,18 @@ void logic() {
int main() {
system("cls");
setup();
int tickCounter = 0;
while(!gameOver){
draw();
input();
logic();
Sleep(100);
tickCounter++;
if (tickCounter == 10){
timer++;
tickCounter = 0;
}
}
return 0;
}