#include"sakura.h"
int map[4];//记录黑色方块的位置
void random()//初始化一次随机数
{
srand((unsigned)time(NULL));//随机种子
//srand函数的格式为(unsigned int seed)
//这里以时间为变量
for (int i = 0; i < 4; i++)
{
map[i] = rand() % 4;//利用数组下标确定行数,利用元素的值确定列数
//x%yd的值位于0~y-1;
}
}
void drawmap()//绘图函数
{
BeginBatchDraw();//开始批量绘图
setlinecolor(BLUE);
setfillcolor(WHITE);
for (int i = 0; i < 4; i++)//代表行数(y坐标)
{
for (int j = 0; j < 4; j++)//代表列数(x坐标)
{
fillrectangle(j * 100, i * 120, (j + 1) * 100, (i + 1) * 120);
}
}//白色方块已全部绘制完毕
setfillcolor(BLACK);
for (int i = 0; i < 4; i++)//i为行数为y坐标
{
fillrectangle(map[i] * 100, i * 120, (map[i] + 1) * 100, (i + 1) * 120);
}
EndBatchDraw();//结束批量绘图
}
int play()
{
MOUSEMSG msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN)
{
int x = msg.x / 100;//记录点击的方块所处的列
int y = msg.y / 120;//记录点击的方块所处的行
if (x == map[3] && y == 3)
{
for (int m = 3; m > 0; m--)
{
map[m] = map[m - 1];//将上一行向下推移
}
srand((unsigned)time(NULL));
map[0] = rand() % 4;
}
else
{
return 0;
}
}
return 1;
}
void start()//开始游戏
{
initgraph(4 * 100, 4 * 120);
while (1)
{
drawmap();
if (0 == play())
{
TCHAR str[60] = _T("就这就这,这不是有手就行");
MessageBox(GetHWnd(), str, _T("游戏结束!"), MB_OK);
//打开一个弹窗,提示相关信息(获取窗口句柄,输出字符串到弹窗,窗口标题,MB_OK为确认按钮)
break;
}
}
//getchar();//卡住窗口
closegraph();
}
int main()
{
random();
start();//打开图像
return 0;
}