Sign in to follow this  
segt

[java] Keyboard Input and Game Loops

Recommended Posts

Recently I created a small 2D game in Java that is a clone of the game Puyo, a game similar to Tetris. I have a strong background in creating games with C++ and so I knew largely what I wanted to do code-wise. Starting out, I had googled for "java game loop" to get an idea for how the basic structure works. Every site I read seemed to do the same thing. A new thread was spawned and the game loop would take place there. Rather than spend time thinking about the implications of this I just mimicked it and got the game done. Now that I have time to reflect on what I have coded, this doesn't seem to make sense. A few more google searches and I found some programmers claiming that a reason for the new thread is to gain the ability to sleep it. Input they say, is captured by a seperate thread which Java has automatically spawned upon execution of the program. That, in order for it to be processed the thread which I manage must be slept (or at least a sleep(0) must be made) or input won't ever get through. However, from what I can tell this is not true. Regardless of whether or not I sleep my thread I still receive and process input. Note I am implementing KeyListener. Is this a result of Java halting my thread so that the code to handle key events can be run? I would appreciate some gritty details as to how input actually works in Java, and how it should properly get processed in a game loop. Since many people seem to program their game loops very differently depending on whether they are making a 2D or 3D game, please provide me with answers that apply to working with a 3D game.

Share this post


Link to post
Share on other sites
You do not need to start a new thread to use sleep. When your program starts, you are already in a thread, which can be put to sleep. ie

public class Game {
private boolean gameRunning = true;
public void go() {
while(gameRunning) {
//do all game stuff
Thread.sleep(0);
}
}
public static void main(String args[]) {
Game g = new Game();
g.go();
}
}


That all runs in the main program thread. No extra threads are created by me. The only other threads are created by the VM. On is the event dispatch thread that controls events and painting. The problem with threads is they use the scheduling system of the underlying OS/Architecture. So you can have thread starvation on systems that don't do pre-emptive multitasking. On a Windows box this is not a problem. So yes you should use Thread.sleep(0), because it will work on all systems and it will not affect the performance on systems that don't need it.

Using the source above, I can add key handling.

public class Game extends JFrame implements KeyListener {
private boolean gameRunning = true;
private boolean keys[65536];
public void go() {
while(gameRunning) {
//do all game stuff
if(keys[KeyEvent.VK_SPACE]) {
//fire or whatever
}
Thread.sleep(0);
}
}
public void keyTyped(KeyEvent e){}
public void keyPressed(KeyEvent e){
keys[e.getgetKeyCode()] = true;
}
public void keyReleased(KeyEvent e){
keys[e.getgetKeyCode()] = false;
}
public static void main(String args[]) {
Game g = new Game();
g.setSize(400, 300);
g.show();
g.go();
}
}


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this