同学们学到这里已经能自己写一个带图形界面的小游戏出来了,首先要恭喜大家。
但是,我们这个小游戏依然十分的简陋,除了功能十分少之外,还存在着一些不易发现的问题。
功能上的缺失
这个留给同学自行去扩展更丰富的玩法,比如关卡,界面的美化等等。
我们需要在游戏结束时将申请的Image和Window资源统一释放掉,养成良好的编码习惯。
#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);
}
void Destroy() {
destroyWindow("PopStarGame");
image_background.release();
image_background_new.release();
image_red.release();
image_yellow.release();
image_green.release();
image_purple.release();
image_blue.release();
image_gameover.release();
}
int main(int argc, char** argv) {
Init();
RandomMap();
namedWindow("PopStarGame", WINDOW_AUTOSIZE);//WINDOW_AUTOSIZE:系统默认,显示自适应
setMouseCallback("PopStarGame", OnMouse, 0);
Running();
GameOver();
waitKey(0);
Destroy();
return 0;
}
做游戏必然逃不掉一个对手,那就是外挂,不过由于是单机游戏,其实只要简单做一些反调试、反注入的措施就可以了,偷懒的话也可以不做。
如果是网络游戏,那么就要考虑外挂对游戏平衡性和游戏体验的影响了。
最后,恭喜同学们成功学完这个课程~如果你觉得对你有所帮助,记得把小诺推荐给更多的同学哦~
完成本节课的内容