#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<easyx.h> #include<stdlib.h> #include<time.h>
#define INTERVAL 100 #define GRID_W 100 #define GRID_H 150
int flags[4] = { 0 };
int gCount = 0; void init() { srand((unsigned int)time(NULL)); for (int i = 0; i < 4; i++) { flags[i] = rand() % 4; }
}
void draw() { const char* title = "别踩白块儿"; setlinestyle(PS_SOLID, 2); setlinecolor(BLACK);
for (int i = 0; i < 5; i++) { line(0, i * 150 + INTERVAL, 400, i * 150 + INTERVAL); line(100 * i, INTERVAL, i * 100, 700); }
settextcolor(BLACK); settextstyle(38, 0, "Arial"); int spaceW = (getwidth() - textwidth(title)) / 2; int spaceH = (INTERVAL - textheight(title)) / 2; outtextxy(spaceW, spaceH, title);
setfillcolor(BLACK); for (int i = 0; i < 4; i++) { int x = flags[i] * GRID_W; int y = i * GRID_H + INTERVAL; if (i == 3) { setfillcolor(RGB(156, 156, 156));
} fillrectangle(x, y, x + GRID_W, y + GRID_H); }
settextstyle(26, 0, "微软雅黑"); char score[30] = { 0 }; sprintf_s(score,"Score:%d", gCount);
outtextxy(20, 40, score); }
bool mousePressMsg(ExMessage* msg) { int x = flags[2]*GRID_W; int y = 2 * GRID_H + INTERVAL; if (msg->x > x && x < x + GRID_W && msg->y > y && msg->y < y + GRID_H) { for (int i = 3; i > 0; i--) { flags[i] = flags[i - 1]; } flags[0] = rand() % 4; gCount++; printf("你点击了正确的黑块\n"); } else { return false; } return true; }
void gameOverHit(int w, int h) { setlinecolor(GREEN); setfillcolor(RGB(93, 107, 153));
int spaceH = (getwidth() - w) / 2; int spaceV = (getwidth() - h) / 2; fillrectangle(spaceH, spaceV, spaceH + w, spaceV + h); int mid_w = (spaceH + w) / 2; int mid_h = (2*spaceV+h) / 2; outtextxy(mid_w, mid_h, "lyx is sb"); } int main() { initgraph(400, 700, EX_SHOWCONSOLE); setbkcolor(WHITE); cleardevice(); setbkmode(TRANSPARENT); init(); draw();
bool isDone = false; while (!isDone) { ExMessage msg = { 0 }; if (peekmessage(&msg)) { switch (msg.message) { case WM_KEYDOWN: if (msg.vkcode == VK_ESCAPE) { printf("quit\n"); isDone = true; } break; case WM_LBUTTONDOWN: if (!mousePressMsg(&msg)) { isDone = true; }; default: break; } } BeginBatchDraw(); cleardevice(); draw();
EndBatchDraw(); } gameOverHit(200,150);
getchar(); return 0; }
|