块儿小游戏,超基础新手项目"/>
C++别踩白块儿小游戏,超基础新手项目
#适用于vs2022,最新版EasyX图形库
#通过调用EasyX图形库制作,项目简单易上手
#include<stdio.h>
#include<easyx.h>
#include<stdlib.h>
#include<time.h>
#include <iostream>
#define INTERVAL 100
#define GRID_W 100
#define GRID_H 150int flags[4] = { 2,1,0,3 };int gCount = 0;void init()
{srand(time(NULL));for (int i = 0; i < 4; i++){flags[i] = rand() % 4;}
}void draw()
{setlinestyle(PS_SOLID, 2);setlinecolor(BLACK);const char* title = "别踩白块儿";for (int i = 0; i < 5; i++){line(0, i* GRID_H + INTERVAL , 400, i* GRID_H + INTERVAL);line(i * GRID_W, INTERVAL, i * GRID_W, 700);}settextstyle(38, 0, "Arial");settextcolor(BLACK);int spaceH = (getwidth() - textwidth(title))/ 2;int spaceV = (INTERVAL - textheight(title)) / 2;outtextxy(0 + spaceH, spaceV, 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 && msg->x < x + GRID_W && msg->y < y + GRID_H){for (int i = 3; i>0; i--){flags[i] = flags[i - 1];}flags[0] = rand() % 4;gCount++;printf("你点击了正确的黑块!\n");return true;}return false;
}void gameOverHit(int w, int h)
{setlinecolor(GREEN);setlinecolor(RGB(93,107,153));int spaceH = (getwidth() - w) / 2;int spaceV = (getheight() - h) / 2;fillrectangle(spaceH, spaceV, spaceH + w, spaceV + h);outtextxy(spaceH, spaceV, "Game Over!");
}int main()
{initgraph(400, 700, EX_SHOWCONSOLE);setbkcolor(WHITE);cleardevice();setbkmode(TRANSPARENT);init();ExMessage msg = { 0 };bool isDone = false;while (!isDone){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;}break;}}BeginBatchDraw();cleardevice();draw();EndBatchDraw();}gameOverHit(200,100);getchar();return 0;
}
最终效果如图
设定为可点击第二行,且点击时才可移动
实用性较差,仅用于新手练习代码逻辑。
更多推荐
C++别踩白块儿小游戏,超基础新手项目
发布评论