///*----------------------------------------别踩白块--------------------------------------------------*/
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include<easyx.h>
#include<time.h>
#include<string.h>
#pragma comment(lib,"winmm.lib")
using namespace std;
IMAGE pict;
//随机数
int arr[4];
void ramd()
{
srand((unsigned)time(NULL)); //以时间为随机数
for (size_t i = 0; i < 4; i++)arr[i] = rand() % 4; //产生的随机数组值。
}
//创建游戏窗口
void init_map()
{
initgraph(400, 480);
loadimage(&pict,100,120); //设置图片的格式。
}
//初始化窗口
void init_wind()
{
BeginBatchDraw();
cleardevice();
setlinecolor(GREEN);
setfillcolor(WHITE);
for (size_t i = 0; i <4; i++)
{
for (size_t j = 0; j < 4; j++)
{
fillrectangle(i * 100, j * 120, (i + 1) * 100, (j + 1) * 120); //绘制4*4的白块。
}
}
setfillcolor(RED); //随机红块
for (size_t i = 0; i < 4; i++)
{
//fillrectangle(arr[i] * 100, i * 120, (arr[i] + 1) * 100, (i + 1) * 120); //每行随机生产1个红块。
putimage(arr[i] * 100, i * 120, &pict); //放入图片
}
EndBatchDraw();
}
int score;
//鼠标操作
bool opera()
{
MOUSEMSG msg = GetMouseMsg();//获取鼠标信息
int m = msg.x;
int n = msg.y;
if (n >= 360 && n <= 480)
{
switch (msg.uMsg) //当前接收到的信息
{
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
if (arr[3] * 100 < m && m < (arr[3] + 1) * 100)
{
for (size_t i = 3; i > 0; i--) arr[i] = arr[i - 1];
arr[0] = rand() % 4;
init_wind();
score += 10;
}
else return false;
break;
default:return true;
break;
}
}
}
//游戏积分
void play()
{
char s[50];
sprintf(s, "当前得分:%d", score);
outtextxy(10, 60, s); //用于在指定位置输出字符串
if (opera() == false)
{
TCHAR txt[50];
wsprintf(txt, "游戏失败,得分:%d", score);
if (MessageBox(GetHWnd(), txt, "是否要继续", MB_YESNO) == IDYES)
{
ramd();
init_wind();
score = 0;
}
else exit(0);
}
if (score == 100)
{
TCHAR txt[50];
wsprintf(txt, "游戏过关,得分:%d", score);
if (MessageBox(GetHWnd(),txt, "是否要继续", MB_YESNO) == IDYES)
{
ramd();
init_wind();
score = 0;
}
else exit(0);
}
}
int main()
{
init_map();
ramd();
init_wind();
while (true)play();
system("pause");
return 0;
}