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