I made a snake-like game based on what I had started already, and it turned out all right besides a few things.
The turning is very choppy and bad and horrible
That’s it
Or at least, that’s the only major thing. The code (I’m pretty sure) could be improved on a ton. The only thing I’m worried about is the turning right now, however.
Here’s the source and .jar, I would appreciate anyone giving some constructive criticism or help fixing the key lag/general sloppiness.
Uhh an Intel Core i7 2600K overclocked to 4GHz, Nvidia GeForce GTX 580, 8GB of RAM, 60GB SSD, and 1TB HDD…MUST…PLAY…BATTLEFIELD 3!
It seems like there is absolutely no point to the xxxPressed, dx, dy, and xxxWait variables, I can’t see any reasonable use for them, except playing around with numbers.
The variables that control all the moving are the dirXXX variables, yet everything seems fine to me…
I didn’t do any extensive debugging because your code is way overcomplicated when you could have taken out half of it.
EDIT: Aha, I stared at it a couple more minutes and I figured it out. This only happens when you press a new key while still holding down the old key. Since my fingers are moving really fast, I couldn’t really notice it but I noticed the bug in the code. I traced it down to keyReleased(int). Let’s say I hold down UP, hold RIGHT, release UP, then release RIGHT: dirUp gets set to true, then dirRight gets set to true. In keyReleased(int), when I release UP: dirRight, dirLeft, and dirDown are set to false; then I release RIGHT: dirLeft, dirUp, and dirDown are set to false. In the end everything is false so nothing is moving!
I suggest using only 1 variable for direction and having 4 constants that specify what direction to move:
private final int RIGHT = 0, LEFT = 1, UP = 2, DOWN = 3;
private int currentDirection = UP;
...
public void keyPressed(int key) {
switch(key) {
case KeyEvent.VK_RIGHT:
currentDirection = RIGHT;
break;
case KeyEvent.VK_LEFT:
currentDirection = LEFT;
break;
case KeyEvent.VK_UP:
currentDirection = UP;
break;
case KeyEvent.VK_DOWN:
currentDirection = DOWN;
break;
}
}
//forget about keyReleased
public void move() {
//calculate timeToMove
if(timeToMove) {
switch(currentDirection) {
case RIGHT:
//move right;
break;
case LEFT:
//move left;
break;
case UP:
//move up
break;
case DOWN:
//move down;
break;
}
}
}
By “overcomplicated” I meant more like “overworked”. You had lots of things going on, different variables that meant the same thing and many variables that were unused. However I was only looking at your Player class. I didn’t open any of your other source files
And of course, you can understand your own code because you typed it, you know what was going on through your head when you did (at least…for the time being ;D)