鼠标点击
标签: 消灭星星小游戏
学习人数: 7.8k

本节我们将要增加鼠标点击功能

 

 

第一步:绑定窗口

setMouseCallback("PopStarGame", OnMouse, 0);

我们将鼠标回调函数绑定到游戏窗口上

 

第二步:输出鼠标在游戏窗口中的位置信息

需要注意的是,窗口横轴为X轴,竖轴为Y轴,坐标是(x, y),而矩阵坐标是按照(row, col),故窗口中的点(x, y)对应的值在矩阵中为(y, x)

void OnMouse(int event, int x, int y, int flags, void* p) {
	printf("location x:%d y:%d\n", x, y);
}

 

第三步:我们需要判断是否是鼠标左键点击,然后获取点击时的坐标

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 OnMouse(int event, int x, int y, int flags, void* p) {
	if (event == CV_EVENT_LBUTTONDOWN) {//鼠标左键按下 
		printf("location x:%d y:%d\n", x, y);
	}
}

 

第四步:通过坐标计算点击的是第几行第几列的星星

代码如下:

#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[WIDTH][HEIGHT] = { 0 };
Mat image_background;
Mat image_red;
Mat image_yellow;
Mat image_green;
Mat image_purple;
Mat image_blue;

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_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");
	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) {//在背景图片上的偏移量
	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 < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			int x = rand() % 5 + 1;//在1-5之间随机一个数
			mpt[i][j] = x;
		}
	}
}

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);
}

void OnMouse(int event, int x, int y, int flags, void* p) {
	if (event == CV_EVENT_LBUTTONDOWN) {//鼠标左键按下 
		CalcPosition(x, y);
	}
}

int main(int argc, char** argv) {
	Init();
	RandomMap();

	for (int i = 0; i < WIDTH; i++) {
		for (int j = 0; j < HEIGHT; j++) {
			int posY = i * 30;
			int posX = j * 30;
			DrawImage(posX, posY, mpt[i][j]);
		}
	}
	namedWindow("PopStarGame", WINDOW_AUTOSIZE);//WINDOW_AUTOSIZE:系统默认,显示自适应
	setMouseCallback("PopStarGame", OnMouse, 0);

	imshow("PopStarGame", image_background);
	waitKey(0);

	return 0;
}

运行之后效果如下:

点击对应的星星即可输出点击的是第几行第几列的星星。

 

大功告成!!!!!



课后作业

完成本节课的内容


登录后开始许愿

暂无评论,来抢沙发