如何在整个程序中调用我的游戏循环以及在tick()中放入什么

时间:2014-03-28 06:48:18

标签: java eclipse loops while-loop game-loop

很抱歉,如果这是一个菜鸟问题,但这是第一次制作游戏而我只是想知道如何在游戏环中调用,我在Keylistener类中创建它,但每当我运行它时因为run()方法中的infiniate while循环而冻结,但我也想在游戏开始时调用它,所以我可以在整个游戏中看到ticks / fps,如果你们需要任何代码,这里是我的代码其他类请说谢谢:)更容易我只是要显示keylistener类所以它不是很多代码供你排序,只是让你知道我的网格使用数组的方式JLabels打电话给WizardCells只是为了让你知道并且我使用了图像用于我没有使用BufferedImage的图标,无论如何我认为这就是你需要知道的全部感谢:) btw timePassed是Delta我只是不喜欢名字delta haha​​

@SuppressWarnings("serial")
public class WizardKeyHandeler extends WizardBattleGrid implements KeyListener, Runnable{

    public static boolean isRunning = true;
    static Thread thread = new Thread();

    protected synchronized static void start(){

    if(isRunning)
        return;
    isRunning = true;
    thread = new Thread();
    thread.start();
}




protected synchronized static void stop(){
    if(!isRunning)
        return;

    isRunning = false;

        try{

            thread.join();
        }
        catch(InterruptedException e){

            e.printStackTrace();
        }
        System.exit(1);

}



public void run() {
    long lastTime = System.nanoTime();
    final double amountOfTicks = 60.0;
    double ns = 1000000000 / amountOfTicks;
    double timePassed = 0;
    int updates = 0;
    int frames = 0;
    long timer = System.currentTimeMillis();

    while(isRunning){

        long currentTime = System.nanoTime();
        timePassed += (currentTime - lastTime)/ ns;
        lastTime = currentTime;
        if(timePassed >= 1){
            tick();
            updates++;
            timePassed--;
        }

        frames++;

        if(System.currentTimeMillis() - timer > 1000){

            timer += 1000;
            System.out.println(updates + " ticks, fps " + frames);
            updates = 0;
            frames = 0;
        }
    }
    stop();
}



private void tick(){

    WizardCells[getBlueSpell().getx()][getBlueSpell().gety()].setIcon(null);
    WizardCells[getBlueSpell().changex(getGoodGuy().getx())][getBlueSpell().changey(getGoodGuy().gety() +1)].setIcon(getBlueSpell().getIcon());


}





@Override
public void keyPressed(KeyEvent e) {
    int key = e.getKeyCode();

    if(key == KeyEvent.VK_W){

        if(getGoodGuy().getx() != 0){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }

    }

    else if(key == KeyEvent.VK_S){

        if(getGoodGuy().getx() != 19){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_D){

        if(getGoodGuy().gety() != 9){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_A){

        if(getGoodGuy().gety() != 0){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_SPACE){


        while(getBlueSpell().gety() != 19){
            run();
        }

        WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);



            }

}












@Override
public void keyReleased(KeyEvent e) {
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_W){
            if(getGoodGuy().getx() != 0){

                WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
                WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }   
        }

    else if(key == KeyEvent.VK_S){

        if(getGoodGuy().getx() != 19){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_D){

        if(getGoodGuy().gety() != 9){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_A){

        if(getGoodGuy().gety() != 0){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_SPACE){


        while(getBlueSpell().gety() != 19){
            run();
        }

        WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);



            }

}



@Override
public void keyTyped(KeyEvent e) {
    int key = e.getKeyCode();
    if(key == KeyEvent.VK_W){
        if(getGoodGuy().getx() != 0){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().changex(getGoodGuy().getx()-1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());

        }

        }

    else if(key == KeyEvent.VK_S){

        if(getGoodGuy().getx() != 19){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().changex(getGoodGuy().getx()+1)][getGoodGuy().gety()].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_D){

        if(getGoodGuy().gety() != 9){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()+1)].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_A){

        if(getGoodGuy().gety() != 0){

            WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);
            WizardCells[getGoodGuy().getx()][getGoodGuy().changey(getGoodGuy().gety()-1)].setIcon(getGoodGuy().getIcon());
        }
    }

    else if(key == KeyEvent.VK_SPACE){


        while(getBlueSpell().gety() != 19){
            run();
        }

        WizardCells[getGoodGuy().getx()][getGoodGuy().gety()].setIcon(null);



            }


}

}

1 个答案:

答案 0 :(得分:0)

您应该使用模型 - 视图 - 控制器方法。让您的击键导致模型的更新,并且独立于此,具有根据模型所处的状态更新显示的计时器循环。该更新通常采用在UI中处理的重绘请求的形式。线。重绘方法(因UI包而异)将调用其他方法来调整UI中的对象和/或在画布上绘制以获得更细粒度的UI。