Flappy Bird PC version – with QT 4.8 & OpenCV Source code

 

It is a simple game like “Flappy Bird”. Written with OpenCV and QT 4.8 (C++) is only for training.


flappy_bird

 

The code for the green lines

    int1 += 2;
    int1 %= 5000000;

    int arr[] = {int1,int1+3,(int1+6),(int1+9)};

        for(int i=0; i < image1->width; i++)
        {
            for(int ii = 0; ii < sizeof(arr)/4; ii++)
            {
                for(int j=291+ii*3; j < 294+ii*3; j++)
                {
                if(arr[ii] > 25)
                    cvSet2D(image2,j,i,cvScalar(46,189,0));
                else
                    cvSet2D(image2,j,i,cvScalar(91,236,160));
                }
                arr[ii]++;   arr[ii] %=50;
            }
        }

 

Code to move the red ball. 

        if( v > 0)
        {
                if((h - t) >0)
                {
                    t -= 0.2; 
                    h -= t; //for the gravity
                    v --;
                }else
                {
                    v = 0;
                    t = 0;
                }
        }else
        {
            if(h < 278)
            {
                t += 0.2;
                h += t; // for the gravity
            }else
            {
                t = (int) (t/2);
                v = t * 5;
            }
        }
        cvCircle(image2,cvPoint(birdH,h),birdR,cvScalar(0,0,255),-1);

 

The code to move the green pipes

  fcount = false;
    for(int i=0; i < sizeof(rec)/4; i++)
    {
        cvRectangle(image2,cvPoint(rec[i],288 - recH[i]),cvPoint(rec[i]+20,288),cvScalar(46,189,116),-2);
        cvRectangle(image2,cvPoint(recT[i],0),cvPoint(recT[i]+20,recTH[i]),cvScalar(46,189,116),-2);

        if(rec[i] <= (birdH - birdR) && (rec[i]+20) >= (birdH + birdR))
        {
            count++;
        }

        if(rec[i] > (birdH - birdR) && rec[i] < (birdH + birdR))
        {
            if( (288-recH[i] <= (h+birdR) ) )
            {
                cvPutText (image2,"GAME OVER",cvPoint(200,150), &font, cvScalar(46,189,116));
                cvShowImage("Game",image2);
                current = count;
                start();
            }
        }

        if(recT[i] > (birdH - birdR) && recT[i] < (birdH + birdR))
        {
            if( (recTH[i] >= (h-birdR) ) )
            {
                cvPutText (image2,"GAME OVER",cvPoint(200,150), &font, cvScalar(46,189,116));
                cvShowImage("Game",image2);
                current = count;
                start();
            }
        }

        rec[i] -= 2;
        if(rec[i]<0)
        {
            rec[i] = image2->width + 100;
            recH[i] = rand() % 100;
        }

        recT[i] -= 2;
        if(recT[i]<0)
        {
            recT[i] = image2->width + 100;
            recTH[i] = (150 + (int)(count/2) ) - recH[i];

        }
    }

Project  Download: Flappy_Bird

 

 

 

 

 

 

 

 

 

 

 

 

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

Leave a Reply