-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht-rex.cpp
More file actions
97 lines (85 loc) · 2.31 KB
/
t-rex.cpp
File metadata and controls
97 lines (85 loc) · 2.31 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
// Function to set the console cursor position
void setCursorPosition(int x, int y) {
static const HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
cout.flush();
COORD coord = { (SHORT)x, (SHORT)y };
SetConsoleCursorPosition(hOut, coord);
}
int main() {
int score = 0;
int delay = 100; // Delay in milliseconds
int height = 10;
int width = 40;
int dinoPos = height - 1;
int cactusPos = width - 1;
bool gameover = false;
// Clear the console screen
system("cls");
// Print the initial game board
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == dinoPos && j == 0) {
cout << "O";
}
else if (i == height - 1) {
cout << "-";
}
else {
cout << " ";
}
}
cout << endl;
}
// Main game loop
while (!gameover) {
// Move the cactus
cactusPos--;
if (cactusPos < 0) {
cactusPos = width - 1;
score++;
}
// Check for collision
if (cactusPos == 0 && dinoPos == height - 1) {
gameover = true;
}
// Update the game board
setCursorPosition(0, 0);
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == dinoPos && j == 0) {
cout << "O";
}
else if (i == height - 1) {
cout << "-";
}
else if (i == height - 1 && j == cactusPos) {
cout << "X";
}
else {
cout << " ";
}
}
cout << endl;
}
// Wait for a short delay
Sleep(delay);
if (_kbhit()) {
// Check if the user has pressed the space bar
char key = _getch();
if (key == ' ') {
dinoPos = height - 2;
}
}
// Move the dinosaur
if (dinoPos < height - 1) {
dinoPos++;
}
}
// Print the final score
cout << "Game over! Your score is " << score << "." << endl;
return 0;
}