Opencv开发实现拼图游戏

时间:2022-11-23 13:28:48

本文实例为大家分享了Opencv开发实现拼图游戏的具体代码,供大家参考,具体内容如下

一、代码

?
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
#include<opencv2/opencv.hpp>
#include<iostream>
#include<stdlib.h>
#include<time.h>
 
#define PL 800
#define PW 600
#define EAZY 50
 
 
using namespace std;
using namespace cv;
 
const Size Dsize = Size (PL,PW);
const Size dsize = Size (PL+9,PW+6);
const string SF="D:\\code\\c\\opencv\\opencv_face_detection_database\\sample\\1.jpg";
int  readimg(Mat &img);
void department(Mat img,int imgmap[4][4],Mat gameimg[16]);
void radom(int imgmap[4][4]);
void showimg(Mat img[16],int imgmap[4][4]);
void moveimg(int imgmap[4][4]);
int gameover(int imgmap[4][4]);
int judge(char ch);
int fx,fy;
 
int  readimg(Mat &img)
{
    Mat gameimg=imread(SF,1);
    if(gameimg.empty())
    {
        cout<<SF<<endl<<"Fail to open the file!"<<endl;
        return 0;
    }
    resize(gameimg,img,Dsize);
 
    return 1;
}
 
void department(Mat img,int imgmap[4][4],Mat gameimg[16])
{
    Mat whiteimg=Mat(150,200,CV_8UC3,Scalar(255,255,255));
    Mat new_img;
    Rect rec;
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            rec.width=200;
            rec.height=150;
            rec.x=i*200;
            rec.y=j*150;
            if(i*j!=9)
                new_img=img(rec);
            else
                new_img=whiteimg;
            gameimg[i*4+j]=new_img;
            waitKey(100);
            imgmap[i][j]=i*4+j;
        }
    }
 
    fx=3,fy=3;
    return ;
}
 
int judge(char ch)
{
    if(ch!='w'&&ch!='W')
        if(ch!='S'&&ch!='s')
            if(ch!='a'&&ch!='A')
                if(ch!='d'&&ch!='D')
                    return 0;
    return 1;
}
 
void moveimg(int imgmap[4][4],char ch)
{
    int x,y,mid;
    switch(ch)
    {
    case 'w':
    case 'W':
        x=fx;
        y=fy-1;
        if(y<0)
            return ;
        break;
 
    case 'a':
    case 'A':
        x=fx-1;
        y=fy;
        if(x<0)
            return ;
        break;
 
    case 'S':
    case 's':
        x=fx;
        y=fy+1;
        if(y>3)
            return ;
        break;
 
    case 'd':
    case 'D':
        x=fx+1;
        y=fy;
        if(x>3)
            return ;
        break;
 
    }
    mid=imgmap[fx][fy];
    imgmap[fx][fy]=imgmap[x][y];
    imgmap[x][y]=mid;
    fx=x,fy=y;
    return ;
}
 
void radom(int imgmap[4][4])
{
    int x=99307,y=77431;
    int t;
    srand((unsigned)time(NULL));
    t=rand();
    char ch;
    for(int i=1; i<EAZY; i++)
    {
 
        t=(t+x)%4;
        switch(t)
        {
        case 0:
            ch='a';
            break;
        case 1:
            ch='s';
            break;
        case 2:
            ch='w';
            break;
        case 3:
            ch='d';
            break;
        }
        moveimg(imgmap,ch);
        x=x*x%y;
    }
 
}
 
void showimg(Mat *img,int imgmap[4][4])
{
    Mat gameimg=Mat(dsize,CV_8UC3,Scalar(0,0,0));
    Rect rec;
 
    for(int i=0; i<4; i++)
    {
        for(int j=0; j<4; j++)
        {
            rec.width=200;
            rec.height=150;
            rec.x=i*203;
            rec.y=j*152;
            img[imgmap[i][j]].copyTo(gameimg(rec));
        }
    }
    imshow("game",gameimg);
    waitKey(1000);
    return ;
}
 
int gameover(int imgmap[4][4])
{
    int s=0;
    for(int i=0;i<4;i++)
        for(int j=0;j<4;j++)
        if(imgmap[i][j]==i*4+j)
        s++;
    if(s==16)
        return 1;
    return 0;
}
int main()
{
    Mat img;
    Mat gameimg[16];
    int imgmap[4][4]= {};
    char ch;
    if(!readimg(img))
        return 0;
    imshow("img",img);
    waitKey(2000);
    destroyWindow("img");
 
    department(img,imgmap,gameimg);
    radom(imgmap);
 
    while(1)
    {
        showimg(gameimg,imgmap);
        if(gameover(imgmap))
            {
                destroyWindow("game");
                imshow("img",img);
                waitKey(1000);
                break;
            }
        ch=getchar();
        while(!judge(ch))
            ch=getchar();
        moveimg(imgmap,ch);
    }
    return 0;
 
}

二、输入输出说明

1、const string SF 为拼图图片路径
2、w/s/d/a 用于方向操作
3、每次输入方向操作符,都要输入回车键作为确认
4、在每次运行前,都会展示3秒原图片

三、存在的问题

1、在用鼠标进行拖拽窗口时,可能会提示无响应,等待一段时间就可以继续操作了。
2、在修改参数时,注意部分常数是应该与参数同时变化的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/thank3194003824/article/details/117988794