2015-04-27 43 views
0

僅供參考,這裏是鏈接到我的任務(當然是一個C語言課程):Snake不知道我的蛇遊戲的邏輯有什麼問題嗎?

口被認爲是「<」,「>」,「V」或「^」取決於方向,身體部分應該是「*」。所以首先,我的上下箭頭鍵控制被切換,但它打印正確的字符(即,當它向下移動時打印「v」,向上移動時打印「^」),並且我不明白爲什麼。

此外,隨着頭部的移動,它剛脫離身體並且不會打印新的身體片段(請參閱Screencap)。

下面是我的自定義結構,主,scene_render和scene_update功能(我們是不允許修改此分配的主要功能):

struct Point{ 
    int x; 
    int y; 
}; 

struct Snake { 
    struct Point segments[MAX_SEGMENTS]; 
    int num_segments; 
    int dir; //0=up, 1=down, 2=left, 3=right 
}; 

struct Scene { 
    // TODO: add fields 
    struct Snake snake; 
    struct Point fruit; 
}; 

主要

int main(void) { 
    // Important: do NOT modify the main function 
    struct Scene scene; 

    scene_init(&scene); 

    int keep_going = 1; 
    while (keep_going == 1) { 
     scene_render(&scene); 
     cons_update(); 
     scene_delay(&scene); 
     keep_going = scene_update(&scene); 
    } 

    return 0; 
} 

scene_render

void scene_render(const struct Scene *s) { 
    // TODO: add your code 
    int num=s->snake.num_segments; 
    cons_clear_screen(); 
    cons_move_cursor(s->snake.segments[0].y, s->snake.segments[0].x); 
    if(s->snake.dir==0) 
    { 
     cons_printw("v"); 
    } 
    else if(s->snake.dir==1) 
    { 
     cons_printw("^"); 
    } 
    else if(s->snake.dir==2) 
    { 
     cons_printw(">"); 
    } 
    else 
    { 
     cons_printw("<"); 
    } 
    for(int i=1; i <num; i++) 
    { 
     cons_move_cursor(s->snake.segments[i].y, s->snake.segments[i].x); 
     cons_printw("*"); 
    } 
    cons_move_cursor(23, 79); //just to get blinking cursor out of the way 
} 

scene_update

int scene_update(struct Scene *s) { 
    // This function should return 0 if the player presses 'q', 
    // 1 otherwise. 
    struct Scene temp=*s; 
    int num=temp.snake.num_segments; 

    int key = cons_get_keypress(); 
    // TODO: add your code 
    if(key=='q') 
    { 
     return 0; 
    } 
    if(key==UP_ARROW && temp.snake.dir!=1) 
    { 
     temp.snake.dir=0; 
     temp.snake.segments[0].y=(temp.snake.segments[0].y)+1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i].y; 
      temp.snake.segments[i].x=s->snake.segments[i].x; 
     } 
    } 
    else if(key==DOWN_ARROW && temp.snake.dir!=0) 
    { 
     temp.snake.dir=1; 
     temp.snake.segments[0].y=(temp.snake.segments[0].y)-1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i].y; 
      temp.snake.segments[i].x=s->snake.segments[i].x; 
     } 
    } 
    else if(key==LEFT_ARROW && temp.snake.dir!=3) 
    { 
     temp.snake.dir=2; 
     temp.snake.segments[0].x=(temp.snake.segments[0].x)-1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i].y; 
      temp.snake.segments[i].x=s->snake.segments[i].x; 
     } 
    } 
    else if(key==RIGHT_ARROW && temp.snake.dir!=2) 
    { 
     temp.snake.dir=3; 
     temp.snake.segments[0].x=(temp.snake.segments[0].x)+1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i].y; 
      temp.snake.segments[i].x=s->snake.segments[i].x; 
     } 
    } 
    else if(key==-1) 
    { 
     if(temp.snake.dir==0) 
     { 
      temp.snake.segments[0].y=(temp.snake.segments[0].y)+1; 
      for(int i=1; i<num; i++) 
      { 
       temp.snake.segments[i].y=s->snake.segments[i].y; 
       temp.snake.segments[i].x=s->snake.segments[i].x; 
      } 
     } 
     else if(temp.snake.dir==1) 
     { 
      temp.snake.segments[0].y=(temp.snake.segments[0].y)-1; 
      for(int i=1; i<num; i++) 
      { 
       temp.snake.segments[i].y=s->snake.segments[i].y; 
       temp.snake.segments[i].x=s->snake.segments[i].x; 
      } 
     } 
     else if(temp.snake.dir==2) 
     { 
      temp.snake.segments[0].x=(temp.snake.segments[0].x)-1; 
      for(int i=1; i<num; i++) 
      { 
       temp.snake.segments[i].y=s->snake.segments[i].y; 
       temp.snake.segments[i].x=s->snake.segments[i].x; 
      } 
     } 
     else 
     { 
      temp.snake.segments[0].x=(temp.snake.segments[0].x)+1; 
      for(int i=1; i<num; i++) 
      { 
       temp.snake.segments[i].y=s->snake.segments[i].y; 
       temp.snake.segments[i].x=s->snake.segments[i].x; 
      } 
     } 
    } 
    temp.fruit.x=s->fruit.x; 
    temp.fruit.y=s->fruit.y; 
    *s=temp; 

    return 1; 
} 

回答

2

爲什麼身體從頭部卸下的原因是因爲你的移動代碼:

if(temp.snake.dir==0) 
    { 
     temp.snake.segments[0].y=(temp.snake.segments[0].y)+1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i].y; 
      temp.snake.segments[i].x=s->snake.segments[i].x; 
     } 
    } 

應該是:

if(temp.snake.dir==0) 
    { 
     temp.snake.segments[0].y=(temp.snake.segments[0].y)+1; 
     for(int i=1; i<num; i++) 
     { 
      temp.snake.segments[i].y=s->snake.segments[i-1].y; 
      temp.snake.segments[i].x=s->snake.segments[i-1].x; 
     } 
    } 

換句話說,第一主體部分應移到頭曾經是。第二個身體部分應該移動到第一部分曾經存在的位置等等。現在你擁有它的方式,所有身體部位始終保持在同一個位置。您需要在許多地方進行此更改,或者更好地創建一個移動主體的功能,以便您不必在任何地方都有重複的代碼。至於爲什麼up是down的,反之亦然,我只能想象屏幕的y座標與你認爲的相反(增加的y值向上或向下)。您應該檢查哪個方式是哪個方式,並確保您的代碼匹配。

+0

這解決了我的問題,並且我能夠修復上下控件,謝謝! –

+0

什麼是附加段的最簡單方法?我有if語句,說如果蛇頭的座標等於水果的座標,那麼做(某些事情) –

+0

@ mjones.udri我會在與當前尾部相同的位置添加一個段(在另一個頂部有兩個段)。蛇移動一次後,新的尾巴將保持原位,然後蛇再次移動後,尾巴將開始追蹤它前面的棋子。 – JS1