Flappy Bird !

Hello!
I have created Flappy bird game for Windows console! I want to share my code with you. Code with comments! Note that for highscore saving you need to create a file in /Program files/FlappyBird/options.txt

My code:
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <iostream> //created by HAKERIS1010
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include <ctime>
#include <fstream>
#include <iomanip>

using namespace std;

ifstream inp;  //data file reading and writing operators
ofstream outp;

char c[30][21]; //variable for storing screen particles (pixels)
int n[30][21];  //variable for checking
int highscore;
int contr,tuk=0,score=0,t=0,bt=0,birdx=0,birdy=0; //variaous variables for certain operations
bool err; //boolean for error detection

void game();  //various functions
void screen();
void pipes();
void bird();
bool gameover();
void checkscore();
void help();
void menu();
void endgame();
void credits();

int main()
{
srand(time(0));  //seeding random number gen, we will need it later;
inp.open("/Program Files/FlappyBird/options.txt");  //opening file in which highscore is stored
if(inp.is_open()) //if file opens successfully, it reads the highscore
{
    inp>>highscore;
    inp.close();
    err=false;  //error will be false, because file opened successfully
}
else
{
    highscore=0; //if file doesnt exist, highscore will be 0, and err will be true
    err=true;
}

int a=0,b;
char sl; //selection variable
while(1) //loop for repeating actions after each start
{
    if(a==0) goto play; 
    if(a>0)               //if you play not the first time, it will ask you if you want to play
    {
        score=0;
        cout<<"Do you want to play again? [y/n] ";
        cin>>sl;
        if(sl=='n') goto quit;
        else goto play;
    }
    play:
    menu(); //calling menu function
    cin>>sl;
    switch(sl) //menu selections
    {
        case '1':
        {
            game(); //if you choose play, it calls function game
            break;
        }
        case '2': //other selections-other functions
        {
            help(); 
            goto play;
            break;
        }
        case '3':
        {
            credits();
            goto play;
            break;
        }
        case '4':
        {
            goto quit; //exits game
            break;
        }
        default:
        {
            goto play;
            break;
        }
    }
    a++; //variable for checking how many times you've played
}
quit:
{
   cout<<"I quit."; //stops game, app closes.
}

return 0;
}

void game()  //function for playing game
{
    int x,y;
    char s;
    for(y=0;y<21;y++)  //setting screen
    {
        for(x=0;x<30;x++)
        {
            if(y<20)
            {
            c[x][y]=' ';
            n[x][y]=0;
            }
            if(y==20)
            {
                c[x][y]='-';
                n[x][y]=2;
            }
        }
    }
    c[10][10]='*';  //in these coordinates will be our bird, marked *
    screen();      //calls func for showin screen
    while(1)       //loop starts, actual gameplay begins
    {
        s='~';  //default control variable
        Sleep(0.2*1000);  //this sets how fast everyhing moves
        t++;              //this is a variable for storing 'time',or how many times a loop passed
        if(kbhit()) //if key is pressed, certain operations are done for bird to move up.
        {
            s=getch();        //gets what key is pressed
            if(s!='~') tuk=1; //if it is not default, then 'tuk' will be equal to 1, meaning that bird will fly up
        }
        for(x=0;x<30;x++) //just setting ground
        {
            c[x][20]='-';
            n[x][20]=2;
        }
        bird();                       //cals bird move function
        checkscore();                 //checks score
        if(gameover()==true) goto gameEnd;   //checks if bird hits pipes, if yes, game ends
        pipes();                             //spawns and moves pipes
        if(score>highscore) highscore=score;  //i think this is clear
        screen();                            //finally, calls screen function to show enerything.

        if(tuk>0) tuk++;           //if key is pressed, bird will fly up 2 times.
        if(tuk==3) tuk=0;          //after that, bird falls
    }
    gameEnd:   //ends game
    {
        if(score>highscore) highscore=score;
        if(err==false)              //if hi-score file exists, it writes your new highscore there.
        {
            outp.open("/Program Files/FlappyBird/options.txt");
            outp<<highscore;
            outp.close();
        }
        screen();    //shows ending screen, and returns to int main
        endgame();
        return;
    }
}

void screen()    //func for showing screen
{
    int x,y;
    system("cls");    //clears console
    for(y=0;y<21;y++) //shows pixels on their coordinates, and your score
    {
        for(x=0;x<30;x++)
        {
            if(x<29) cout<<c[x][y];
            if(x==29) cout<<c[x][y]<<endl;
        }
    }
    cout<<""<<endl;
    cout<<"Your Score: "<<score;
}

void pipes()  //pipe movement and spawn func
{
    int x,y,r;
    if(t==10)   //if time is 10, or loop has passed 10 times, it spawns a new pipe
    {
        r=(rand()%11)+5;  //generates random number, which will be the pipe's hole center
        for(y=0;y<20;y++)  // only y coordinate is needed
        {
            c[29][y]='|';  //sets pipe
            n[29][y]=2;    //n will be 2, for checking if bird has hit it,
        }
        c[29][r-1]=' ';  //sets hole
        c[29][r]=' ';
        c[29][r+1]=' ';
        n[29][r-1]=0;
        n[29][r]=0;
        n[29][r+1]=0;
        t=0;
        goto mv; //moves pipes
    }
    else goto mv;
    mv:                 //pipe movement
    {
        for(y=0;y<20;y++)  //loops for generating coordinates
        {
            for(x=0;x<30;x++)
            {
                if(c[x][y]=='|')  //all the pipes will be moved left by 1;
                {
                    if(x>0)
                    {
                        c[x-1][y]='|';
                        n[x-1][y]=2;
                        c[x][y]=' ';
                        n[x][y]=0;
                    } 
                    if(x==0)  //if screen ends (x=0) pipe will dissapear, to prevent errors
                    {
                        c[x][y]=' ';
                        n[x][y]=0;
                    }
                }
            }
        }
    }
}

void bird()   //bird movement function!
{
    int x,y;
    if(tuk>0) //if key is pressed, bird moves up
    {
        bt=0;
        for(y=0;y<20;y++)   //loops for finding bird coordinates
        {
            for(x=0;x<30;x++)
            {
                if(c[x][y]=='*')
                {
                    if(y>0)
                    {
                    c[x][y-1]='*';  //bird moves up by 1;
                    c[x][y]=' ';
                    birdx=x;        //sets bird x coordinate
                    birdy=y-1;      //sets bird y coord
                    return;         //retuns to game func
                    }
                }
            }
        }
    }
    else   //if no key is pressed, bird falls
    {
        bt++;
        for(y=0;y<20;y++)
        {
            for(x=0;x<30;x++)
            {
                if(c[x][y]=='*')
                {
                    if(y<20)  //if bird is not on the ground
                    {
                        if(bt<3)   //if bird time is lower that 3, it falls 1 pixel
                        {
                            c[x][y+1]='*';
                            c[x][y]=' ';
                            birdx=x;
                            birdy=y+1;
                            return;
                        }
                        else if(bt>2 && bt<5)  //more time has passed, faster bird falls (acceleration)
                        {
                            c[x][y+2]='*';
                            c[x][y]=' ';
                            birdx=x;
                            birdy=y+2;
                            return;
                        }
                        else if(bt>4)
                        {
                            c[x][y+3]='*';
                            c[x][y]=' ';
                            birdx=x;
                            birdy=y+3;
                            return;
                        }
                    }
                    else
                    {
                        return;  //if bird is already on the ground, function returns to check for game over.
                    }
                }
            }
        }
    }
}
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

void checkscore()  //checks if bird gained score
{
    int y;
    for(y=0;y<20;y++)
    {
        if(c[birdx][y]=='|')  //if bird x coord is equal to pipe's coord, you get 1 point
        {
            score++;
            return;
        }
    }
}

bool gameover()  //checks if bird has hit something
{
    int x,y,f=0;
    if(birdy>19) //checks if bird hits ground
    {
        c[birdx][19]='*';  //sets bird and ground again, prevents errors
        c[birdx][20]='-';
        f=1;           //f=1, means function will return true
        goto quit;
    }
    else
    {     //checks if bird has hit pipes, here the 'n' variable is needed (pipe's coordinate's n is equal 2 (more than 0))
        if(n[birdx][birdy]>0 && (c[birdx][birdy]=='|' || c[birdx][birdy]=='*'))
        {
            c[birdx][birdy]='|';
            c[birdx-1][19]='*';
            f=1;
            goto quit;
        }
    }
    quit:
    if(f==1) return true;
    else return false;
}

void endgame() //just some screens for certain actions
{
    screen();   //this one pops up if game ends
    cout<<""<<endl<<endl;
    cout<<" ------------------------------------------------------------------------- "<<endl;
    cout<<"|    *****      *     *       * ******       ****  *       ****** ****    |"<<endl;
    cout<<"|   *          * *    * *   * * *           *    *  *     * *     *   *   |"<<endl;
    cout<<"|   *  ****   *   *   *  * *  * *****       *    *   *   *  ****  ****    |"<<endl;
    cout<<"|   *  *  *  *******  *   *   * *           *    *    * *   *     * *     |"<<endl;
    cout<<"|    *****  *       * *       * ******       ****      *    ***** *   *   |"<<endl;
    cout<<" ------------------------------------------------------------------------- "<<endl;
    cout<<""<<endl<<endl;
    cout<<"                        Y O U R   S C O R E : "<<score<<endl<<endl;
    cout<<"                        H I G H   S C O R E : "<<highscore<<endl;
    cout<<""<<endl<<endl;
}

void menu()  //shows menu
{
    system("cls");
    cout<<""<<endl;
    cout<<" --------------------------------------------------------  "<<endl;
    cout<<"|                                                        | "<<endl;
    cout<<"|   **** *    **** **** **** *   *    ***  * ***  ***    | "<<endl;
    cout<<"|   *    *    *  * *  * *  * *   *    *  * * *  * *  *   | "<<endl;
    cout<<"|   ***  *    **** **** **** *****    ***  * ***  *  *   | "<<endl;
    cout<<"|   *    *    *  * *    *      *      *  * * *  * *  *   | "<<endl;
    cout<<"|   *    **** *  * *    *      *      ***  * *  * ***    | "<<endl;
    cout<<"|                                                  v 1.0 | "<<endl;
    cout<<" --------------------------------------------------------  "<<endl;
    cout<<""<<endl<<endl;
    cout<<"                  High Score:  "<<highscore<<endl<<endl;
    cout<<""<<endl<<endl;
    cout<<"                     M E N U:    "<<endl<<endl;
    cout<<"                  1: Start Game  "<<endl<<endl;
    cout<<"                  2: Help        "<<endl<<endl;
    cout<<"                  3: Credits     "<<endl<<endl;
    cout<<"                  4: Exit        "<<endl<<endl;
}

void credits()
{
    char sel;
    system("cls");
    while(true)
    {
    cout<<""<<endl<<endl;
    cout<<"               Lead programmer: hakeris1010 "<<endl<<endl;
    cout<<"               Designer: hakeris1010 "<<endl<<endl;
    cout<<"               Testers: hakeris1010 "<<endl<<endl;
    cout<<"               Special thanks to: hakeris1010 ,Dong Nguyen (original)"<<endl<<endl;
    cout<<"               Version: 1.0 "<<endl<<endl<<endl;
    cout<<"Go back? [y/n]  ";
    cin>>sel;
    if(sel=='y') return;
    else system("cls");
    }
}

void help()
{
    char sel;
    system("cls");
    while(true)
    {
    cout<<""<<endl<<endl;
    cout<<"                   Controls: Press any key to fly up.         "<<endl<<endl;
    cout<<"             Goal: Fly through the holes between the pipes.   "<<endl;
    cout<<"             When you pass through the hole,you get 1 point.  "<<endl;
    cout<<"                    Try to pass as much as you can.           "<<endl;
    cout<<"            But be careful, don't hit the pipes or the ground!"<<endl<<endl;
    cout<<"                          Are you ready? Go!                  "<<endl<<endl<<endl;
    cout<<"Go back? [y/n]  ";
    cin>>sel;
    if(sel=='y') return;
    else system("cls");
    }
}
//version 1.0
//made by hakeris1010
//thanks for playing!!! 
Now try doing it without goto or global variables.
Topic archived. No new replies allowed.