社区讨论

真实的物理引擎

灌水区参与者 10已保存回复 15

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
15 条
当前快照
1 份
快照标识符
@lod8qz7n
此快照首次捕获于
2023/10/31 02:36
2 年前
此快照最后确认于
2023/11/05 13:02
2 年前
查看原帖
CPP
#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或点叉叉)

回复

15 条回复,欢迎继续交流。

正在加载回复...