简体   繁体   中英

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

@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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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