-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.cpp
More file actions
94 lines (82 loc) · 2.26 KB
/
Copy pathchatgpt.cpp
File metadata and controls
94 lines (82 loc) · 2.26 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
#include <iostream>
#include <conio.h>
#include <windows.h>
using namespace std;
// Console window size
const int WIDTH = 40;
const int HEIGHT = 20;
// Car dimensions
const int CAR_WIDTH = 4;
const int CAR_HEIGHT = 2;
// Car position and speed
int car_x = WIDTH / 2 - CAR_WIDTH / 2;
int car_y = HEIGHT - CAR_HEIGHT - 1;
int car_speed = 1;
// Obstacle position and speed
int obstacle_x = rand() % (WIDTH - 3) + 1;
int obstacle_y = 0;
int obstacle_speed = 1;
// Score
int score = 0;
// Function to draw the game screen
void draw_screen() {
system("cls");
for(int i = 0; i < HEIGHT; i++) {
for(int j = 0; j < WIDTH; j++) {
if(i == 0 || i == HEIGHT - 1 || j == 0 || j == WIDTH - 1)
cout << "*";
else if(i == car_y && j >= car_x && j < car_x + CAR_WIDTH)
cout << "=";
else if(i == obstacle_y && j >= obstacle_x && j < obstacle_x + 3)
cout << "@";
else
cout << " ";
}
cout << endl;
}
cout << "Score: " << score << endl;
}
// Function to update the game state
void update_game() {
// Move the car
if(_kbhit()) {
switch(_getch()) {
case 'a': car_x--; break;
case 'd': car_x++; break;
}
}
// Move the obstacle
obstacle_y += obstacle_speed;
if(obstacle_y >= HEIGHT - 1) {
obstacle_x = rand() % (WIDTH - 3) + 1;
obstacle_y = 0;
score++;
}
// Check for collision
if(obstacle_y == car_y + 1 && obstacle_x <= car_x + CAR_WIDTH - 1 && obstacle_x + 2 >= car_x) {
cout << "Game over!" << endl;
exit(0);
}
}
// Main function
int main() {
// Set console window size
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT windowSize = {0, 0, WIDTH - 1, HEIGHT - 1};
SetConsoleWindowInfo(hConsole, true, &windowSize);
// Set console buffer size
COORD bufferSize = {WIDTH, HEIGHT};
SetConsoleScreenBufferSize(hConsole, bufferSize);
// Hide the cursor
CONSOLE_CURSOR_INFO cursorInfo;
cursorInfo.bVisible = false;
cursorInfo.dwSize = 1;
SetConsoleCursorInfo(hConsole, &cursorInfo);
// Game loop
while(true) {
draw_screen();
update_game();
Sleep(50);
}
return 0;
}