#include <windows.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>
struct Square {
int x, y;
int turn;
void move_() {
switch (turn) {
case 0:
if (x == 0) {
turn = 4;
move_();
}
else x--;
break;
case 1:
if (x == 0 && y == 24) {
turn = 5;
move_();
}
else if (x == 0) {
turn = 3;
move_();
}
else if (y == 24) {
turn = 7;
move_();
}
else x--, y++;
break;
case 2:
if (y == 24) {
turn = 6;
move_();
}
else y++;
break;
case 3:
if (x == 29 && y == 24) {
turn = 7;
move_();
}
else if (x == 29) {
turn = 1;
move_();
}
else if (y == 24) {
turn = 5;
move_();
}
else x++, y++;
break;
case 4:
if (x == 29) {
turn = 0;
move_();
}
else x++;
break;
case 5:
if (x == 29 && y == 0) {
turn = 1;
move_();
}
else if (x == 29) {
turn = 7;
move_();
}
else if (y == 0) {
turn = 3;
move_();
}
else x++, y--;
break;
case 6:
if (y == 0) {
turn = 2;
move_();
}
else y--;
break;
case 7:
if (x == 0 && y == 0) {
turn = 3;
move_();
}
else if (x == 0) {
turn = 5;
move_();
}
else if (y == 0) {
turn = 1;
move_();
}
else x--, y--;
break;
}
}
} squares[100];
int map[30][25];
HANDLE hOut;
void modeset(int w, int h) {
COORD size = {w, h};
SetConsoleScreenBufferSize(hOut, size);
SMALL_RECT rc = {1, 1, w, h};
SetConsoleWindowInfo(hOut, true, &rc);
system("cls");
return;
}
void goto_(int x, int y) {
COORD coord = {x, y};
SetConsoleCursorPosition(hOut, coord);
}
int main(){
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
modeset(62, 25);
int n;
do {
printf("input a num(the balls you want): ");
scanf("%d", &n);
if (n <= 100) break;
printf("your num is too big...\n");
} while (n > 100);
srand(time(NULL));
for (int i = 0; i < n; i++) {
do {
squares[i].x = rand() % 30;
squares[i].y = rand() % 25;
squares[i].turn = rand() % 8;
} while (map[squares[i].x][squares[i].y]);
map[squares[i].x][squares[i].y] = 1;
}
while (1) {
system("cls");
for (int i = 0; i < n; i++) {
goto_(squares[i].x * 2, squares[i].y);
printf("●");
squares[i].move_();
}
Sleep(25);
}
return 0;
}
亲身体验了,特别爽
(如果想退出请按Ctrl+C或点叉叉)