Last active
December 27, 2017 19:13
-
-
Save hasanaga/6299156bbd52a229a1382c8a4291276e to your computer and use it in GitHub Desktop.
Flappy Bird - with QT & OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#------------------------------------------------- | |
# | |
# Project created by QtCreator 2014-02-14T16:58:57 | |
# | |
#------------------------------------------------- | |
QT += core gui | |
TARGET = GameFlappyBird | |
TEMPLATE = app | |
INCLUDEPATH += C:\\opencv\\include \ | |
C:\\opencv\\3rdparty\\videoinput\\include | |
LIBS += -LC:\\opencv\\bin \ | |
-lopencv_core245 \ | |
-lopencv_highgui245 \ | |
-lopencv_imgproc245 \ | |
-lopencv_nonfree245 \ | |
-lopencv_features2d245 \ | |
-lopencv_calib3d245 \ | |
-lopencv_contrib245 \ | |
-lopencv_flann245 \ | |
-lopencv_photo245 \ | |
-lopencv_video245 \ | |
-lopencv_stitching245 \ | |
LIBS += C:\\opencv\\3rdparty\\videoinput\\libvideoInput.a | |
LIBS += C:\\opencv\\3rdparty\\videoinput\\*.lib | |
SOURCES += main.cpp\ | |
mainwindow.cpp | |
HEADERS += mainwindow.h | |
FORMS += mainwindow.ui |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QtGui/QApplication> | |
#include "mainwindow.h" | |
int main(int argc, char *argv[]) | |
{ | |
QApplication a(argc, argv); | |
MainWindow w; | |
w.show(); | |
return a.exec(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <QTimer> | |
#include <QDebug> | |
#include <QKeyEvent> | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
image1 = cvLoadImage("1.jpg"); | |
image2 = cvCreateImage(cvSize(image1->width,image1->height),image1->depth,image1->nChannels); | |
timer1 = new QTimer(this); | |
connect(timer1,SIGNAL(timeout()),this,SLOT(timerFunc())); | |
cvNamedWindow("Game"); | |
current = 0; | |
double hScale=1.0; | |
double vScale=1.0; | |
int lineWidth=2; | |
cvInitFont(&font,CV_FONT_ITALIC, hScale,vScale,0,lineWidth); | |
} | |
void MainWindow::start() | |
{ | |
int1 = 0; | |
v = 0; | |
h = 100; | |
t = 0; | |
x = 0; | |
rec[0] = image2->width + 100; | |
rec[1] = image2->width + 250; | |
rec[2] = image2->width + 400; | |
rec[3] = image2->width + 550; | |
recH[0] = 50; | |
recH[1] = 60; | |
recH[2] = 70; | |
recH[3] = 80; | |
recT[0] = image2->width + 26; | |
recT[1] = image2->width + 176; | |
recT[2] = image2->width + 326; | |
recT[3] = image2->width + 476; | |
recTH[0] = 50; | |
recTH[1] = 60; | |
recTH[2] = 70; | |
recTH[3] = 80; | |
birdH = 250; | |
birdR = 10; | |
count = 0; | |
fcount = false; | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
void MainWindow::timerFunc() | |
{ | |
cvConvertImage(image1,image2); | |
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; | |
} | |
} | |
//////////////////////////////// | |
if( v > 0) | |
{ | |
if((h - t) >0) | |
{ | |
t -= 0.2; | |
h -= t; | |
v --; | |
}else | |
{ | |
v = 0; | |
t = 0; | |
} | |
}else | |
{ | |
if(h < 278) | |
{ | |
t += 0.2; | |
h += t; | |
}else | |
{ | |
t = (int) (t/2); | |
v = t * 5; | |
} | |
} | |
cvCircle(image2,cvPoint(birdH,h),birdR,cvScalar(0,0,255),-1); | |
///////////////////////////////////////////////////////////////////////// | |
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]; | |
} | |
} | |
scount =QString::number(count); | |
cvPutText (image2,scount.toStdString().c_str(),cvPoint(200,340), &font, cvScalar(46,189,116)); | |
cvPutText (image2,"past game:",cvPoint(380,340), &font, cvScalar(46,189,116)); | |
scount =QString::number(current); | |
cvPutText (image2,scount.toStdString().c_str(),cvPoint(560,340), &font, cvScalar(46,189,116)); | |
cvShowImage("Game",image2); | |
} | |
void MainWindow::keyPressEvent(QKeyEvent *evt) | |
{ | |
if(evt->key()==Qt::Key_1) | |
{ | |
t = 1; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_2) | |
{ | |
t = 2; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_3) | |
{ | |
t = 3; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_4) | |
{ | |
t = 4; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_5) | |
{ | |
t = 5; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_6) | |
{ | |
t = 6; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_7) | |
{ | |
t = 7; | |
v = 5*t; | |
} | |
if(evt->key()==Qt::Key_8) | |
{ | |
t = 8; | |
v = 5*t; | |
} | |
} | |
void MainWindow::on_Start_clicked() | |
{ | |
start(); | |
timer1->start(10); | |
} | |
void MainWindow::on_pushButton_clicked() | |
{ | |
t = 4; | |
v = 5*t; | |
} | |
void MainWindow::on_pushButton_2_clicked() | |
{ | |
timer1->stop(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MAINWINDOW_H | |
#define MAINWINDOW_H | |
#include <opencv2/highgui/highgui.hpp> | |
#include <opencv2/imgproc/imgproc.hpp> | |
#include <QMainWindow> | |
#include <QTimer> | |
using namespace cv; | |
namespace Ui { | |
class MainWindow; | |
} | |
class MainWindow : public QMainWindow | |
{ | |
Q_OBJECT | |
public: | |
explicit MainWindow(QWidget *parent = 0); | |
~MainWindow(); | |
QTimer *timer1; | |
QTimer *timer2; | |
IplImage *image1, *image2; | |
Mat image3; | |
int int1,int2,int3; | |
int v,h,x; | |
int rec1,rec2,rec3,rec4; | |
int rec[4]; | |
int recH[4]; | |
int recT[4]; | |
int recTH[4]; | |
int birdH; | |
int birdR; | |
int count; | |
bool fcount; | |
CvFont font; | |
QString scount; | |
int current; | |
double t; | |
void start(); | |
public slots: | |
void timerFunc(); | |
private slots: | |
void on_Start_clicked(); | |
void on_pushButton_clicked(); | |
void on_pushButton_2_clicked(); | |
protected: | |
void keyPressEvent(QKeyEvent *evt); | |
private: | |
Ui::MainWindow *ui; | |
}; | |
#endif // MAINWINDOW_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<ui version="4.0"> | |
<class>MainWindow</class> | |
<widget class="QMainWindow" name="MainWindow"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>400</width> | |
<height>300</height> | |
</rect> | |
</property> | |
<property name="windowTitle"> | |
<string>MainWindow</string> | |
</property> | |
<widget class="QWidget" name="centralWidget"> | |
<widget class="QPushButton" name="Start"> | |
<property name="geometry"> | |
<rect> | |
<x>20</x> | |
<y>100</y> | |
<width>101</width> | |
<height>41</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Start</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pushButton"> | |
<property name="geometry"> | |
<rect> | |
<x>190</x> | |
<y>130</y> | |
<width>91</width> | |
<height>31</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Jump</string> | |
</property> | |
</widget> | |
<widget class="QPushButton" name="pushButton_2"> | |
<property name="geometry"> | |
<rect> | |
<x>190</x> | |
<y>90</y> | |
<width>91</width> | |
<height>31</height> | |
</rect> | |
</property> | |
<property name="text"> | |
<string>Pause</string> | |
</property> | |
</widget> | |
</widget> | |
<widget class="QMenuBar" name="menuBar"> | |
<property name="geometry"> | |
<rect> | |
<x>0</x> | |
<y>0</y> | |
<width>400</width> | |
<height>21</height> | |
</rect> | |
</property> | |
</widget> | |
<widget class="QToolBar" name="mainToolBar"> | |
<attribute name="toolBarArea"> | |
<enum>TopToolBarArea</enum> | |
</attribute> | |
<attribute name="toolBarBreak"> | |
<bool>false</bool> | |
</attribute> | |
</widget> | |
<widget class="QStatusBar" name="statusBar"/> | |
</widget> | |
<layoutdefault spacing="6" margin="11"/> | |
<resources/> | |
<connections/> | |
</ui> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment