2008-12-17 61 views
0

我正在构建一个简单的冰滑动拼图来测试快板的使用。到目前为止,我已经完成了所有工作,尽管实际上我还没有在目标中添加目标,也没有添加任何难题,只是它背后的引擎。我在使用“r”将玩家跳回起始点时遇到了一些麻烦,谁能告诉我为什么?C++ allegro输入

#include <allegro.h> 
#include "maps.cpp" 

BITMAP* buffer; 

int x = 15; 
int y = 11; 

int tempX = 15; 
int tempY = 11; 

int startX = 15; 
int startY = 11; 

//This will be our background, 1 = clouds, 2 = brick, 3 = floor 
int map[24][32] = //{{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,3,2,2,2,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,3,3,3,3,2,3,3,3,3,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2}, 
        //{2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2}}; 

//This will contain all the objects, 100 = player 
int objMap[24][32] = //{{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,100,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, 
        //{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; 

void setupGame(){ 

    buffer = create_bitmap(640, 480); 

    for (int i = 0; i <= 24; i++){ 

     for(int t = 0; t <= 32; t++){ 

      if(map[i][t] == 1) rectfill(buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol(128, 255, 255)); 
      else if(map[i][t] == 2) rectfill(buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol(255, 128, 0)); 
      else if(map[i][t] == 3) rectfill(buffer, t * 20, i * 20, (t + 1) * 20, (i + 1) * 20, makecol(0, 0, 255)); 

      }  

    } 

    for (int i = 0; i <= 24; i++){ 

     for(int t = 0; t <= 32; t++){ 

      if(objMap[i][t] == 100) circlefill(buffer, (t * 20) + 10, (i * 20) + 10, 10, makecol(255, 255, 0)); 

      }  

    } 

    draw_sprite(screen, buffer, 0, 0); 

} 

void movePlayer(){ 

    tempX = x; 
    tempY = y; 

    if(key[KEY_UP] && map[y - 1][x] == 3) 
     for (; map[y - 1][x] == 3; --y){    
     } 

    else if(key[KEY_DOWN] && map[y + 1][x] == 3) 
     for (; map[y + 1][x] == 3; ++y){ 
     } 

    else if(key[KEY_RIGHT] && map[y][x + 1] == 3) 
     for (; map[y][x + 1] == 3; ++x){ 
     } 

    else if(key[KEY_LEFT] && map[y][x - 1] == 3) 
     for (; map[y][x - 1] == 3; --x){ 
     } 
    else if(key[KEY_R] && map[startY][startX] == 3) 
     for (; map[startY][startX] == 3 ; y = startY, x = startX){ // this is the section 
     } 


    acquire_screen(); 

    rectfill(buffer, tempX * 20, tempY * 20, (tempX + 1) * 20, (tempY + 1) * 20, makecol(0, 0, 255)); 

    circlefill(buffer, (x * 20) + 10, (y * 20) + 10, 10, makecol(255, 255, 0)); 

    draw_sprite(screen, buffer, 0, 0); 
    release_screen(); 

    rest(20); 
} 

int main(){ 

    allegro_init(); 
    install_keyboard(); 
    set_color_depth(16); 
    set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0); 

    setupGame(); 

    while(!key[KEY_ESC]){ 

      movePlayer(); 

    } 

    return 0; 

} 

END_OF_MAIN(); 

这一切都运行,除了它似乎跳过的部分。

如果你能帮助,谢谢

回答

1

for语句检查其结束条件(中段),它曾经执行在最后一节的语句之前。由于在输入for语句之前结束条件为真,即由于map [startY] [startX]已经是3,所以最后一条语句只是被跳过。

说实话,我认为这里真正的问题是你使用for语句。即使在正常工作的地方,你也会以非常不自然和单一的方式使用它。更好的是(例如):

while (map[y][x - 1] == 3) --x; 

在不工作的“重启”的情况下,很自然的事情将是完全跳过了对语句:

else if(key[KEY_R] && map[startY][startX] == 3) 
{ 
    y = startY; 
    x = startX; 
} 
+0

好点,我只是在每个“if”之后有一个投掷“for”的反射。 – user33061 2008-12-18 11:17:11