Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2012 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/3389139 to your computer and use it in GitHub Desktop.
Save anonymous/3389139 to your computer and use it in GitHub Desktop.
Screen Tearing - GameKernel.java
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment