码迷,mamicode.com
首页 > 其他好文 > 详细

OPENCV图像处理——我的第一个游戏

时间:2015-04-02 14:43:26      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

障碍物的结构体:

 1 typedef struct
 2 {
 3     
 4     IplImage* Image[2];
 5     
 6     int px[2],py[2];
 7     int MaxDelta;
 8     int MinDelta;
 9     
10 }ImgType;
11 
12 
13 ImgType Img[10];
14 
15 void DefImg_P()
16 {
17     
18     Img[0].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier.png");
19     Img[0].px[0]=828;
20     Img[0].py[0]=-17;
21     Img[0].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier1.png");
22     Img[0].px[1]=830;
23     Img[0].py[1]=227;
24     Img[0].MaxDelta=1001;
25     Img[0].MinDelta=-156;
26     
27     Img[1].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier1.png");
28     Img[1].px[0]=830;
29     Img[1].py[0]=-5;
30     Img[1].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier.png");
31     Img[1].px[1]=828;
32     Img[1].py[1]=355;
33     Img[1].MaxDelta=1000;
34     Img[1].MinDelta=-156;
35     
36     Img[2].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier3.png");
37     Img[2].px[0]=828;
38     Img[2].py[0]=-17;
39     Img[2].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier2.png");
40     Img[2].px[1]=830;
41     Img[2].py[1]=227;
42     Img[2].MaxDelta=1001;
43     Img[2].MinDelta=-156;
44     
45     Img[3].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier2.png");
46     Img[3].px[0]=830;
47     Img[3].py[0]=-5;
48     Img[3].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier3.png");
49     Img[3].px[1]=828;
50     Img[3].py[1]=355;
51     Img[3].MaxDelta=1000;
52     Img[3].MinDelta=-156;
53     
54     
55 }

障碍物的打印:

 1 //将barrier载入背景图中的函数
 2 
 3 void ShowBarrier(ImgType barrier,IplImage* BackGround,int delta)
 4 {
 5     
 6     int x,y;
 7     int r,g,b;
 8     
 9     for(x=barrier.Image[0]->width-2;x>2;x--)
10     {
11         
12         for(y=barrier.Image[0]->height-2;y>2;y--)
13         {    
14             
15             if(y+barrier.py[0]>=0&&y+barrier.py[1]<=479)
16             {
17                 
18                 r=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x);
19                 g=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x+1);
20                 b=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x+2);
21                 
22                 if(r>=220&&g>=220&&b>=220)  continue;
23                 if(x+barrier.px[0]-delta<0||x+barrier.px[0]-delta>998) continue;
24 
25                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta))=r;
26                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta)+1)=g;
27                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta)+2)=b;
28                 
29             }
30             
31         }
32         
33     }
34     
35     for(x=barrier.Image[1]->width-2;x>2;x--)
36     {
37         
38         for(y=barrier.Image[1]->height-2;y>2;y--)
39         {    
40             
41             if(y+barrier.py[1]>=0)
42             {
43                 
44                 r=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x);
45                 g=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x+1);
46                 b=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x+2);
47                 
48                 if(r>=220&&g>=220&&b>=220)  continue;
49                 if(x+barrier.px[1]-delta<0||x+barrier.px[1]-delta>998) continue;
50 
51                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta))=r;
52                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta)+1)=g;
53                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta)+2)=b;
54             }
55             
56         }
57         
58     }
59     
60 }


背景队列:

 1 typedef struct node
 2 {
 3     
 4     ImgType* data;
 5     int NowDelta;
 6     
 7     struct node* next;
 8     
 9 }QueNode;
10 
11 struct HeadTail
12 {
13     
14     QueNode* head;
15     QueNode* tail;
16     
17 }BackQue;
18 
19 void InitBackQue()
20 {
21     
22     BackQue.head=NULL;
23     BackQue.tail=NULL;
24     
25 }
26 
27 QueNode* CreactQue(int interval)
28 {
29     
30     QueNode* p=(QueNode*)malloc(sizeof(QueNode));
31     
32     srand((unsigned)time(NULL));
33     int RandBack=rand()%2;
34     
35     p->data=Img[RandBack];
36     p->NowDelta=p->data->MinDelta+nterval;
37     p->next=NULL;
38     
39     return p;
40 
41 }


正在游戏:

//正在游戏
void Gaming( )
{
    
    IplImage* BackGround=cvLoadImage("E:\\w\\Open Cv\\Background.jpg",1);
    
    cvNamedWindow("Game");
    cvSetMouseCallback("Game",OnMouse);
    
    DefImg_P();
    InitBackQue();

    int status=10;
    int temp=count;
    
    while(1)
    {

    ShowRandBarrier(BackGround);
    
    BirdFly(BackGround,status);

    if(count!=temp){
        
        temp=count;
        status-=20;
    }
    else
        status+=10;

    cvShowImage("Game",BackGround);
    
    cvWaitKey(100);

    cvReleaseImage(&BackGround);
    //释放图像内存空间


    BackGround=cvLoadImage("E:\\w\\Open Cv\\Background.jpg",1);

}

     cvDestroyWindow("Game");
    //释放窗口内存空间
    
}

完整版:

 

  1 #include "stdafx.h"
  2 #include "cv.h"
  3 #include "highgui.h"    
  4 #include "cxcore.h"
  5 #include<stdio.h>
  6 #include<algorithm>
  7 #include "mmsystem.h" 
  8 #include<stdio.h>
  9 #include<stdlib.h>
 10 #include<time.h>
 11 #include<stack>
 12 #pragma comment(lib,"winmm.lib")
 13 
 14 #define INITBACK 0
 15 #define BEGINCUR 1
 16 #define HELPCUR  2
 17 #define QUITCUR  3
 18 #define ABOUTCUR 4
 19 #define BEGIN 5
 20 #define HELPCON 6
 21 #define ABOUTCON 7
 22 #define QUIT 8
 23 #define OTHERS 9
 24     
 25 int BeginBack;
 26 
 27 void HaMouse(int event, int x, int y, int flags, void* param)
 28 {
 29 
 30     if(BeginBack==HELPCON)
 31     {
 32         
 33         if(event==1&&x>=810&&x<=942&&y>=390&&y<=445)
 34             BeginBack=OTHERS;
 35 
 36         return;
 37     
 38     }
 39 
 40     if(BeginBack==ABOUTCON)
 41     {
 42 
 43         if(event==1&&x>=779&&x<=955&&y>=370&&y<=437)
 44             BeginBack=OTHERS;
 45 
 46         return;
 47 
 48     }
 49 }
 50 
 51 void BeginMouse(int event, int x, int y, int flags, void* param)
 52 {
 53     
 54     printf("%d %d\n",x,y);
 55 
 56     if(x>=369&&x<=589&&y>=237&&y<=283)
 57     {
 58 
 59         if(event==1) BeginBack=BEGIN;
 60         else BeginBack=BEGINCUR;
 61 
 62     }
 63 
 64     else if(x>=375&&x<=573&&y>=292&&y<=336) 
 65     {
 66 
 67         if(event==1) BeginBack=HELPCON;
 68         else BeginBack=HELPCUR;
 69 
 70     }
 71     
 72     else if(x>=366&&x<=582&&y>=347&&y<=392) 
 73     {
 74         
 75         if(event==1)  BeginBack=QUIT;
 76         else  BeginBack=QUITCUR;
 77 
 78     }
 79     
 80     else if(x>=380&&y>=401&&x<=568&&y<=445) 
 81     {
 82 
 83         if(event==1) BeginBack=ABOUTCON;
 84         else  BeginBack=ABOUTCUR;
 85 
 86     }
 87         
 88     else BeginBack=INITBACK;
 89 
 90 }
 91 
 92 void BeginGame()
 93 {
 94 
 95     IplImage *BackS=cvLoadImage("E:\\w\\Open Cv\\Initback.png",1);
 96     IplImage *aboutc=cvLoadImage("E:\\w\\Open Cv\\aboutcur.png",1);
 97     IplImage *beginc=cvLoadImage("E:\\w\\Open Cv\\begincur.jpg",1);
 98     IplImage *helpc=cvLoadImage("E:\\w\\Open Cv\\helpcur.png",1);
 99     IplImage *quitc=cvLoadImage("E:\\w\\Open Cv\\quitcur.png",1);
100 
101     cvNamedWindow("Begin");
102 
103     cvSetMouseCallback("Begin",BeginMouse);
104     
105     while(1){
106 
107         if(BeginBack==BEGINCUR)  cvShowImage("Begin",beginc);
108         else if(BeginBack==HELPCUR) cvShowImage("Begin",helpc);
109         else if(BeginBack==QUITCUR) cvShowImage("Begin",quitc);
110         else if(BeginBack==ABOUTCUR) cvShowImage("Begin",aboutc);
111         else if(BeginBack==INITBACK) cvShowImage("Begin",BackS);
112         else break;
113 
114         cvWaitKey(10);
115 
116     }
117 
118     cvReleaseImage(&BackS);
119     cvReleaseImage(&beginc);
120     cvReleaseImage(&helpc);
121     cvReleaseImage(&aboutc);
122     cvReleaseImage(&quitc);
123 
124     cvDestroyWindow("Begin");
125 
126 
127 }
128 
129 void HelpOrAbout()
130 {
131 
132     cvNamedWindow("HorA");
133 
134     cvSetMouseCallback("HorA",HaMouse);
135 
136     IplImage *helpcon=cvLoadImage("E:\\w\\Open Cv\\helpcon.png",1);
137     IplImage *aboutcon=cvLoadImage("E:\\w\\Open Cv\\aboutcon.png",1);
138 
139 
140     while(1){
141             
142         if(BeginBack==HELPCON) cvShowImage("HorA",helpcon);
143         else if(BeginBack==ABOUTCON) cvShowImage("HorA",aboutcon);
144         else break;
145         
146         cvWaitKey(10);
147 
148     }
149 
150     BeginBack=INITBACK;
151 
152     cvReleaseImage(&helpcon);
153     cvReleaseImage(&aboutcon);
154 
155 
156     cvDestroyWindow("HorA");
157 
158 }
159 
160 void BeforeStart()
161 {
162 
163         while(1){
164   
165         if(BeginBack!=BEGIN&&BeginBack!=QUIT) BeginGame();
166         
167         if(BeginBack!=BEGIN&&BeginBack!=QUIT) HelpOrAbout();
168         
169         else break;
170 
171     }
172     
173     
174 }
175 
176 //障碍物的结构
177 typedef struct
178 {
179     
180     IplImage* Image[2];
181     
182     int px[2],py[2];
183     int MaxDelta;
184     int MinDelta;
185     
186 }ImgType;
187 
188 
189 ImgType Img[10];
190 
191 
192 //初始化障碍物数据
193 void DefImg_P()
194 {
195     
196     Img[0].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier.png");
197     Img[0].px[0]=828;
198     Img[0].py[0]=-17;
199     Img[0].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier1.png");
200     Img[0].px[1]=830;
201     Img[0].py[1]=227;
202     Img[0].MaxDelta=1001;
203     Img[0].MinDelta=-156;
204     
205     Img[1].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier1.png");
206     Img[1].px[0]=830;
207     Img[1].py[0]=-5;
208     Img[1].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier.png");
209     Img[1].px[1]=828;
210     Img[1].py[1]=355;
211     Img[1].MaxDelta=1000;
212     Img[1].MinDelta=-156;
213     
214     Img[2].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier3.png");
215     Img[2].px[0]=828;
216     Img[2].py[0]=-17;
217     Img[2].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier2.png");
218     Img[2].px[1]=830;
219     Img[2].py[1]=227;
220     Img[2].MaxDelta=1001;
221     Img[2].MinDelta=-156;
222     
223     Img[3].Image[0]=cvLoadImage("E:\\w\\Open Cv\\barrier2.png");
224     Img[3].px[0]=830;
225     Img[3].py[0]=-5;
226     Img[3].Image[1]=cvLoadImage("E:\\w\\Open Cv\\barrier3.png");
227     Img[3].px[1]=828;
228     Img[3].py[1]=355;
229     Img[3].MaxDelta=1000;
230     Img[3].MinDelta=-156;
231     
232     
233 }
234 
235 int count=0;
236 
237 void OnMouse(int event, int x, int y, int flags, void* param)
238 {
239     
240     if(event==1) count=!count;
241     
242     printf("%d %d\n",x,y);
243 
244 }
245 
246 void BirdFly(IplImage* BackGround,int status)
247 {
248     int px=100,py=100+status;
249     
250     IplImage* bird=cvLoadImage("E:\\w\\Open Cv\\bird1.png",1);
251     
252 
253         for(int x=0;x<bird->width;x++)
254     {
255 
256     
257         for(int y=0;y<bird->height;y++)
258         {
259              
260             int r,g,b;
261             int r1,g1,b1;
262 
263             r=CV_IMAGE_ELEM(bird,uchar,y,3*x);
264             g=CV_IMAGE_ELEM(bird,uchar,y,3*x+1);
265             b=CV_IMAGE_ELEM(bird,uchar,y,3*x+2);
266         
267             r1=CV_IMAGE_ELEM(BackGround,uchar,y+py,3*(x+px));
268             g1=CV_IMAGE_ELEM(BackGround,uchar,y+py,3*(x+px)+1);
269             b1=CV_IMAGE_ELEM(BackGround,uchar,y+py,3*(x+px)+2);
270 
271             if(r1<10&&g1<10&&b1<10&&r!=210&&g!=210&&b!=210)
272                 BeginBack=OTHERS;
273             if(r>=200&&g>=200&&b>=200)
274                 continue;
275 
276             if(y+py<=479){
277 
278             CV_IMAGE_ELEM(BackGround,uchar,(y+py),3*(x+px))=r;
279             CV_IMAGE_ELEM(BackGround,uchar,(y+py),3*(x+px)+1)=g;
280             CV_IMAGE_ELEM(BackGround,uchar,(y+py),3*(x+px)+2)=b;
281             
282         }
283 
284         }
285         
286     }
287     
288 }
289 
290 
291 //将barrier载入背景图中的函数,偏移量为delta
292 void ShowBarrier(ImgType barrier,IplImage* BackGround,int delta)
293 {
294     
295     int x,y;
296     int r,g,b;
297     
298     for(x=barrier.Image[0]->width-2;x>2;x--)
299     {
300         
301         for(y=barrier.Image[0]->height-2;y>2;y--)
302         {    
303             
304             if(y+barrier.py[0]>=0)
305             {
306                 
307                 r=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x);
308                 g=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x+1);
309                 b=CV_IMAGE_ELEM(barrier.Image[0],uchar,y,3*x+2);
310                 
311                 if(r>=220&&g>=220&&b>=220)  continue;
312                 if(x+barrier.px[0]-delta<0||x+barrier.px[0]-delta>998) continue;
313 
314                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta))=r;
315                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta)+1)=g;
316                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[0]),3*(x+barrier.px[0]-delta)+2)=b;
317                 
318             }
319             
320         }
321         
322     }
323     
324     for(x=barrier.Image[1]->width-2;x>2;x--)
325     {
326         
327         for(y=barrier.Image[1]->height-2;y>2;y--)
328         {    
329             
330             if(y+barrier.py[1]>=0&&y+barrier.py[1]<=479)
331             {
332                 
333                 r=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x);
334                 g=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x+1);
335                 b=CV_IMAGE_ELEM(barrier.Image[1],uchar,y,3*x+2);
336                 
337                 if(r>=220&&g>=220&&b>=220)  continue;
338                 if(x+barrier.px[1]-delta<0||x+barrier.px[1]-delta>998) continue;
339 
340                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta))=r;
341                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta)+1)=g;
342                 CV_IMAGE_ELEM(BackGround,uchar,(y+barrier.py[1]),3*(x+barrier.px[1]-delta)+2)=b;
343             }
344             
345         }
346         
347     }
348     
349 }
350 
351 //显示的障碍物的队列的结构
352 typedef struct node
353 {
354     
355     ImgType* data;
356     int NowDelta;
357     
358     struct node* next;
359     
360 }QueNode;
361 
362 //队尾,队首的结构
363 struct HeadTail
364 {
365     
366     QueNode* head;
367     QueNode* tail;
368     
369 }BackQue;
370 
371 
372 //初始化障碍物
373 void InitBackQue()
374 {
375     
376     BackQue.head=NULL;
377     BackQue.tail=NULL;
378     
379 }
380 
381 
382 //创建新的队列结点和删除队首结点
383 QueNode* CreactQue(int interval)
384 {
385     
386     QueNode* p=(QueNode*)malloc(sizeof(QueNode));
387     
388     srand((unsigned)time(NULL));
389     
390     int RandBack=rand()%4;
391     
392     p->data=&Img[RandBack];
393     p->NowDelta=p->data->MinDelta+interval;
394     p->next=NULL;
395     
396     return p;
397 
398 }
399 
400 //随机产生障碍物
401 void ShowRandBarrier(IplImage* BackGround)
402 {
403     
404     int interval=-280;
405     
406     if(BackQue.head==NULL)
407     {
408         
409         int i;
410         BackQue.head=CreactQue(0);
411         BackQue.tail=BackQue.head;
412         
413         for(i=1;i<=3;i++)
414         {
415             
416             BackQue.tail->next=CreactQue(interval*i);
417             BackQue.tail=BackQue.tail->next;
418         
419         }
420         
421     }
422     
423     QueNode* p=BackQue.head;
424     
425     while(p){
426         
427         if(p->NowDelta>p->data->MaxDelta)
428         {
429             
430             QueNode* q=p;
431             p=p->next;
432             free(q);
433             
434             BackQue.tail->next=CreactQue(BackQue.tail->NowDelta+interval/2);
435             
436             BackQue.tail=BackQue.tail->next;
437             BackQue.head=p;
438             
439         }
440             
441         ShowBarrier(*(p->data),BackGround,p->NowDelta);
442         
443         p->NowDelta+=10;
444         p=p->next;
445         
446     }
447     
448 }
449 
450 void Gaming( )
451 {
452     
453     IplImage* BackGround=cvLoadImage("E:\\w\\Open Cv\\Background.jpg",1);
454     
455     cvNamedWindow("Game");
456     cvSetMouseCallback("Game",OnMouse);
457     
458     DefImg_P();
459     InitBackQue();
460 
461     int status=10;
462     int temp=count;
463     
464     while(1)
465     {
466 
467     ShowRandBarrier(BackGround);
468     
469     BirdFly(BackGround,status);
470 
471     if(count!=temp){
472         
473         temp=count;
474         status-=20;
475     }
476     else
477         status+=10;
478 
479     cvShowImage("Game",BackGround);
480     
481     cvWaitKey(100);
482 
483     cvReleaseImage(&BackGround);
484     //释放图像内存空间
485 
486 
487     BackGround=cvLoadImage("E:\\w\\Open Cv\\Background.jpg",1);
488 
489     if(BeginBack==OTHERS)
490         break;
491 
492 }
493 
494      cvDestroyWindow("Game");
495     //释放窗口内存空间
496     
497 }
498 
499 
500 int main(int argc, char* argv[])
501 {
502     
503     BeforeStart();
504 
505     mciSendString("open E:\\w\\Open Cv\\backmusic.mp3 alias back", NULL,0,NULL);
506     mciSendString("play back repeat", NULL, 0,NULL);
507 
508     if(BeginBack==QUIT) return 0;
509     
510     Gaming( );
511 
512     IplImage* BackGround=cvLoadImage("E:\\w\\Open Cv\\gameover.jpg",1);
513     
514     cvNamedWindow("gameover");
515 
516     cvShowImage("gameover",BackGround);
517     
518     cvWaitKey(0);
519 
520     return 0;
521 
522 }

实现效果:
技术分享

技术分享

 

OPENCV图像处理——我的第一个游戏

标签:

原文地址:http://www.cnblogs.com/yanglingwell/p/4386709.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!