我试着用C语言制作一个在终端上运行的ascii raycaster。我想以前没做过这样的事。它似乎无法正确打印帧。希望有人能告诉我问题是什么。这是我的代码:https://ghostbin.com/paste/9vxmz
我原以为程序会第一人称显示一个迷宫(存储在地图数组中)。我还没有实施控制。基本上,有一个二维字符数组,它是终端屏幕的大小。这个数组应该每秒打印一次。屏幕数组从raycaster获取其值。我不知道问题到底出在哪里,但这个程序没那么大。拜托,我对C完全不熟悉。
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <math.h>
#include <unistd.h>
int getrows();
int getcols();
double diagnalMovementX(double x, double angle);
double diagnalMovementY(double y, double angle);
#define ROWS getrows() // the amount of horisontal rows in the terminal
#define COLS getcols() // the amount of verticle columns in the terminal
#define PI 3.14159265358979 // defines pi
#define TAU 2*PI // makes things easier because TAU is 360 degrees in radians
#define DTOR PI/180 // multiply by this to convert degrees to radians
#define RTOD 180/PI // multiply by this to convert radians to degrees
int currentframe=0; // the current frame
const int map[13][12]={ // the actual map
{1,1,0,1,1,1,1,1,1,1,1,1},
{1,0,0,0,1,0,0,1,0,0,0,1},
{1,1,1,0,1,1,0,1,0,1,0,1},
{1,0,1,0,0,0,0,1,0,1,0,1},
{1,0,1,1,1,1,0,1,1,1,0,1},
{1,0,0,0,0,0,0,1,0,0,0,1},
{1,0,1,0,1,1,1,1,0,1,1,1},
{1,0,1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,1,1,1,0,1},
{1,0,1,0,0,0,0,0,0,0,0,1},
{1,0,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,1,0,0,0,0,0,0,1},
{1,1,1,1,1,0,1,1,1,1,1,1}
};
int main() {
char screen[COLS][ROWS]; // this array is modified and printed every frame
double posX=2, posY=0; // position for the player
double direction=0*DTOR; // direction of the player
double fov=0*DTOR; // the feild of vision
double angleBetweenEachRay=fov/COLS; // the angle between each ray
double rayX, rayY; // the position of the end of the ray
double rayAngle; // the angle that the ray travels
double distanceToWall; // the distance the player is away from a wall
int lineHeight; // how many columns the wall should use on the screen
int sizeOfBlankSpaces; // the size that the ceiling and floor should appear on the screen
int thisIntPreventsCrashes=0; // prevents a ray from traveling endlessly
// gameloop starts here
while (1) {
// this allows movement
/* NOT IMPLEMENTED YET */
// this loop generates the columns of the frame
for (int i=0; i<COLS; i++) {
// calculate the ray angle
rayAngle=direction-fov/2-angleBetweenEachRay+angleBetweenEachRay*(i+1);
// fix the ray angle if it is not a proper value
if (rayAngle>TAU) {
rayAngle-=TAU;
}
else if (rayAngle<0) {
rayAngle+=TAU;
}
// makes the ray start from the player
rayX=posX;
rayY=posY;
// increases the ray size
rayX=diagnalMovementX(rayX, rayAngle);
rayY=diagnalMovementY(rayY, rayAngle);
// this code prevents the ray from traveling endlessly and the game from freezing if there is no wall for the ray to hit
thisIntPreventsCrashes++;
if (thisIntPreventsCrashes>10) {
lineHeight=0;
}
// checks if the ray hit a wall
if (map[(int) rayY][(int) rayX]==1) {
// calculates the distance to the wall using the distance formula
distanceToWall=sqrt(pow((rayX-posX), 2)+pow((rayY-posY), 2));
// calculates the size the wall should appear onscreen
lineHeight=ROWS/(10*distanceToWall);
}
// shows X when there is no wall
if (lineHeight==0) {
for (int ii=0; ii<ROWS; ii++) {
screen[i][ii]='X';
}
}
else {
// calculates the ceiling and floor size
sizeOfBlankSpaces=(ROWS-lineHeight)/2;
// ceiling
for(int ii=0; ii<sizeOfBlankSpaces; ii++) {
screen[i][ii]=' ';
}
// walls
for(int ii=sizeOfBlankSpaces; ii<sizeOfBlankSpaces+lineHeight; ii++) {
screen[i][ii]='';
}
// floor
for(int ii=sizeOfBlankSpaces+lineHeight; ii<ROWS; ii++) {
screen[i][ii]=' ';
}
}
}
// this sets the framerate
sleep(1);
// this clears the current frame
fflush(stdout);
//this code prints a new frame
for (int iy=0; iy<ROWS; iy++) {
for (int ix=0; ix<COLS; ix++) {
printf("%c", screen[iy][ix]);
}
printf("\n");
}
currentframe++;
}
return 0;
}
int getrows() {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
return w.ws_row;
}
int getcols() {
struct winsize w;
ioctl(0, TIOCGWINSZ, &w);
return w.ws_col;
}
double diagnalMovementX(double x, double angle) {
if (angle<TAU && angle>0.5*TAU) {
x-=0.5*cos(angle);
}
else if (angle>0 && angle<0.5*TAU) {
x+=0.5*cos(angle);
}
return x;
}
double diagnalMovementY(double y, double angle) {
if (angle<TAU && angle>0.5*TAU) {
if (angle<0.75*TAU) {
y-=0.5*sin(angle);
}
else if (angle>0.75*TAU) {
y+=0.5*sin(angle);
}
}
else if (angle>0 && angle<0.5*TAU) {
if (angle<0.25*TAU) {
y+=0.5*sin(angle);
}
else if (angle>0.25*TAU) {
y-=0.5*sin(angle);
}
}
else if (angle==0 || angle==TAU) {
y+=0.5;
}
else if (angle==0.5*TAU) {
y-=0.5;
}
return y;
}
最佳答案
fflush(stdout);
在帧打印嵌套后