SO I created a decent game loop and have no idea how to call it properly or what am doing wrong if you can take a look it's be really nice thanks
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
@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);
}
}
}