The more the I think about it, the more I can't see a way to write good keyboard control in Java on Linux. Let me explain:-
Say you are writing a basic shoot'em-up. AFAIK, there are basically two ways to control the players sprite when your interface is the KeyPressed and KeyReleased events: you can either have the actual movement code inside these events, such as:
public void keyPressed(KeyEvent e) {
if (e == Key_UP) {
ship.y_pos++;
}
}
The problem with this way is that the ship will move as fast as the keyboard repeat rate, regardless of the main game loop or anything.
The other way (and the way I'm doing it in my game) is to store which keys are currently pressed (in a boolean array), and then process them in the game loop, such as:-
public void keyPressed(KeyEvent e) {
keys_pressed[e.getKeyCode()] == true;
}
(and set it to false on the KeyRelease as well).
public void gameLoop() {
while (true) {
if (keys_pressed[KEY_UP] == true) {
ship.y_pos++;
}
// Rest of game code here
}
}
The flaw with this, as I mentioned, is that if the player holds down a key, then with so many keypress/release events being fired for the duration it is held down, the key might not be marked as being pressed at the time when the keys are being processed in the main game loop.
Can anyone tell me what I'm missing? Or should I just move to another language?
The trials and tribulations of an amateur game programmer. Please tell him where he is going wrong.
Saturday, January 21, 2006
Saturday, January 07, 2006
A Different Kind of Language
I used to say all programming langauges were the same. "Yeah, what's the difference between C, Java, Pascal and Basic? Some use squiggly brackets, some use begin/end, some use two equals signs, some use ":=". But apart from that they all boil down to pretty much the same thing."
That was until I discovered Lisp. I've only started even looking at it in the last few days, but I've learnt enough to know that all programming languages are not the same. Obviously I'd heard of Lisp; I've been programming for enough years now to have heard of all these strange old languages (Fortan, Algol etc..) that they used in the 60's, but never thought anything of actually using them. However, from what I've read, Lisp's power seems to be a well-kept secret by the Lisp community designed to give them the advantage. Their secret is out now, and as soon as I get my head round Lisp enough to actually write something more than Hello World, you'll be the first to know.
That was until I discovered Lisp. I've only started even looking at it in the last few days, but I've learnt enough to know that all programming languages are not the same. Obviously I'd heard of Lisp; I've been programming for enough years now to have heard of all these strange old languages (Fortan, Algol etc..) that they used in the 60's, but never thought anything of actually using them. However, from what I've read, Lisp's power seems to be a well-kept secret by the Lisp community designed to give them the advantage. Their secret is out now, and as soon as I get my head round Lisp enough to actually write something more than Hello World, you'll be the first to know.
Wednesday, January 04, 2006
Whats the best programming language?
I hope to write more on this sometime, but I think that if you spend more time trying to get the language to work rather than the program you are trying to write with it, you're onto a loser.
Subscribe to:
Posts (Atom)