我们上一节发现没有判断游戏是否结束,那么这一节就要判断游戏是否结束以及计算机游戏得分。
第一步:游戏结束的条件
1、星星全部消除完
2、星星已经没有同色相邻的情况
第二步:写检查是否结束算法
bool CheckGameOver() {//判断游戏是否结束
int star_num = 0;
int flag = 1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] != 0) star_num++;
else continue;
for (int k = 0; k < 4; k++) {
int tx = i + direct[k][0];
int ty = j + direct[k][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[i][j] == mpt[tx][ty]) flag = 0;
}
}
}
if (star_num == 0) return true;
return flag;
}
第三步:增加游戏结束的状态,游戏结束关闭当前界面,弹出游戏结束界面
代码如下:
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<stdio.h>
using namespace cv;
const int WIDTH = 15;
const int HEIGHT = 20;
int mpt[HEIGHT][WIDTH] = { 0 };
Mat image_background;
Mat image_background_new;
Mat image_red;
Mat image_yellow;
Mat image_green;
Mat image_purple;
Mat image_blue;
Mat image_gameover;
enum
{
CV_EVENT_MOUSEMOVE = 0,//移动
CV_EVENT_LBUTTONDOWN = 1,//左键按下
CV_EVENT_RBUTTONDOWN = 2,//右键按下
CV_EVENT_MBUTTONDOWN = 3,//中键按下
CV_EVENT_LBUTTONUP = 4,//左键弹起
CV_EVENT_RBUTTONUP = 5,//右键弹起
CV_EVENT_MBUTTONUP = 6,//中键弹起
CV_EVENT_LBUTTONDBLCLK = 7,//左键点击
CV_EVENT_RBUTTONDBLCLK = 8,//右键点击
CV_EVENT_MBUTTONDBLCLK = 9 //中键点击
};
void Init() { //初始化加载要用到的图片
image_background = imread("./Image/background.png");
image_background_new = imread("./Image/background.png");
image_red = imread("./Image/red.png");
image_yellow = imread("./Image/yellow.png");
image_green = imread("./Image/green.png");
image_purple = imread("./Image/purple.png");
image_blue = imread("./Image/blue.png");
image_gameover = imread("./Image/gameover.png");
if (image_background.empty()) {
printf("could not load image...\n");
exit(1);
}
}
Mat SwitchColor(int color) {//1对应红色星星 2对应黄色星星
switch (color)
{
case 1:
return image_red;
case 2:
return image_yellow;
case 3:
return image_green;
case 4:
return image_purple;
case 5:
return image_blue;
default:
break;
}
}
void DrawImage(int posX, int posY, int color) {//在背景图片上的偏移量
if (color == 0) return;//空白
Mat image_star = SwitchColor(color);
for (int h = 0; h < image_star.rows; h++) {
for (int w = 0; w < image_star.cols; w++) {
Vec3b& carPixel = image_star.at<Vec3b>(h, w);
uchar* ptr = image_background.ptr<uchar>(posX + h, posY + w);
if ((carPixel[0] <= 30) && (carPixel[1] <= 30) && (carPixel[2] <= 30))
continue;//RPG 0,0,0即为黑色 我们需要将星星图片周围偏黑的部分过滤掉
ptr[0] = carPixel[0];
ptr[1] = carPixel[1];
ptr[2] = carPixel[2];
}
}
}
void RandomMap() {//初始化地图
srand(time(NULL));//使用时间做随机种子,每次生成的地图不一样
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int x = rand() % 5 + 1;//在1-5之间随机一个数
mpt[i][j] = x;
}
}
}
int direct[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };//上下左右四个方向
void FindStar(int x, int y, int color) {
for (int i = 0; i < 4; i++) {
int tx = x + direct[i][0];
int ty = y + direct[i][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[tx][ty] == color) {
mpt[tx][ty] = 0;//0表示空
FindStar(tx, ty, color);
}
}
}
void CheckFall() {//下落之后的位置计算
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] == 0) {
for (int k = i; k >= 0; k--) {
if (k != 0) mpt[k][j] = mpt[k - 1][j];
else mpt[k][j] = 0;
}
}
}
}
}
void CheckRight2Left() {//靠左之后的位置计算
for (int j = WIDTH - 1; j >= 0; j--) {
int flag = 0;
for (int i = 0; i < HEIGHT; i++)
if (mpt[i][j] != 0) flag = 1;
if (flag == 0) {
for (int k = j; k < WIDTH; k++) {
for (int i = 0; i < HEIGHT; i++) {
if (k != WIDTH - 1) mpt[i][k] = mpt[i][k + 1];
else mpt[i][k] = 0;
}
}
}
}
}
void MovePosition() {
CheckFall();
CheckRight2Left();
}
void CalcPosition(int x, int y) {
int row = y / 30;
int col = x / 30;
printf("click star at row:%d col:%d\n", row + 1, col + 1);
if (mpt[row][col] != 0) {//点击不为空白
FindStar(row, col, mpt[row][col]);
MovePosition();
}
}
void OnMouse(int event, int x, int y, int flags, void* p) {
if (event == CV_EVENT_LBUTTONDOWN) {//鼠标左键按下
CalcPosition(x, y);
}
}
bool CheckGameOver() {//判断游戏是否结束
int star_num = 0;
int flag = 1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] != 0) star_num++;
else continue;
for (int k = 0; k < 4; k++) {
int tx = i + direct[k][0];
int ty = j + direct[k][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[i][j] == mpt[tx][ty]) flag = 0;
}
}
}
if (star_num == 0) return true;
return flag;
}
void Running() {//游戏引擎
while (1) {
image_background_new.copyTo(image_background);//使用新的背景图
if (CheckGameOver() == true) break;//游戏结束
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int posX = i * 30;
int posY = j * 30;
DrawImage(posX, posY, mpt[i][j]);
}
}
imshow("PopStarGame", image_background);
waitKey(100);//100ms刷新一次
}
}
void GameOver() {
imshow("PopStarGame", image_gameover);
}
int main(int argc, char** argv) {
Init();
RandomMap();
namedWindow("PopStarGame", WINDOW_AUTOSIZE);//WINDOW_AUTOSIZE:系统默认,显示自适应
setMouseCallback("PopStarGame", OnMouse, 0);
Running();
GameOver();
waitKey(0);
return 0;
}
运行之后效果如下:

第四步:增加得分计算
我们得分计算规则如下:
消除分数=消除星星个数的平方*5
那么算法代码如下:
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<stdio.h>
using namespace cv;
const int WIDTH = 15;
const int HEIGHT = 20;
int mpt[HEIGHT][WIDTH] = { 0 };
Mat image_background;
Mat image_background_new;
Mat image_red;
Mat image_yellow;
Mat image_green;
Mat image_purple;
Mat image_blue;
Mat image_gameover;
int score = 0;//分数
int clear_num;//单次消除个数
enum
{
CV_EVENT_MOUSEMOVE = 0,//移动
CV_EVENT_LBUTTONDOWN = 1,//左键按下
CV_EVENT_RBUTTONDOWN = 2,//右键按下
CV_EVENT_MBUTTONDOWN = 3,//中键按下
CV_EVENT_LBUTTONUP = 4,//左键弹起
CV_EVENT_RBUTTONUP = 5,//右键弹起
CV_EVENT_MBUTTONUP = 6,//中键弹起
CV_EVENT_LBUTTONDBLCLK = 7,//左键点击
CV_EVENT_RBUTTONDBLCLK = 8,//右键点击
CV_EVENT_MBUTTONDBLCLK = 9 //中键点击
};
void Init() { //初始化加载要用到的图片
image_background = imread("./Image/background.png");
image_background_new = imread("./Image/background.png");
image_red = imread("./Image/red.png");
image_yellow = imread("./Image/yellow.png");
image_green = imread("./Image/green.png");
image_purple = imread("./Image/purple.png");
image_blue = imread("./Image/blue.png");
image_gameover = imread("./Image/gameover.png");
if (image_background.empty()) {
printf("could not load image...\n");
exit(1);
}
}
Mat SwitchColor(int color) {//1对应红色星星 2对应黄色星星
switch (color)
{
case 1:
return image_red;
case 2:
return image_yellow;
case 3:
return image_green;
case 4:
return image_purple;
case 5:
return image_blue;
default:
break;
}
}
void DrawImage(int posX, int posY, int color) {//在背景图片上的偏移量
if (color == 0) return;//空白
Mat image_star = SwitchColor(color);
for (int h = 0; h < image_star.rows; h++) {
for (int w = 0; w < image_star.cols; w++) {
Vec3b& carPixel = image_star.at<Vec3b>(h, w);
uchar* ptr = image_background.ptr<uchar>(posX + h, posY + w);
if ((carPixel[0] <= 30) && (carPixel[1] <= 30) && (carPixel[2] <= 30))
continue;//RPG 0,0,0即为黑色 我们需要将星星图片周围偏黑的部分过滤掉
ptr[0] = carPixel[0];
ptr[1] = carPixel[1];
ptr[2] = carPixel[2];
}
}
}
void RandomMap() {//初始化地图
srand(time(NULL));//使用时间做随机种子,每次生成的地图不一样
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int x = rand() % 5 + 1;//在1-5之间随机一个数
mpt[i][j] = x;
}
}
}
void AddScore(int num) {
score += (num * num * 5);
}
int direct[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };//上下左右四个方向
void FindStar(int x, int y, int color) {
for (int i = 0; i < 4; i++) {
int tx = x + direct[i][0];
int ty = y + direct[i][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[tx][ty] == color) {
mpt[tx][ty] = 0;//0表示空
clear_num++;
FindStar(tx, ty, color);
}
}
}
void CheckFall() {//下落之后的位置计算
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] == 0) {
for (int k = i; k >= 0; k--) {
if (k != 0) mpt[k][j] = mpt[k - 1][j];
else mpt[k][j] = 0;
}
}
}
}
}
void CheckRight2Left() {//靠左之后的位置计算
for (int j = WIDTH - 1; j >= 0; j--) {
int flag = 0;
for (int i = 0; i < HEIGHT; i++)
if (mpt[i][j] != 0) flag = 1;
if (flag == 0) {
for (int k = j; k < WIDTH; k++) {
for (int i = 0; i < HEIGHT; i++) {
if (k != WIDTH - 1) mpt[i][k] = mpt[i][k + 1];
else mpt[i][k] = 0;
}
}
}
}
}
void MovePosition() {
CheckFall();
CheckRight2Left();
}
void CalcPosition(int x, int y) {
int row = y / 30;
int col = x / 30;
printf("click star at row:%d col:%d\n", row + 1, col + 1);
if (mpt[row][col] != 0) {//点击不为空白
clear_num = 0;
FindStar(row, col, mpt[row][col]);
AddScore(clear_num);
MovePosition();
}
printf("score is : %d\n", score);
}
void OnMouse(int event, int x, int y, int flags, void* p) {
if (event == CV_EVENT_LBUTTONDOWN) {//鼠标左键按下
CalcPosition(x, y);
}
}
bool CheckGameOver() {//判断游戏是否结束
int star_num = 0;
int flag = 1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] != 0) star_num++;
else continue;
for (int k = 0; k < 4; k++) {
int tx = i + direct[k][0];
int ty = j + direct[k][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[i][j] == mpt[tx][ty]) flag = 0;
}
}
}
if (star_num == 0) return true;
return flag;
}
void Running() {//游戏引擎
while (1) {
image_background_new.copyTo(image_background);//使用新的背景图
if (CheckGameOver() == true) break;//游戏结束
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int posX = i * 30;
int posY = j * 30;
DrawImage(posX, posY, mpt[i][j]);
}
}
imshow("PopStarGame", image_background);
waitKey(100);//100ms刷新一次
}
}
void GameOver() {
imshow("PopStarGame", image_gameover);
}
int main(int argc, char** argv) {
Init();
RandomMap();
namedWindow("PopStarGame", WINDOW_AUTOSIZE);//WINDOW_AUTOSIZE:系统默认,显示自适应
setMouseCallback("PopStarGame", OnMouse, 0);
Running();
GameOver();
waitKey(0);
return 0;
}
运行之后效果如下:

第五步:将最终得分显示在游戏结束的界面上
代码如下:
#include<opencv2/opencv.hpp>
#include<stdio.h>
using namespace cv;
const int WIDTH = 15;
const int HEIGHT = 20;
int mpt[HEIGHT][WIDTH] = { 0 };
Mat image_background;
Mat image_background_new;
Mat image_red;
Mat image_yellow;
Mat image_green;
Mat image_purple;
Mat image_blue;
Mat image_gameover;
int score = 0;//分数
int clear_num;//单次消除个数
enum
{
CV_EVENT_MOUSEMOVE = 0,//移动
CV_EVENT_LBUTTONDOWN = 1,//左键按下
CV_EVENT_RBUTTONDOWN = 2,//右键按下
CV_EVENT_MBUTTONDOWN = 3,//中键按下
CV_EVENT_LBUTTONUP = 4,//左键弹起
CV_EVENT_RBUTTONUP = 5,//右键弹起
CV_EVENT_MBUTTONUP = 6,//中键弹起
CV_EVENT_LBUTTONDBLCLK = 7,//左键点击
CV_EVENT_RBUTTONDBLCLK = 8,//右键点击
CV_EVENT_MBUTTONDBLCLK = 9 //中键点击
};
void Init() { //初始化加载要用到的图片
image_background = imread("./Image/background.png");
image_background_new = imread("./Image/background.png");
image_red = imread("./Image/red.png");
image_yellow = imread("./Image/yellow.png");
image_green = imread("./Image/green.png");
image_purple = imread("./Image/purple.png");
image_blue = imread("./Image/blue.png");
image_gameover = imread("./Image/gameover.png");
if (image_background.empty()) {
printf("could not load image...\n");
exit(1);
}
}
Mat SwitchColor(int color) {//1对应红色星星 2对应黄色星星
switch (color)
{
case 1:
return image_red;
case 2:
return image_yellow;
case 3:
return image_green;
case 4:
return image_purple;
case 5:
return image_blue;
default:
break;
}
}
void DrawImage(int posX, int posY, int color) {//在背景图片上的偏移量
if (color == 0) return;//空白
Mat image_star = SwitchColor(color);
for (int h = 0; h < image_star.rows; h++) {
for (int w = 0; w < image_star.cols; w++) {
Vec3b& carPixel = image_star.at<Vec3b>(h, w);
uchar* ptr = image_background.ptr<uchar>(posX + h, posY + w);
if ((carPixel[0] <= 30) && (carPixel[1] <= 30) && (carPixel[2] <= 30))
continue;//RPG 0,0,0即为黑色 我们需要将星星图片周围偏黑的部分过滤掉
ptr[0] = carPixel[0];
ptr[1] = carPixel[1];
ptr[2] = carPixel[2];
}
}
}
void RandomMap() {//初始化地图
srand(time(NULL));//使用时间做随机种子,每次生成的地图不一样
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int x = rand() % 5 + 1;//在1-5之间随机一个数
mpt[i][j] = x;
}
}
}
void AddScore(int num) {
score += (num * num * 5);
}
int direct[4][2] = { 0, 1, 1, 0, 0, -1, -1, 0 };//上下左右四个方向
void FindStar(int x, int y, int color) {
for (int i = 0; i < 4; i++) {
int tx = x + direct[i][0];
int ty = y + direct[i][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[tx][ty] == color) {
mpt[tx][ty] = 0;//0表示空
clear_num++;
FindStar(tx, ty, color);
}
}
}
void CheckFall() {//下落之后的位置计算
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] == 0) {
for (int k = i; k >= 0; k--) {
if (k != 0) mpt[k][j] = mpt[k - 1][j];
else mpt[k][j] = 0;
}
}
}
}
}
void CheckRight2Left() {//靠左之后的位置计算
for (int j = WIDTH - 1; j >= 0; j--) {
int flag = 0;
for (int i = 0; i < HEIGHT; i++)
if (mpt[i][j] != 0) flag = 1;
if (flag == 0) {
for (int k = j; k < WIDTH; k++) {
for (int i = 0; i < HEIGHT; i++) {
if (k != WIDTH - 1) mpt[i][k] = mpt[i][k + 1];
else mpt[i][k] = 0;
}
}
}
}
}
void MovePosition() {
CheckFall();
CheckRight2Left();
}
void CalcPosition(int x, int y) {
int row = y / 30;
int col = x / 30;
printf("click star at row:%d col:%d\n", row + 1, col + 1);
if (mpt[row][col] != 0) {//点击不为空白
clear_num = 0;
FindStar(row, col, mpt[row][col]);
AddScore(clear_num);
MovePosition();
}
printf("score is : %d\n", score);
}
void OnMouse(int event, int x, int y, int flags, void* p) {
if (event == CV_EVENT_LBUTTONDOWN) {//鼠标左键按下
CalcPosition(x, y);
}
}
bool CheckGameOver() {//判断游戏是否结束
int star_num = 0;
int flag = 1;
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (mpt[i][j] != 0) star_num++;
else continue;
for (int k = 0; k < 4; k++) {
int tx = i + direct[k][0];
int ty = j + direct[k][1];
if (tx < 0 || ty < 0 || tx >= HEIGHT || ty >= WIDTH) continue;//超边界
if (mpt[i][j] == mpt[tx][ty]) flag = 0;
}
}
}
if (star_num == 0) return true;
return flag;
}
void Running() {//游戏引擎
while (1) {
image_background_new.copyTo(image_background);//使用新的背景图
if (CheckGameOver() == true) break;//游戏结束
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int posX = i * 30;
int posY = j * 30;
DrawImage(posX, posY, mpt[i][j]);
}
}
imshow("PopStarGame", image_background);
waitKey(100);//100ms刷新一次
}
}
void GameOver() {
char score_string[105] = { 0 };
sprintf_s(score_string, "Game Score is : %d", score);
putText(image_gameover, score_string, Point(30, 100), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(150, 55, 245));
imshow("PopStarGame", image_gameover);
}
int main(int argc, char** argv) {
Init();
RandomMap();
namedWindow("PopStarGame", WINDOW_AUTOSIZE);//WINDOW_AUTOSIZE:系统默认,显示自适应
setMouseCallback("PopStarGame", OnMouse, 0);
Running();
GameOver();
waitKey(0);
return 0;
}
运行之后效果如下:

完成本节课的内容