2008-12-17 31 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