Hey man whats up. I took a second to go through your code and discovered several minor problems you might have overlooked.
First off when reading input from KeyEvent you should always use
if (e.getKeyCode() == KeyEvent.VK_#) {
//whatever you wanna do
}
Where the '#' would be the name of the key you are looking for input from.
Next, if you look here in your keyPressed method, as soon as the input is detected it is setting most your boolean values to false...
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == 37) {
left = true;
}
if (e.getKeyCode() == 38) {
up = false;
}
if (e.getKeyCode() == 39) {
right = false;
}
if (e.getKeyCode() == 40) {
down = false;
}
}
When you have specified to only move the player when these values are true, like below. So you must change them all to true.
while(true){
if (left) {
x-=2;
left = false;
}
if (up) {
y-=2;
up = false;
}
if (right) {
x+=2;
right = false;
}
if (down) {
y+=2;
down = false;
}
Instead of setting up, down, left, and right to false when they are found true, you should set them false when the same key you pressed to move the player, is released. You can do this easily by copying and pasting your keyPressed method, changing "keyPressed" to "keyReleased", and replacing all the true values with false, like so:
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = false;
}
}
This will make it so your player is moving only when the key used to move the player is held down.
What I have done in my code is the following:
- Changed the keyPressed method to read input by using "KeyEvent.VK_#"
- Set my boolean values to false in the keyReleased method
- Removed setting my boolean values to false, when they are found true in the while loop
GameLoop.java
import java.applet.Applet;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class GameLoop extends Applet implements Runnable, KeyListener {
public int x, y;
public boolean up, down, left, right;
public void run() {
x = 50;
y = 50;
while(true) {
if(left) {
x -= 2;
}
if(up) {
y -= 2;
}
if(right) {
x += 2;
}
if(down) {
y += 2;
}
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = true;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = true;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = true;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = true;
}
}
public void keyReleased(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_LEFT) {
left = false;
}
if (e.getKeyCode() == KeyEvent.VK_UP) {
up = false;
}
if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
right = false;
}
if (e.getKeyCode() == KeyEvent.VK_DOWN) {
down = false;
}
}
public void keyTyped(KeyEvent e) {};
}
Game.java
import java.awt.Graphics;
public class Game extends GameLoop {
public void init(){
setSize(300, 300);
Thread th = new Thread(this);
th.start();
addKeyListener(this);
}
public void paint(Graphics g){
g.fillRect(x, y, 20, 20);
}
}
You should be able to use this information to make the player do most things you want with basic keyboard input
Let me know if you have any questions or need further explanation.