c++拼图小游戏

发布时间 2023-05-16 22:24:27作者: 满城衣冠

文件目录结构

 

 common.h

#pragma once
#include<iostream>
#include<graphics.h>//图形库
#include<array>//定长数组
#include<algorithm>//乱序算法
#include<functional>//仿函数
#include<ctime>//时间time函数的头文件
using namespace std;
//窗口属性类
//窗口的宽和高
#define WIDTH 800
#define HEIGHT 800
//每一小块图片的大小
#define IMGW 200
#define IMGH 200

game.h

#pragma once
#include "common.h"
#include "window.h"
#include "map.h"
class Game {
public:
Game();
void Run();//玩游戏过程
~Game();
protected:
Map* pM;
Window* pW;
};

map.h

#pragma once
#include "common.h"
class Map {
public:
//类外只能访问公有属性
Map(string whiteURL, string mmURL);
void drawMap();
//获取白块定位
int getWhiteRow();
int getWhiteCols();
//给类外提供一个接口 设置地图的属性
void setValue(int row, int cols, int value);
int getValue(int row, int cols);

protected:
//类外不能访问保护和私有属性
IMAGE whiteIMG;//白色图片
IMAGE mmIMG;//拼图图片
array<array<int, 4>, 4> map;//二维数组4*4定长数组,容器知识

 

};

window.h

#pragma once
#include "common.h"
class Window {
public:
Window(int w, int h, string url="", int flag = 0);//构造函数
void refresh();
~Window();

protected:
int width;
int height;
IMAGE background;
string imgURL;
};

game.cpp

 

#include "game.h"

Game::Game():pW(new Window(WIDTH,HEIGHT)),pM(new Map("white.jpg", "mm2.jpg"))
{

}

void Game::Run()
{
EASYXMSG m;//定义鼠标变量存储鼠标消息
while (1) {
peekmessage(&m, EM_MOUSE);
pM->drawMap();
//白块的行列
int i = pM->getWhiteRow();
int j = pM->getWhiteCols();
if (m.message == WM_LBUTTONDOWN) {
//得到鼠标的行列
int mi = m.y / IMGH;
int mj = m.x / IMGW;
if (i == mi && j + 1 == mj) {
pM->setValue(i, j, pM->getValue(mi, mj));
pM->setValue(mi, mj, 15);
}
if (i == mi && j - 1 == mj) {
pM->setValue(i, j, pM->getValue(mi, mj));
pM->setValue(mi, mj, 15);
}

if (i +1== mi && j == mj) {
pM->setValue(i, j, pM->getValue(mi, mj));
pM->setValue(mi, mj, 15);
}
if (i-1 == mi && j == mj) {
pM->setValue(i, j, pM->getValue(mi, mj));
pM->setValue(mi, mj, 15);
}


}
}
}

Game::~Game()
{
delete pM;
delete pW;
}

map.cpp

 

#include "map.h"

Map::Map(string whiteURL, string mmURL)
{
//加载资源
loadimage(&whiteIMG, whiteURL.c_str(), IMGW, IMGH);
loadimage(&mmIMG, mmURL.c_str(), WIDTH, HEIGHT);
//乱序算法
srand((unsigned int)time(nullptr));
array<int, 16> temp = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,1,15 };
random_shuffle(temp.begin(), temp.end() - 1);
//把一维数组中的值赋给二维数组
int count = 0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
map[i][j] = temp[count++];
}
}

}

void Map::drawMap()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
int x = j * IMGW;
int y = i * IMGH;
//根据数组中的值去贴图
if (map[i][j] == 15) {
putimage(x, y, &whiteIMG);
}
else {
int row = map[i][j] / 4;
int cols = map[i][j] % 4;
int xx = cols * IMGW;
int yy = row * IMGH;
putimage(x, y, IMGW, IMGH, &mmIMG, xx, yy);
}
}
}
}

int Map::getWhiteRow()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (map[i][j] == 15)
return i;
}

}
return -1;
}

int Map::getWhiteCols()
{
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (map[i][j] == 15)
return j;
}

}
return -1;
}

void Map::setValue(int row, int cols, int value)
{
map[row][cols] =value;
}

int Map::getValue(int row, int cols)
{
return map[row][cols];
}

window.cpp

#include "window.h"

Window::Window(int w, int h, string url, int flag)
{
width = w;
height = h;
imgURL = url;
//创建窗口
initgraph(w, h, flag);
if (url.size() != 0) {
loadimage(&background, url.c_str(), w, h);
putimage(0, 0, &background);
}
}

void Window::refresh()
{
if (imgURL.size() != 0) {
putimage(0, 0, &background);
}
else {
cleardevice();
}
}

Window::~Window()
{
closegraph();//关闭窗口
}

testGame.cpp

//窗口类
//地图类
//游戏类
#include "game.h"
int main() {

Game g;
g.Run();

return 0;
}

运行截图: