鼠标操作是用c++写的,放在主函数里运行需要反应几十秒,甚至几分钟才执行操作
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
#include<math.h>
#include<conio.h>
#include<Windows.h>
#include"button.h"
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
#define GPIO_NUM 4
#define GPIO_W 100
#define INTERVA 17
#define GPIO_M GPIO_NUM*GPIO_W+(GPIO_NUM+1)*INTERVA
int map[GPIO_NUM][GPIO_NUM];
enum color
{
zero=RGB(205,193,180),
twoto1=RGB(238,228,218),
twoto2=RGB(237,224,200),
twoto3=RGB(242,177,121),
twoto4=RGB(245,149,99),
twoto5=RGB(246,124,95),
twoto6=RGB(246,94,59),
twoto7=RGB(242,177,121),
twoto8=RGB(237,204,97),
twoto9=RGB(255,0,128),
twoto10=RGB(145,0,72),
twoto11=RGB(242,17,158),
back=RGB(187,173,160),
};
color arr[13] = { zero,twoto1,twoto2,twoto3,twoto4,twoto5,twoto6,twoto7,twoto8,twoto9,twoto10,twoto11,back };
int createNum()
{
srand((unsigned)time(NULL) + clock());
if (rand() % 10 == 1)
{
return 4;
}
else
{
return 2;
}
}
void ranNum()
{
while (1)
{
int r = rand() % GPIO_NUM;
int c = rand() % GPIO_NUM;
if (map[r][c] == 0)
{
map[r][c] = createNum();
break;
}
}
}
void gameInit()
{
srand((unsigned)time(NULL) + clock());
for (int i= 0;i<2;)
{
int r = rand()%GPIO_NUM;
int c = rand()%GPIO_NUM;
if (map[r][c] == 0)
{
map[r][c] = createNum();
i++;
}
map[r][c] = createNum();
}
}
void gameDraw()
{
setbkcolor(RGB(187, 173, 160));
cleardevice();
for (int i=0;i<GPIO_NUM;i++)
{
for (int k=0;k<GPIO_NUM;k++)
{
int x = k * GPIO_W + (k + 1) * INTERVA;
int y = i * GPIO_W + (i + 1) * INTERVA;
int index = (int)log2((double)map[i][k]);
COLORREF tcolor = arr[index];
setfillcolor(tcolor);
solidroundrect(x, y, x + GPIO_W, y + GPIO_W, 20, 20);
if (map[i][k] != 0)
{
settextstyle(50, 0, "黑体");
setbkmode(TRANSPARENT);
settextcolor(RGB(119, 110, 101));
char str[10] = "";
sprintf_s(str,"%d",map[i][k]);
int tw = textwidth(str);
int th = textheight(str);
int tx =(GPIO_W-tw)/2;
int ty = (GPIO_W-th)/2;
outtextxy(x+tx, y+ty, str);
printf("%d", map[i][k]);
}
printf("\n");
}
}
}
void moveUP()
{
for (int i = 0; i < GPIO_NUM; i++)
{
int temp = 0;
for (int begin = 1; begin < GPIO_NUM; begin++)
{
if (map[begin][i] != 0)
{
if (map[temp][i] == 0)
{
map[temp][i] = map[begin][i];
map[begin][i] = 0;
}
else if (map[temp][i] == map[begin][i])
{
map[temp][i] += map[begin][i];
map[begin][i] = 0;
temp++;
}
else
{
map[temp + 1] [i] = map[begin][i];
if (temp + 1 != begin)
{
map[begin][i] = 0;
}
temp++;
}
}
}
}
}
void moveDOWN()
{
for (int i = 0; i < GPIO_NUM; i++)
{
int temp = 3;
for(int begin=2;begin>=0;begin--)
if (map[begin][i] != 0)
{
if (map[temp][i] == 0)
{
map[temp][i] = map[begin][i];
map[begin][i] = 0;
}
else if (map[temp][i] == map[begin][i])
{
map[temp][i] += map[begin][i];
map[begin][i] = 0;
temp--;
}
else
{
map[temp - 1][i] = map[begin][i];
if (temp - 1 != begin)
{
map[begin][i] = 0;
}
temp--;
}
}
}
}
void moveRIGHT()
{
for (int i = 0; i < GPIO_NUM; i++)
{
int temp = 3;
for (int begin = 2; begin >=0; begin--)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
temp--;
}
else
{
map[i][temp -1] = map[i][begin];
if (temp -1 != begin)
{
map[i][begin] = 0;
}
temp--;
}
}
}
}
}
void moveLEFT()
{
for (int i = 0; i < GPIO_NUM; i++)
{
int temp = 0;
for (int begin = 1; begin < GPIO_NUM; begin++)
{
if (map[i][begin] != 0)
{
if (map[i][temp] == 0)
{
map[i][temp] = map[i][begin];
map[i][begin] = 0;
}
else if (map[i][temp] == map[i][begin])
{
map[i][temp] += map[i][begin];
map[i][begin] = 0;
temp++;
}
else
{
map[i][temp + 1] = map[i][begin];
if (temp + 1 != begin)
{
map[i][begin] = 0;
}
temp++;
}
}
}
}
}
void keyDeal()
{
char key = _getch();
switch (key)
{
case 'w':
case 'W':
case 72:
moveUP();
ranNum();
break;
case 's':
case 'S':
case 80:
moveDOWN();
ranNum();
break;
case 'a':
case 'A':
case 75:
moveLEFT();
ranNum();
break;
case 'd':
case 'D':
case 77:
moveRIGHT();
ranNum();
break;
}
}
Button::Button(int x, int y, int x1, int y1, COLORREF color,string textString,string textStyle)
{
this->x = x;
this->y = y;
this->x1 = x1;
this->y1 = y1;
this->color = color;
this->textString = textString;
this->textStyle = textStyle;
}
void Button::drawButton()
{
setfillcolor(this->color);
fillrectangle(x, y, x1, y1);
setbkmode(TRANSPARENT);
settextcolor(BLACK);
settextstyle(30, 0, textString.c_str());
outtextxy(x+10 , y+5 , textString.c_str());
}
bool Button::isInButton(MOUSEMSG m)
{
if (m.x>=x&&m.x<=x1&&m.y>=y&&m.y<=y1)
return true;
return false;
}
bool Button::clickButton(MOUSEMSG m)
{
if (isInButton(m))
{
if (m.uMsg == WM_LBUTTONDOWN)
{
return true;
}
}
return false;
}
COLORREF& Button::getColor()
{
return color;
}
string& Button::getText()
{
return textString;
}
int main()
{
MOUSEMSG m;
initgraph(GPIO_M, GPIO_M+40,SHOWCONSOLE);
gameInit();
Button *reset = new Button(174, 478, 321, 518, RED, "重新开始", "黑体");
createNum();
while (1)
{
gameDraw();
m= GetMouseMsg();
reset->drawButton();
if (reset->clickButton(m))
{
PlaySound("C:\\Users\\18236\\Desktop\\1.wav", NULL, SND_FILENAME | SND_ASYNC | SND_LOOP);
}
keyDeal();
}
closegraph();
return 0;
}
//下面的是我定义的头文件声明
#include<graphics.h>
#include<conio.h>
#include<iostream>
#include<string>
using namespace std;
class Button
{
public:
Button(int x, int y, int x1, int y1, COLORREF color, string textString, string textStyle);
void drawButton();
bool isInButton(MOUSEMSG m);
bool clickButton(MOUSEMSG m);
COLORREF& getColor();
string& getText();
protected:
int x;
int y;
int x1;
int y1;
COLORREF color;
string textString;
string textStyle;
};
你好,请问解决了吗
把代码发出来看看,你的描述无法了解问题所在。