public
anonymous / GameKernel.java
Created

Screen Tearing - GameKernel.java

  • Download Gist
GameKernel.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.Timer;
 
 
public class GameKernel implements Runnable {
 
public static int playerX = 400;
public static int playerY = 300;
private int moveVel = 10;
private int direction = 1;
 
private Timer timer;
public static final int TIMESTEP_MS = 10;
public void run() {
final GameWindow gw = new GameWindow();
 
ActionListener taskPerformer = new ActionListener() {
 
@Override
public void actionPerformed(ActionEvent e) {
update();
gw.render();
}
};
timer = new Timer(TIMESTEP_MS, taskPerformer);
timer.start();
}
 
public void update() {
playerX += direction * moveVel;
if (playerX >= GameWindow.SIZE.width - 100) {
direction *= -1;
}
else if (playerX <= 100) {
direction *= -1;
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.