Ich versuche, eine einfache ASCII 3D Spinning Cube -Animation in der Windows -Konsole mit C ++ zu erstellen.#include
#include
#include
#include
#include
using namespace std;
const int WIDTH = 80;
const int HEIGHT = 40;
const float cubeWidth = 20;
float A = 0, B = 0, C = 0;
float distanceFromCamera = 100;
float K1 = 40;
char buffer[WIDTH * HEIGHT];
float zBuffer[WIDTH * HEIGHT];
float cosA, sinA, cosB, sinB, cosC, sinC; // biến lượng giác dùng chung
void gotoxy(HANDLE hConsole, int x, int y) {
COORD pos{(SHORT)x, (SHORT)y};
SetConsoleCursorPosition(hConsole, pos);
}
void rotate(float i, float j, float k, float &x, float &y, float &z) {
// rotation quanh 3 trục
float x1 = i;
float y1 = j * cosA - k * sinA;
float z1 = j * sinA + k * cosA;
float x2 = x1 * cosB + z1 * sinB;
float y2 = y1;
float z2 = -x1 * sinB + z1 * cosB;
x = x2 * cosC - y2 * sinC;
y = x2 * sinC + y2 * cosC;
z = z2;
}
void calculatePoint(float i, float j, float k, char ch) {
float x, y, z;
rotate(i, j, k, x, y, z);
z += distanceFromCamera;
float ooz = 1 / z;
int xp = int(WIDTH / 2 + K1 * ooz * x * 2);
int yp = int(HEIGHT / 2 - K1 * ooz * y);
int idx = xp + yp * WIDTH;
if (idx >= 0 && idx < WIDTH * HEIGHT) {
if (ooz > zBuffer[idx]) {
zBuffer[idx] = ooz;
buffer[idx] = ch;
}
}
}
int main() {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
// Ẩn con trỏ nhấp nháy
CONSOLE_CURSOR_INFO cursorInfo;
GetConsoleCursorInfo(hConsole, &cursorInfo);
cursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(hConsole, &cursorInfo);
while (true) {
fill(buffer, buffer + WIDTH * HEIGHT, ' ');
fill(zBuffer, zBuffer + WIDTH * HEIGHT, 0);
// tính lượng giác 1 lần mỗi frame
cosA = cosf(A); sinA = sinf(A);
cosB = cosf(B); sinB = sinf(B);
cosC = cosf(C); sinC = sinf(C);
for (float i = -cubeWidth; i < cubeWidth; i += 0.5f) {
for (float j = -cubeWidth; j < cubeWidth; j += 0.5f) {
calculatePoint(i, j, -cubeWidth, '@');
calculatePoint(cubeWidth, j, i, '$');
calculatePoint(-cubeWidth, j, -i, '~');
calculatePoint(-i, j, cubeWidth, '#');
calculatePoint(i, -cubeWidth, -j, ';');
calculatePoint(i, cubeWidth, j, '+');
}
}
gotoxy(hConsole, 0, 0);
for (int k = 0; k < WIDTH * HEIGHT; k++) {
cout
Obwohl ich "\ x1b [H" verwende, um den Cursor wieder nach oben zu bewegen, wird die Konsole immer noch nach unten geprüft-jeder neue Rahmen wird unten unter dem vorherigen gedruckt. (IM Verwenden Sie VSCODE unter Windows 11). < /P>
Vielen Dank! (CMD, PowerShell, Windows -Terminal) < /p>
Erwartete Verhalten:
Ich möchte>
Warum scrollen meine ASCII -Animation weiter nach unten, anstatt auf Windows Console (C ++) zu aktualisieren? ⇐ C++
-
- Similar Topics
- Replies
- Views
- Last post