Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/security/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java对象数组-按int在数组中放置对象_Java - Fatal编程技术网

Java对象数组-按int在数组中放置对象

Java对象数组-按int在数组中放置对象,java,Java,我现在正在用Java开发一个游戏,在屏幕顶部生成块,块会掉下来(你必须躲避它们)。目前我在屏幕顶部有一个数组中的游戏生成块,但我不知道如何使它们的y位置下降(int) 基本上,我只需要帮助创建一个公共空区,该空区检查一个对象数组,它找到的每个对象实例都会将其y位置降低12 从代码中选择的剪贴画: static Object[][] food = new Object[7][700]; public void draw() { try { Graphics g = buf

我现在正在用Java开发一个游戏,在屏幕顶部生成块,块会掉下来(你必须躲避它们)。目前我在屏幕顶部有一个数组中的游戏生成块,但我不知道如何使它们的y位置下降(int)

基本上,我只需要帮助创建一个公共空区,该空区检查一个对象数组,它找到的每个对象实例都会将其y位置降低12

从代码中选择的剪贴画:

static Object[][] food = new Object[7][700];

public void draw() {
    try {
        Graphics g = bufferStrategy.getDrawGraphics();
        g.clearRect(0, 0, this.getSize().width, this.getSize().height);

        // Draw to back buffer
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.getSize().width, this.getSize().height);
        g.setColor(Color.BLUE);
        g.fillRect(Player.getX(), Player.getY(), Player.WIDTH, Player.HEIGHT);
        g.setColor(Color.GRAY);

        for(int x = 0; x < food.length; x++) {
            for(int y = 0; y < food[x].length; y ++) {
                Object o = food[x][y];
                if(o instanceof Hamburger) {
                    new Hamburger(x * 100, y, g);           
                }
                else if(o instanceof Salad) {
                    new Salad(x * 100, y, g);
                }
            }
        }

        GUI.draw(g);

    } catch(Exception e) {
        e.printStackTrace();
    } finally {
        bufferGraphics.dispose();
    }
}

public void dropBlocks() {

}

public void addBlock() {
    if(new Random().nextInt(2) == 1) {
        food[new Random().nextInt(7)][0] = new Hamburger(0, 0);
    } else food[new Random().nextInt(7)][0] = new Salad(0, 0);
}

public  void drawBackbufferToScreen() {
    bufferStrategy.show();

    Toolkit.getDefaultToolkit().sync();
}

public void run() {
    int i = 0;
    while(running) {
        i++;
        draw();
        Player.update();
        drawBackbufferToScreen();

        if(i == 50) {
            addBlock();
            i = 0;
        }

        dropBlocks();   

        Thread.currentThread();
        try {
            Thread.sleep(10);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

我会这样做:

Object[][] board = new Object[7][700];  //Game board.  
//you can also switch the width and height
//Initialization

for (int x = 699; x >= 0; x--) {
    for (int y = 0; y < 7; y++) {
        if (x == 699) {
            board[y][x] = BLANK; //set spot to open if it is the bottom row
            continue;
        }
        board[y][x+1] = board[y][x]; //move row down
    }
}
//Generate new Objects if needed for the top row of the board.
Object[]board=新对象[7][700]//游戏板。
//您还可以切换宽度和高度
//初始化
对于(int x=699;x>=0;x--){
对于(int y=0;y<7;y++){
如果(x==699){
线路板[y][x]=空白;//如果是最下面一行,则将spot设置为打开
继续;
}
线路板[y][x+1]=线路板[y][x];//下移行
}
}
//如果需要,为板的顶行生成新对象。

显然,对象没有
getY()方法。你可以用铸造和
实例来做一些丑陋的事情,但是这种设计相当糟糕。尝试转向面向对象的模型

interface Drawable {
    int getX();
    int getY();
    void moveDown(int numSpaces);
}

class Hamburger implements Drawable {
    private int x;
    private int y;
    public Hamburger(final int xPos, final int yPos) {
       x = xPos;
       y = yPos;
    }

    @Override
    public void moveDown(final int numSpaces) {
        // eg negative number, off the screen
        if(isValid(numSpaces)) {
           setY(getY() - numSpaces);
        }
    }
}

class Positioner {
  public static void moveMyObjects(final Drawable[][] objs) {
      for(Drawable[] rows : objs) {
          for(Drawable col : rows) {
             // clearly Object doesn't have a getY() method, so you should create your own interface and implement it
             col.moveDown(12);
          }
       }
   }
}

(我不认为你会这样做
->不,我们会。我们总是需要代码来理解你在做什么。因此,请发布一个简短的可编译但完整的代码来解释你在做什么。我添加了与绘制/添加块有关的代码片段。这似乎会导致块在每个块上生成2个。你可以调整d“下移行”中按编号的rop行,所以它们一次向下移动超过一个像素。只是说,这是一种内存和计算效率都很低的生成电路板的方法。我如何才能得到它,使块不断下降,直到它离开屏幕?这种方法仍然只是使其下降约一秒钟,然后保持静止。上面的代码将对象向下移动了若干个像素每次运行时都有像素。你能检查它是否重复运行并且每次执行后都在重新绘制GUI吗?是否有任何原因说明如果我将1替换为任何高的数字,我只会得到错误?这很好。你可以使食物工具可绘制。我不会为你编写程序。另外,不要扩展具体的类。请参阅
interface Drawable {
    int getX();
    int getY();
    void moveDown(int numSpaces);
}

class Hamburger implements Drawable {
    private int x;
    private int y;
    public Hamburger(final int xPos, final int yPos) {
       x = xPos;
       y = yPos;
    }

    @Override
    public void moveDown(final int numSpaces) {
        // eg negative number, off the screen
        if(isValid(numSpaces)) {
           setY(getY() - numSpaces);
        }
    }
}

class Positioner {
  public static void moveMyObjects(final Drawable[][] objs) {
      for(Drawable[] rows : objs) {
          for(Drawable col : rows) {
             // clearly Object doesn't have a getY() method, so you should create your own interface and implement it
             col.moveDown(12);
          }
       }
   }
}