对于类我应该扩展bug actor来制作一个在网格上制作M的bug。这是我到目前为止,但错误没有转向指定的方向。相反,它形成一个方形。对我做错了什么的帮助?
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 :(得分:0)
Ta da!
这是代码。如果你什么都不懂,请告诉我。
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++;
}
}
}
}