2013-04-08 130 views
0

我想根據我的2D數組來繪製一個迷宮。它只打印在窗口的頂部,沒有其他地方。SDL中的瓷磚迷宮

#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; 
} 
+5

截圖將真正幫助這裏。 – Thomas 2013-04-08 11:18:52

+0

那是什麼'getch()'?您正在使用SDL,請使用SDL輸入例程。 – genpfault 2013-04-08 18:58:03

+0

@genpfault OP使用'#include '和+ getch()來查看屏幕,我發現沒有問題。 – 2013-04-08 19:47:41

回答

0

您從不重置x1x2值。一旦你到達行的末尾,增加y1y2下一行,你x1和x2繼續走了800

第一個for循環應該包括

int x1 = -40; 
int x2 = 0;