2015-02-12 43 views
2

我正在製作蛇遊戲的過程中,但我對如何在處理中實現蛇的移動感到困惑。我爲蛇創建了一個類,其中包含一個可以檢測按鍵的移動功能,但是我不知道如何實際編碼蛇的移動。任何人都可以根據下面的代碼給我一個關於如何實現Snake運動的簡單解釋嗎?在處理中移動蛇邏輯

int score = 0; 
int unit = 20; 
PFont courierNew24, courierNew40; 
ArrayList unitList; 
String direction = "right"; 
String nextDirection = ""; 
int directionCount = 0; 

class Snake 
{ 
    Snake() { 
    unitList = new ArrayList(); 
    unitList.add(new Unit(4, 3)); 
    unitList.add(new Unit(4, 4)); 
    unitList.add(new Unit(4, 5)); 
    unitList.add(new Unit(4, 6)); 
    unitList.add(new Unit(4, 7)); 
    unitList.add(new Unit(4, 8)); 
    } 

    void drawSnake() 
    { 
    for (int i = 0; i < unitList.size(); i++) 
    { 
     Unit snakePiece = (Unit) unitList.get(i); 
     snakePiece.drawSnakePiece(); 
    } 
    } 

    void moveSnake() 
    { 
    if (direction != "left" && nextDirection == "right") 
    { 
     //Move Snake 
    } 
    if (direction != "right" && nextDirection == "left") 
    { 
    } 
    if (direction != "up" && nextDirection == "down") 
    { 
    } 
    if (direction != "down" && nextDirection == "up") 
    { 
    } 
    } 
} 

class Unit 
{ 
    int row, column; 

    Unit (int unitRow, int unitColumn) 
    { 
    row = unitRow; 
    column = unitColumn; 
    } 

    void drawSnakePiece() 
    { 
    fill(0, 255, 0); 
    rect(column*unit, row*unit, unit, unit); 
    } 

    void drawApple() 
    { 
    fill(255, 0, 0); 
    ellipse(column*unit+(unit/2), row*unit+(unit/2), unit, unit); 
    } 

    void collision(int unitRow, int unitColumn) 
    { 
    if (row == unitRow && column == unitColumn) 
    { 
    } 
    } 
} 

//Functions 
void scoreBoard() 
{ 
    fill(255); 
    textFont(courierNew24, 24); 
    text("Score: " + score, 20, 670); 
} 

void gameOver() 
{ 
    fill(255); 
    textFont(courierNew40, 40); 
    textAlign(CENTER, CENTER); 
    text("Game Over, Score of " + score, 500, 350); 
} 

void setup() 
{ 
    size(1000, 700); 
    background(0); 
    courierNew24 = loadFont("CourierNewPSMT-24.vlw"); 
    courierNew40 = loadFont("CourierNewPSMT-40.vlw"); 
    scoreBoard(); 
} 

void draw() 
{ 
    smooth(); 
    Snake snake = new Snake(); 
    snake.drawSnake(); 
    snake.moveSnake(); 

    Unit apple = new Unit(10, 10); 
    apple.drawApple(); 
} 

void keyPressed() 
{ 
    switch(key) 
    { 
    case 'a': 
    case 'A': 
    directionCount += 1; 
    if (directionCount > 1) 
    { 
     direction = nextDirection; 
    } 
    nextDirection = "left"; 
    break; 
    case 'd': 
    case 'D': 
    directionCount += 1; 
    if (directionCount > 1) 
    { 
     direction = nextDirection; 
    } 
    nextDirection = "right"; 
    break; 
    case 'w': 
    case 'W': 
    directionCount += 1; 
    if (directionCount > 1) 
    { 
     direction = nextDirection; 
    } 
    nextDirection = "up"; 
    break; 
    case 's': 
    case 'S': 
    directionCount += 1; 
    if (directionCount > 1) 
    { 
     direction = nextDirection; 
    } 
    nextDirection = "down"; 
    break; 
    } 
} 

回答

2

的簡要說明:

  1. 你持有的所有蛇單位名單 - 這是已經完成。頭部和尾部是列表的第一個和最後一個元素。所以它實際上是一個隊列。
  2. 在每個勾號上,確定您應該移動的方向。例如,如果方向是左側,則下一個頭部座標將相對於當前頭部處於(-1,0)。
  3. 將新單元插入到頭部位置的列表中,並在步驟2中確定座標。
  4. 從列表(和屏幕)中刪除尾部單元。

這將安排運動。如果您發現蘋果處於頭頂位置,請初始化增長計數器。在每個勾號上,如果growth_counter> 0,則減少它並跳過尾部單位刪除。因此,只有頭部會移動直到它長大。