-3
我們的任務是製作一個簡單的'pac-man'遊戲,只需吃一個矩形內的垃圾。垃圾是「*」。 我迄今爲止代碼:Java幫助 - 製作'清掃'遊戲
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--;
}
}
}
}
}
我的問題: 它複製「@」並停止甚至活動。我試圖讓它移動aorund,直到所有內部8x8星符號全部消失。
你有沒有嘗試單步執行IDE調試器中的代碼?那是開始的地方。 –
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ –