Use OpenCV for a game

来源:互联网 发布:java多线程源码解析 编辑:程序博客网 时间:2024/06/08 07:22

     Well, when mentioned OpenCV, it may firstoccur to you is image processing. It may because it has a verity of imageprocessing function integrated. Besides that, OpenCV can be used for any applicationhas a graphic interface. So this time I will introducing a game project basedon OpenCV.

  

     In early 2014, there is a game calledFlappy bird which is very popular amount phone application. Unfortunately, thisapplication is no longer available on app store. What a shame that a good gameis over. After that, many developer imitate the game and made it re-availableto the public. So this time I’m trying to use OpenCV to re-create this game.


Before we start, you need first setup the environment of OpenCV. For download the package and installation, please refer to:

http://opencv.org/downloads.html

After you setup CV successfully, you can now startup a new C++ project:

graphic.h

/*Project: Flappy bird Opencv version    -- by Jun HouFileName: graphic.hDescription: All class defination, function defination*/#include <opencv2/highgui/highgui.hpp> //Input and output functions#include <opencv/cv.h> //algorithms#include "stdio.h"#include <iostream>#include <vector>#include <windows.h>#include <list>using namespace cv;class flappy{public:    Mat img_bird;    Mat img_mask;    Mat img_end;    Mat img_begin;    Mat img_background;    Mat img_original;    Mat frame;    Point bird_pos;    int space;    //space which allow birds go in    int score;    //record the score    int counter;  //    int row;    int v;CvFont font;    bool status;  //true:game on, false: game over    CHAR showMsg[100];    std::list<Point> brack;    flappy(){        img_original.create(480,640,CV_8UC3);        bird_pos = Point(300,400);        img_bird = imread("bird.png");        img_mask = imread("bird_msk.png",CV_LOAD_IMAGE_GRAYSCALE);        img_end = imread("gameover.png");        img_background = imread("back_ground.png");        img_begin = imread("fp_cover.png");    cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, 1.0,1.0,0,2);            for(int col = 0; col < 640; col++)    {    for(int row = 0; row < 480; row ++)    {    img_original.ptr<uchar>(row)[3 * col] = 255;    img_original.ptr<uchar>(row)[3 * col + 1] = 255;    img_original.ptr<uchar>(row)[3 * col + 2] = 255;    }    }        img_original.copyTo(frame);        space = 170;        score = 0;        counter = 240;        row = 0;        v = -10;        status = false;}    void run(){        status = true;        cv::namedWindow("Game",CV_WINDOW_AUTOSIZE);      while(1)    {    imshow("Game",img_begin);    Sleep(50);    char c = cv::waitKey(1);            if(c == 'a' || c == 'A') break;    }    while(1)    {    counter++;    if (counter >= 50)    {    counter = 0;    brack.push_back(Point(620,rand()%350));    }    img_original.copyTo(frame);    cv::Rect rect(row,0,640,480);    img_background(rect).copyTo(frame);    if(row>=640) row = 0;    else row++;    if(!brack.empty())    {    for(auto iter = brack.begin(); iter != brack.end();)    {    if(iter->x <= 10)    {    auto delit = iter;    iter++;    brack.erase(delit);    }    else     {    if(iter->x >= 297 && iter->x < 300) score = score+10; //record the score    iter->x = iter->x - 3;  // control the speed    iter++;    }    }    }    for(auto iter = brack.begin(); iter != brack.end(); iter++)    {    Point recUp1 = Point(iter->x,0);    Point recUp2 = Point(iter->x+10,iter->y);    Point recDn1 = Point(iter->x,iter->y+space);    Point recDn2 = Point(iter->x+10,480);    cv::rectangle(frame,recUp1,recUp2,Scalar(80,200,100),25,2);    cv::rectangle(frame,recDn1,recDn2,Scalar(80,200,100),25,2);    if(bird_pos.x >(iter->x-25) && bird_pos.x < (iter->x+35))    {    if(bird_pos.y < iter->y || bird_pos.y > iter->y+space)    {    //printf("Gameover\n");    status = true;    Sleep(900);    break;    }    }    }    if(!status) break;    bird_pos.y = bird_pos.y+v;    v = v + 2;    // draw bird    cv::Rect bird(bird_pos.x - 25,min(445,bird_pos.y - 17),51,35);    img_bird.copyTo(frame(bird),img_mask);    sprintf(showMsg,"Score: %d",score);    putText(frame,showMsg,Point(450,100),FONT_HERSHEY_COMPLEX_SMALL,1.0,Scalar(0,0,200),2,8);    imshow("Game",frame);    Sleep(50);    char c = cv::waitKey(1);            if(c == 27) break;    if(c == 'a')    {    v = -15; // fly height when pressed     }    if(bird_pos.y > 460 )    {    status = true;    Sleep(900);    break;    }    }    while(1)    {    putText(img_end,showMsg,Point(200,200),FONT_HERSHEY_COMPLEX_SMALL,2.0,Scalar(0,0,200),2,8);    imshow("Game",img_end);    char c = cv::waitKey(1);            if(c == 27) break;    }}};


core.cpp

/*Project: Flappy bird Opencv version    -- by Jun HouFileName: core.cppDescription: main function*/#include "graphic.h"void main(){    flappy flappy_bird;    flappy_bird.run();}


Running example:








0 0
原创粉丝点击