我正在尝试根据我的二维阵列绘制一个迷宫。它只打印在窗口的顶部而不是其他地方。
#include "SDL.h"
#include "SDL_gfxPrimitives.h"
#include <conio.h>
int main( int argc, char* args[] )
{
char caMaze[20][20] = { //the array for the maze
{"###################"},
{"#+##### # #"},
{"# ## ###########"},
{"## # ##### #"},
{"## ## ####### #####"},
{"## ## ####### #####"},
{"## ## # ##"},
{"## ## ## ####"},
{"########### ## ####"},
{"#### ## ####"},
{"#### ### ##### ####"},
{"# # ## #### ####"},
{"## ## ## ### #####"},
{"## ## ## #####"},
{"## ### #### #######"},
{"## # # ######"},
{"#### #### #########"},
{"######### =#"},
{"###################"},
};
int x1 = -40;
int x2 = 0;
int y1 = -40;
int y2 = 0;
SDL_Surface *screen = NULL;
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
int loop = 0;
for(int i = 0; i < 20; i++)
{
y1 += 40;
y2 += 40;
for(int j = 0; j< 20; j++)
{
x1 += 40;
x2 += 40;
if( caMaze[i][j] == '#')
{
boxRGBA(screen, x1, y1, x2, y2, 255, 0, 0, 255);
}
if( caMaze[i][j] == ' ')
{
boxRGBA(screen, x1, y1, x2, y2, 255, 0, 255, 255);
}
if(caMaze[i][j] == '+')
{
boxRGBA(screen, x1, y1, x2, y2, 0, 0, 255, 255);
}
if (caMaze[i][j] == '=')
{
boxRGBA(screen, x1, y1, x2, y2, 0, 255, 0, 255);
}
}
}
if(SDL_Flip(screen) == -1)
{
return 1;
}
getch();
SDL_Quit();
return 0;
}
答案 0 :(得分:0)
您永远不会重置x1
和x2
值。一旦到达行尾并为下一行增加y1
,y2
,您的x1和x2将继续超过800。
第一个for循环应包括
int x1 = -40;
int x2 = 0;