-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathps3.cpp
More file actions
125 lines (106 loc) · 2.97 KB
/
Copy pathps3.cpp
File metadata and controls
125 lines (106 loc) · 2.97 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <conio.h>
using namespace std;
bool gameover = false;
int birdx, birdy;
const int WIDTH = 60, HEIGHT = 30;
const int GAP = 6;
const int NUM_PIPES = 4;
int pipeX[NUM_PIPES];
int pipeY[NUM_PIPES];
int score=0;
void gotoxy(int x,int y){
COORD coord;
coord.X=x;
coord.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
}
void setup() {
birdx = 15;
birdy = 20;
for (int i = 0; i < NUM_PIPES; i++) {
pipeX[i] = WIDTH - 10 + i * 15;
pipeY[i] = rand() % (HEIGHT - GAP);
}
}
void draw() {
gotoxy(0,0);
string screen=" ";
// top border
for (int i = 0; i < WIDTH; i++) screen+="=";
screen+="\n";
// game area
for (int j = 1; j < HEIGHT; j++) {
for (int i = 0; i < WIDTH; i++) {
if (i == 0 || i == WIDTH - 1) screen+= "|"; // walls
else if (i == birdx && j == birdy) screen+= "0"; // bird
else {
bool pipeDrawn = false;
for (int p = 0; p < NUM_PIPES; p++) {
if (i == pipeX[p] && (j < pipeY[p] || j > pipeY[p] + GAP)) {
screen+= "|";
pipeDrawn = true;
break;
}
}
if (!pipeDrawn) screen+=" ";
}
}
screen+="\n";
}
for (int i = 0; i < WIDTH; i++) screen+= "=";
screen+="\n";
screen+="Score: "+to_string(score)+"\n";
cout<<screen;
cout<<"press tab to move the bird"<<endl<<"press 'q' to quit"<<endl;
}
void updateBird() {
if (_kbhit()) {
char ch = _getch();
if (ch == ' ') birdy -= 5; // jump
else if (ch == 'q') gameover = true;
}
birdy++; // gravity
}
void updatePipes() {
for (int i = 0; i < NUM_PIPES; i++) {
pipeX[i]--; // move left
if (pipeX[i] < 1) {
pipeX[i] = WIDTH - 2; // reset to right
pipeY[i] = rand() % (HEIGHT - GAP); // new hole
}
}
}
void updatescore(){
for(int i=0;i<NUM_PIPES;i++){
if(birdx-1==pipeX[i]) score++;
}
}
int main() {
setup();
while (!gameover) {
draw();
if(birdy==0||birdy==30) {gameover=true;
system("cls");
gotoxy(20,10);
cout<<"Game Over!"<<"score: "<<score<<endl;
break;}
for(int i=0;i<NUM_PIPES;i++){
if(birdx==pipeX[i]){
if(birdy<pipeY[i]||birdy>pipeY[i]+GAP){
gameover=true;
system("cls");
gotoxy(20,10);
cout<<"Game Over!"<<endl<<"score :"<<score<<endl;
break;
}
}
}
updatescore();
updateBird();
updatePipes();
Sleep(200);
}
}