[C++] 纯文本查看 复制代码
#include <graphics.h>
#include <math.h>
void drawR(int x, int y, bool p, int g = 10) {
color_t c = EGEACOLOR(0xFF, DARKGRAY);
if (!p) c = EGEACOLOR(0xFF, WHITE);
setfillcolor(c);
ege_fillrect(x * g, y * g, g, g);
setlinewidth(0.5);
setcolor(EGEACOLOR(0x24, BLACK));
ege_rectangle(x * g, y * g, g, g);
}
void walk(int &r, bool s, int &x, int &y) {
if (s)
r = (r + 1) % 4;
else
r = (r + 3) % 4;
x += round(sin(r * 3.1415926 / 2));
y += round(cos(r * 3.1415926 / 2));
}
int main(int argc, char *argv[]) {
int width = 600;
int height = 600;
int n = 6;
if (argc > 3) {
n = atoi(argv[1]);
width = atoi(argv[2]);
height = atoi(argv[3]);
}
bool data[1000][1000];
memset(data, false, 1000 * 1000);
setcaption("兰顿蚂蚁");
initgraph(width, height, INIT_RENDERMANUAL);
setbkcolor(EGERGB(0xFF, 0xFF, 0xFF));
//ege_enable_aa(true);
setfont(48, 0, "宋体");
setcolor(EGEARGB(0x80, 0x30, 0x30, 0x30));
outtextxy(150, 200, "按任意键开始");
getch();
cleardevice();
for (int i = 0; i < height / n; i++) { //行
for (int j = 0; j < width / n; j++) { //列
drawR(j, i, 0, n);
}
}
int x = width / 2 / n, y = height / 2 / n;
int r = 0;
while (!(x > width / n || y > height / n || x < 0 || y < 0)) {
data[x][y] = !data[x][y];
drawR(x, y, data[x][y], n);
walk(r, data[x][y], x, y);
Sleep(1);
}
while (is_run()) {
//TODO
}
closegraph();
return 0;
}