From 1ea3f01414d3666beb9a7e3f8fe91a2c1749a482 Mon Sep 17 00:00:00 2001 From: MerajDerafshi Date: Tue, 23 Sep 2025 03:08:34 +0330 Subject: [PATCH] feat(game): add timer functionality to gameplay --- snake.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/snake.cpp b/snake.cpp index d9b18ce..284e720 100644 --- a/snake.cpp +++ b/snake.cpp @@ -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; } \ No newline at end of file