不确定为什么此游戏对象不会显示在屏幕上

时间:2018-08-20 19:08:00

标签: java

我正在创建BrickBreaker克隆,并且对编码还很陌生。我的问题是屏幕上没有渲染新的游戏对象(砖),我也无法弄清原因。

我制作了其他游戏对象,例如玩家和球类,但没有显示它们,但它们的制作方法与积木完全相同。

主游戏循环

public class Game extends Canvas implements Runnable {

public Game(){
    handler = new Handler();
    menu = new Menu(this, handler);
    this.addKeyListener(new KeyInput(handler));
    this.addMouseListener(menu);

    new Window(WIDTH, HEIGHT, "BrickBreaker", this);
    hud = new HUD();
    r = new Random();



    if(gameState == STATE.Level1){
        handler.addObject(new Player((WIDTH/2 - 32), (1250), ID.Player, handler));
        handler.addObject(new Ball(r.nextInt(WIDTH), r.nextInt(HEIGHT), ID.Ball, handler));
        handler.addObject(new Brick(x, y, ID.Brick, handler));

    }       
}

public synchronized void start(){
    thread = new Thread(this);
    thread.start();
    running = true;
}


public synchronized void stop(){
    try{
        thread.join();
        running = false;
    }catch(Exception e){
        e. printStackTrace();
    }   
}

public void run(){
    this.requestFocus();
    long lastTime = System.nanoTime();
    double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double delta = 0;
    long timer = System.currentTimeMillis();
    int frames = 0;
    while(running){
        long now = System.nanoTime();
        delta += (now - lastTime) / ns;
        lastTime = now;
        while(delta >= 1){
            tick();
            delta--;
        }
        if(running)
            render();
        frames++;

        if(System.currentTimeMillis() - timer > 1000){
            timer += 1000;
            System.out.println("FPS: " + frames);
            frames = 0;
        }
    }
    stop();
}

private void tick(){
    handler.tick();
    if(gameState == STATE.Level1){
        hud.tick();
    } else if(gameState == STATE.Menu){
        menu.tick();
    }
}

private void render(){
    BufferStrategy bs = this.getBufferStrategy();
    if(bs == null){
        this.createBufferStrategy(3);
        return;
    }

    Graphics g = bs.getDrawGraphics();


    g.setColor(Color.BLACK);
    g.fillRect(0, 0, WIDTH, HEIGHT);

    handler.render(g);

    if(gameState == STATE.Menu || gameState == STATE.Help){
        menu.render(g);
    } else{
        hud.render(g);
    }

    g.dispose();
    bs.show();
}

public static void main(String args[]){
    new Game();
}
}

处理程序类

public class Handler {

LinkedList<GameObject> object = new LinkedList<GameObject>();

public void tick(){
    for(int i =0; i<object.size();i++){
        GameObject tempObject = object.get(i);

        tempObject.tick();
    }
}

public void render(Graphics g){
    for(int i =0; i<object.size();i++){
        GameObject tempObject = object.get(i);

        tempObject.render(g);
    }
}

public void addObject(GameObject object){
    this.object.add(object);
}

public void removeObject(GameObject object){
    this.object.remove(object);
}
}

砖类

public class Brick extends GameObject{

Handler handler;

public Brick(int x, int y, ID id, Handler handler) {
    super(x, y, id);
    brickWidth = 500;
    brickHeight = 200;
    this.handler = handler;
}

@Override
public void tick() {
    collision();

}

private void collision(){
    for(int i=0;i<handler.object.size();i++){

        GameObject tempObject = handler.object.get(i);

        if(tempObject.getId() == ID.Player){
            if(getBounds().intersects(tempObject.getBounds())){
                velY *= -1;;
            }
        }
    }
}


public void render(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(x, y, brickWidth, brickHeight);

}

@Override
public Rectangle getBounds() {
    return new Rectangle(x, y, brickWidth, brickHeight);

}
}

0 个答案:

没有答案