2013-10-19 64 views
0

對於我來說,我應該擴展bug角色來製作一個在網格上產生M的bug。這是我迄今爲止的,但錯誤不會按照指定的方向。相反,它使方形。任何幫助我做錯了什麼?GridWorld Mbug演員

import info.gridworld.actor.Bug; 
import info.gridworld.grid.Location; 

public class MBug extends Bug{ 

private int lineLength; 
private int steps; 
private int line; 

public MBug(int length) 
{ 
    setDirection(Location.NORTH); 
    steps = 0; 
    line = 1; 
    lineLength = length; 
} 
public void act(){ 
    if (line <= 4 && steps < lineLength){ 
     if (canMove()){ 
      move(); 
      steps++; 
     } 
    }else if (line == 2){ 
     setDirection(Location.SOUTHEAST); 
     steps = 0; 
     line++; 
    }else if (line == 3){ 
     setDirection(Location.NORTHEAST); 
     steps = 0; 
     line++; 
    }else if (line == 4){ 
     setDirection(Location.SOUTH); 
     steps = 0; 
     line++; 
    } 
} 

}

回答

0

ただ!

MBug

這是代碼。如果你什麼都不懂,請告訴我。

import info.gridworld.actor.Bug; 
import info.gridworld.grid.Location; 

public class MBug extends Bug{ 

    private int lineLength; 
    private int steps; 
    /* strokeNum is basically a code to tell the bug which stroke it is on. 
    * In this case, an 'M' has four strokes: 
    * up, diagonal down, diagonal up, and then down. 
    * This method can be used to create any letter, 
    * but round strokes (C's, R's, etc.) take so many individual strokes that it's almost impossible. 
    */ 
    private int strokeNum; 

    public MBug(int length){ 
     lineLength=length; 
     steps = 0; 
     strokeNum=0; 
    } 
    public void act(){ 
     if(strokeNum==0){ 
      setDirection(Location.NORTH); 
     }else if(strokeNum==1){ 
      setDirection(Location.SOUTHEAST); 
      //This is to shorten the length of this stroke. 
      steps++; 
     }else if(strokeNum==2){ 
      setDirection(Location.NORTHEAST); 
      //This is to shorten the length of this stroke. 
      steps++; 
     }else if(strokeNum==3){ 
      setDirection(Location.SOUTH); 
     } 
     if(canMove() && strokeNum<4){ 
      move(); 
      steps++; 
      if(steps>=lineLength){ 
       steps=0; 
       strokeNum++; 
      } 
     } 
    } 
}