繁体   English   中英

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

[英]How to call my gameloop throughout entire program and what to put in tick()

好的,很抱歉,如果这是一个菜鸟问题,但这是第一次制作游戏,我只想知道如何在gameloop中调用,我已经在Keylistener类中创建了它,但是每当我运行它时,它都将冻结到run()方法中的无限while循环,但是我也想在游戏开始时调用它,这样我就可以看到整个游戏的滴答/ fps,这是我的代码,如果你们需要其他任何类,请这么说,谢谢:)为了更容易,我将显示keylistener类,因此不需要太多代码来进行排序,因此,您知道我制作网格的方式是使用名为WizardCells的JLabel数组就是这样,这样您就知道了,我为不使用BufferedImage的图标使用了imageicons图标,无论如何,我想这就是您需要知道的全部了:) 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);



            }


}

}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM