Java帮助-制作“清扫者”游戏
我们的任务是制作一个简单的“吃豆人”游戏,只吃矩形内的垃圾。垃圾被丢弃了。 到目前为止,我的代码是:Java帮助-制作“清扫者”游戏,java,Java,我们的任务是制作一个简单的“吃豆人”游戏,只吃矩形内的垃圾。垃圾被丢弃了。 到目前为止,我的代码是: public class Sweeper { public static void main(String[] args) throws InterruptedException { //************************************************************ // Variable se
public class Sweeper
{
public static void main(String[] args) throws InterruptedException
{
//************************************************************
// Variable set up
int sy = 10, sx= 10; // Box size
int x= 5, y= 5; // Start point
int maxmc = 8; // Max distance moving
char [] [] env = new char[sy][sx];
int right = sx - 2, top = sy - 2, left = sx - (right + 1) , bottom = sy - (top + 1);
//************************************************************
//************************************************************
// Filling the grid with Stars
for(int i=0; i<sy;i++)
{
for(int j =0; j < sx; j++)
{
env[i][j] = '*';
}
}
//************************************************************
int mc = 0, direction = 0, count = (right * top);
// The actual game
while(count != 0)
{
direction = (int)(Math.random() * 3);
// Display
//System.out.println("\n\n\n\n\n");
for(int i = 0; i < sy; i++)
{
for(int j= 0; j< sx; j++)
{
System.out.print(env[i][j]);
}
System.out.println();
}
System.out.println("\n\n\n");
Thread.sleep(700);
System.out.println(direction);
if((x <= right && x >= left && y <= top && y >= bottom))
{
if(env[x][y] == ' ')
{
// RIGHT
if(direction == 0)
{
env[y][x] = '@';
x--;
env[y][x+2] = ' ';
}
// left
else if(direction == 1)
{
env[y][x] = '@';
x++;
env[y][x-2] = ' ';
}
// TOP
else if(direction ==2)
{
env[y][x] = '@';
y--;
env[y+2][x] = ' ';
}
// bottom
else if(direction ==3)
{
env[y][x] = '@';
y++;
env[y-2][x] = ' ';
}
}
else
{
if(direction == 0)
{
env[y][x] = '@';
x--;
env[y][x+2] = ' ';
}
// left
else if(direction == 1)
{
env[y][x] = '@';
x++;
env[y][x-2] = ' ';
}
// TOP
else if(direction ==2)
{
env[y][x] = '@';
y--;
env[y+2][x] = ' ';
}
// bottom
else if(direction ==3)
{
env[y][x] = '@';
y++;
env[y-2][x] = ' ';
}
// Keeps track of the trash
count--;
}
}
}
}
}
我的问题是:
它复制“@”并有时停止移动。
我试着让它移动,直到所有的8x8星形符号都消失 只是为了清理代码:移动算法与4条if/else语句重复。你只需要在if中输入计数!env[x][y]==''语句 对于你的问题:你在你的边界条件下检查得很糟糕,你做到了
env[y][x] = '@';
x--;
env[y][x+2] = ' ';
但是如果x=1,那么在代码之后是0,因为
if((x <= right && x >= left && y <= top && y >= bottom))
如果x=0,right=0,则它永远不会进入语句,count不能更改,并且它会内环循环。您是否尝试过在IDE调试器中单步执行代码?这是开始的地方。