灵感来源:云空调,便携小空调-后记
common.h
#pragma once
#include <graphics.h>
#include <iostream>
#include <map>
#include <string>
using namespace std;
c++空调.cpp
#include"DrawImg.h"
#include"res.h"
#include"button.h"
int main()
{
Draw draw;
Button* pp = new Button(); //按钮
ExMessage msg; //定义鼠标消息
initgraph(900, 600);
setbkcolor(WHITE);
cleardevice();
while (true)
{
while (peekmessage(&msg, EM_MOUSE)) //获取鼠标消息
{
pp->ClickButton(msg);
}
BeginBatchDraw();
draw.GameDraw(); //绘制
pp->draw();
EndBatchDraw();
}
}
button.h
#pragma once
#include "common.h"
#include"DrawImg.h"
class Button
{
public:
Button();
void ClickButton(ExMessage msg);
int& getTemp() { return tempCnt; }
void draw();
protected:
int tempCnt = 16; //温度变量
bool showWitch=false; //开关
bool showCool = false;
bool showHot = false;
bool showTempUp = false; //温度
bool showTempDown = false;
};
button.cpp
#include "button.h"
#include"common.h"
#include"res.h"
Button::Button() {}
void Button::ClickButton(ExMessage msg)
{
if (msg.message == WM_LBUTTONDOWN)
{
//开关
if ((msg.x > 418 && msg.y >= 340) && (msg.x <= 488 && msg.y <= 410))
{
showWitch = !showWitch;
}
//冷
if ((msg.x > 318 && msg.y >= 340) && (msg.x <= 388 && msg.y <= 410))
{
showCool = true;
showHot = false;
getTemp() = 16;
}
//热
if ((msg.x > 520 && msg.y >= 340) && (msg.x <= 610 && msg.y <= 410))
{
showHot = true;
showCool = false;
getTemp() = 30;
}
//温度控制 上 温度从16度开始
//int ge=6, shi=1;
if ((msg.x > 418 && msg.y >= 420) && (msg.x <= 488 && msg.y <= 490))
{
showTempUp = true;
}
//温度控制 下
if ((msg.x > 418 && msg.y >= 500) && (msg.x <= 488 && msg.y <= 570))
{
showTempDown = true;
}
}
}
void Button::draw()
{
/*setlinecolor(RED);*/
/*rectangle(418, 420, 488, 490);*/
//开关
if (showWitch)
{
Res::DrawIMG(110, 235, "风", 1);
Res::DrawIMG(0, 0, "空调", 1);
Res::Draw_IMG(771, 86, to_string(getTemp() / 10 % 10) + "_");
Res::Draw_IMG(795, 86, to_string(getTemp() % 10) + "_");
Res::DrawIMG(788, 40, "标识", 1);
Res::Draw_IMG(818, 86, "摄氏度");
Res::DrawIMG(418, 340, "开关", 1);
//冷
if (showCool)
{
Res::DrawIMG(788, 40, "标识", 1);
Res::Draw_IMG(818, 86, "摄氏度");
}
//热
if (showHot)
{
Res::DrawIMG(788, 40, "标识", 0);
Res::Draw_IMG(818, 86, "摄氏度");
}
//温度控制 上 温度从16度开始
//int ge=6, shi=1;
if (showTempUp && getTemp() <= 30)
{
getTemp()++;
//Res::Draw_IMG(771, 86, to_string(getTemp() / 10 % 10) + "_");
//Res::Draw_IMG(795, 86, to_string(getTemp() % 10) + "_");
//Res::Draw_IMG(818, 86, "摄氏度");
ge = getTemp() % 10; //16%10==6 19%10==9 20%10==0 21%10==2 22%10==2
shi = getTemp() / 10 % 10;
showTempUp = false;
}
//温度控制 下
if (showTempDown && getTemp() >= 17)
{
getTemp()--;
//Res::Draw_IMG(771, 86, to_string(getTemp() / 10 % 10) + "_");
//Res::Draw_IMG(795, 86, to_string(getTemp() % 10) + "_");
//Res::Draw_IMG(818, 86, "摄氏度");
showTempDown = false;
}
}
}
res.h
#pragma once
#include "common.h"
class Res
{
private:
Res();
public:
static int WIDTH(string name);
static int HEIGHT(string name);
static Res* GetInstance();
static void Draw_IMG(int x, int y, string name);
static void DrawIMG(int x, int y, string name, int preIndex); //图片绘制
~Res();
public:
static map<string, IMAGE*> img; //图片资源
};
res.cpp
#include "res.h"
map<string, IMAGE*> Res::img; //图片
/*———————————————————————————————————————————
加载 和 初始化 图片相关内容
————————————————————————————————————————————*/
Res::Res()
{
//空调
string background[2] = { "./res/AirConditionerfir.png" ,"./res/AirConditionersec.png" };
//风
string blow[2] = { "./res/Blow0.png" ,"./res/Blow1.png"};
//开关
string switches[2] = {"./res/Open.png" ,"./res/Off.png"};
//标识
string flag[2] = { "./res/Sun.png","./res/Ice.png" };
//冷热按钮
string button[2] = { "./res/IceButton.png","./res/SunButton.png" };
//温度按钮
string temp[2] = { "./res/Up.png","./res/Down.png" };
//摄氏度
string C = "./res/number/temp.png";
//温度 0-9
for (int i = 0; i <= 9; i++)
{
string init = "./res/number/";
string strURY = "0";
strURY[0] += i;
strURY += "_1.png";
strURY = init + strURY;
img[to_string(i - 0) + "_"] = new IMAGE; //img["0_"]
loadimage(img[to_string(i - 0) + "_"], strURY.c_str());
//cout << strURL << endl;
}
img["空调"] = new IMAGE[2];
img["风"] = new IMAGE[2];
img["开关"] = new IMAGE[2];
img["标识"] = new IMAGE[2];
img["冷热按钮"] = new IMAGE[2];
img["温度按钮"] = new IMAGE[2];
img["摄氏度"] = new IMAGE;
loadimage(img["摄氏度"] , C.data());
for (int i = 0; i < 2; i++)
{
loadimage(img["空调"] + i, background[i].data());
}
for (int i = 0; i < 2; i++)
{
loadimage(img["风"] + i, blow[i].data());
}
for (int i = 0; i < 2; i++)
{
loadimage(img["开关"] + i, switches[i].data(), 70,70);
}
for (int i = 0; i < 2; i++)
{
loadimage(img["标识"] + i, flag[i].data());
}
for (int i = 0; i < 2; i++)
{
loadimage(img["冷热按钮"] + i, button[i].data(), 70, 70);
}
for (int i = 0; i < 2; i++)
{
loadimage(img["温度按钮"] + i, temp[i].data(), 70, 70);
}
}
//获取图片的高度
int Res::WIDTH(string name)
{
return GetInstance()->img[name]->getwidth();
}
int Res::HEIGHT(string name)
{
return GetInstance()->img[name]->getheight();
}
Res* Res::GetInstance()
{
static Res* res = new Res;
return res;
}
//贴图
void Res::Draw_IMG(int x, int y, string name)
{
putimage(x, y, GetInstance()->img[name]);
}
void Res::DrawIMG(int x, int y, string name, int preIndex)
{
putimage(x, y, GetInstance()->img[name] + preIndex);
}
Res::~Res()
{
delete img["摄氏度"];
delete[]img["空调"];
delete[]img["风"];
delete[]img["开关"];
delete[]img["标识"];
delete[]img["冷热按钮"];
delete[]img["温度按钮"];
for (int i = 0; i < 9; i++)
{
delete img[to_string(i - 0)];
delete img[to_string(i - 0) + "y"];
}
}
DrawImg.h
#pragma once
#include "common.h"
class Draw
{
public:
void GameDraw();
};
DrawImg.cpp
#include"res.h"
#include "DrawImg.h"
void Draw::GameDraw()
{
Res::DrawIMG(0, 0, "空调",0);
Res::DrawIMG(318, 340, "冷热按钮", 0);
Res::DrawIMG(520, 340, "冷热按钮", 1);
Res::DrawIMG(418, 340, "开关", 0);
Res::DrawIMG(418, 420, "温度按钮", 0);
Res::DrawIMG(418, 500, "温度按钮", 1);
Res::DrawIMG(110, 235, "风", 0);
}