0

我正在尝试根据我的二维数组绘制一个迷宫。它只打印在窗口顶部,没有其他地方。

#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;
}
4

1 回答 1

0

您永远不会重置x1andx2值。一旦到达行尾并增加y1y2对于下一行,您的 x1 和 x2 继续超过 800。

第一个 for 循环应该包括

int x1 = -40;
int x2 = 0;
于 2013-04-08T16:34:01.233 回答