Java Object Array - 按int删除数组中的对象

时间:2013-01-26 06:13:10

标签: java

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

基本上我只需要帮助创建一个公共void来检查一个对象数组,并且它找到一个对象的每个实例,它会将它的y位置降低12。

从代码中选择的snippits:

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();
        }
    }
}

Food.java(什么是汉堡和沙拉延伸)

public class Food {
public static int x;
public static int y;
public static int HEIGHT;
public static int WIDTH;
public int type;

private static Image blockImage;

// Default constructor
public Food() {
    Food.x = 0;
    Food.y = 0;
    Food.HEIGHT = 80;
    Food.WIDTH = 80;
}

public Food(int x, int y) {
    Food.x = x;
    Food.y = y;
    Food.HEIGHT = 80;
    Food.WIDTH = 80;
}

// Getters and setters

2 个答案:

答案 0 :(得分:1)

我会做这样的事情:

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.

答案 1 :(得分:0)

显然,Object没有getY()方法。你可以用铸造和instanceof做丑陋的事情,但这种设计相当腐烂。尝试转向面向对象的模型。

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);
          }
       }
   }
}