简体   繁体   English

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

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

Ok so sorry if this is a noob question, but this is the first time making a game and I just want to know how to call in the gameloop, I have it created in my Keylistener class, but whenever I run it it just freezes due to an infiniate while loop in the run() method, but I would also like to have it called when the game is started so I can see the ticks/ fps throughout the entire game, here's my code if you guys need any other classes please say so thanks :) to be easier I'm just going to show the keylistener class so it's not a lot of code for you to sort through, and just so you know the way I made my grid was using an array of JLabels called WizardCells just so you know and I used imageicons for the icons I did not use BufferedImage, anyway I think that's all you need to know here it is thanks :) btw timePassed is Delta I just don't like the name delta haha 好的,很抱歉,如果这是一个菜鸟问题,但这是第一次制作游戏,我只想知道如何在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);



            }


}

} }

You should use a model-view-controller approach. 您应该使用模型视图控制器方法。 Have your key strokes result in an update of the model and, independent of that, have a timer loop that updates the display according to whatever state the model is in. That update usually takes the form of a repaint request which is handled in the UI thread. 让您的按键动作导致模型更新,并且与此无关,具有计时器循环,可以根据模型所处的状态来更新显示。该更新通常采用在UI中处理的重画请求的形式线。 The repaint method (varies across UI packages) will invoke other methods to adjust objects in the UI and/or draw on a canvas for finer-grained UI. repaint方法(在UI包中有所不同)将调用其他方法来调整UI中的对象和/或在画布上绘制以获得更细粒度的UI。

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

相关问题 GameLoop,要为Delta投入什么? - GameLoop, What To Put For Delta? 什么是使清单在整个Android Java项目中被读取和修改的正确方法? - What is the proper way to make my List be read and modified throughout my entire Android Java project? 如何在整个程序中仅创建一个 Scanner 对象 - How to create only one Scanner object throughout my program 这个小程序游戏循环有什么问题? - What is wrong with this applet gameloop? 如何暂停和恢复GameLoop? - How to pause and resume GameLoop? 如何调用tick()方法 - How to call the tick() method 如何在整个程序中启用我的Socket并通过它传递值 - How can I make my Socket on throughout the program and pass value through it 如何在整个项目中传递值并在程序中的任何地方使用它 - How do I pass a value throughout my project and use it anywhere in the program 是否可以为我在整个程序中使用的所有变量创建集中保存点? 如果是这样,怎么样? - Is it possible to create a centralized saving point of all the variables I use all throughout my program? If so, how? 如何计算一整年中每天白天和晚上的班次,工厂的哪些员工处于“待命”状态? - How to calculate what employees at a factory are “on call” each day and night shift throughout the year?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM