-
Notifications
You must be signed in to change notification settings - Fork 0
/
Flappy_bird.cc
149 lines (109 loc) · 2.62 KB
/
Flappy_bird.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// C++ Implementation for drawing line
#include <graphics.h>
// driver code
#include<iostream>
#include<random>
using namespace std;
#define NOP 3 //No. of Pipes
#define NOB 1 //No. of Birds
class Bird
{
public:
double x,y; //position of Bird
double vx,vy; //velocity
int tmp; //for testing
public:
Bird(double x1, double y1, double vx1, double vy1, int tmp1){
x = x1; y = y1; vx = vx1; vy = vy1; tmp = tmp1;
}
~Bird();
void bird_update();
};
void Bird::bird_update(){
y += vy;
vy += 1.0;
}
class Pipe
{
public:
double locx[4], locy[4];
double speed;
public:
Pipe(){
speed = 10;
for (int i = 0; i < 4; ++i) {
locx[i] = 0; locy[i] = 0 ;
}
}
~Pipe();
void pipe_update();
};
void Pipe::pipe_update(){
for (int i = 0; i < 4; ++i) locx[i] -= speed;
}
void farthest_pipe(Pipe *pip[NOP], int ind){
double tmp = 0;
tmp = pip[ind]->locx[1];
for (int i = 0; i < NOP; ++i){
tmp = max(tmp, pip[i]->locx[1]);
}
pip[ind]->locx[0] = tmp+100;
pip[ind]->locx[1] = tmp+200;
pip[ind]->locx[2] = tmp+100;
pip[ind]->locx[3] = tmp+200;
}
int main()
{
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
int maxX = getmaxx();
int maxY = getmaxy();
Bird *bob;
bob = new Bird(maxX/4.0, maxY/2.0, 1, 1, 1);
double p_x[4], p_y[4];
//Upper part of Pipe //Lower part of pipe
p_x[0] = maxX/3.0; p_x[1] = maxX/3.0+100; p_x[2] = maxX/3.0; p_x[3] = maxX/3.0+100;
p_y[0] = 0; p_y[1] = maxY/2 - 100; p_y[2] = maxY/2 + 100; p_y[3] = maxY;
int r = 0;
Pipe **p_new;
p_new = new Pipe *[NOP];
for (int i = 0; i < NOP; ++i) {
p_new[i] = new Pipe();
for (int j = 0; j < 4; ++j) {
r = rand()%100 + 1;
p_new[i]->locx[j] = p_x[j] + i*200;
p_new[i]->locy[j] = p_y[j];
if(i != 0){
p_new[i]->locy[1] = maxY/2 - r;
p_new[i]->locy[2] = maxY/2 + r;
}
}
}
int i = 0;
while (i < 100)
{
circle(bob->x, bob->y, 20);
floodfill(bob->x, bob->y, 4);
if (bob->y < (maxY-30)) bob->bird_update();
for (int i = 0; i < NOP; ++i){
rectangle(p_new[i]->locx[0], p_new[i]->locy[0], p_new[i]->locx[1], p_new[i]->locy[1]);
floodfill((p_new[i]->locx[0] + p_new[i]->locx[1])/2.0,
(p_new[i]->locy[0] + p_new[i]->locy[1])/2.0, 4);
rectangle(p_new[i]->locx[2], p_new[i]->locy[2], p_new[i]->locx[3], p_new[i]->locy[3]);
floodfill((p_new[i]->locx[2] + p_new[i]->locx[3])/2.0,
(p_new[i]->locy[2] + p_new[i]->locy[3])/2.0, 4);
}
for (int i = 0; i < NOP; ++i) {
p_new[i]->pipe_update();
if (p_new[i]->locx[0] < 10){
farthest_pipe(p_new, i);
}
}
delay(10);
cleardevice();
i++;
}
getch();
closegraph();
delete []p_new;
}