-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple Dino game.c
80 lines (64 loc) · 1.72 KB
/
simple Dino game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <conio.h>
#include <windows.h> // for Sleep()
// Function to display the game screen
void draw(int dinoPos, int obstaclePos) {
system("cls"); // Clear the screen
// Print the ground
for (int i = 0; i < 20; i++) {
printf("_");
}
printf("\n");
// Print the obstacle
for (int i = 0; i < obstaclePos; i++) {
printf(" ");
}
printf("@"); // The obstacle
printf("\n");
// Print the dino
for (int i = 0; i < dinoPos; i++) {
printf(" ");
}
printf("O"); // The dinosaur
printf("\n");
}
int main() {
int dinoPos = 0;
int obstaclePos = 50;
int jump = 0;
int score = 0;
printf("Dino Game (Press Space to Jump, Q to Quit)\n");
while (1) {
if (_kbhit()) { // Check if a key is pressed
char key = _getch();
if (key == ' ') { // Spacebar to jump
jump = 1;
} else if (key == 'q' || key == 'Q') { // Quit the game
break;
}
}
if (jump == 1) {
dinoPos++;
if (dinoPos == 10) {
jump = 0;
}
} else {
dinoPos--;
if (dinoPos == 0) {
jump = 0;
}
}
obstaclePos--;
if (obstaclePos == 0) {
obstaclePos = 50;
score++;
}
if (dinoPos == obstaclePos && jump == 0) {
printf("\nGame Over! Your Score: %d\n", score);
break;
}
draw(dinoPos, obstaclePos);
Sleep(50); // Sleep for a short time to control game speed
}
return 0;
}