[SOLVED] Snake-type Game

I made a snake-like game based on what I had started already, and it turned out all right besides a few things.

  1. The turning is very choppy and bad and horrible
  2. 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.

http://www.mediafire.com/?lxk259ducle3fev

Thanks,
-Nathan

Hehehe I’ve seen those sprites before somewhere… :wink:

Runs at 7000FPS and I quit after 2000+ points :stuck_out_tongue:

The only bug I found was when hitting two different arrow keys semi-quickly the player stops.

Hahaha yeah I couldn’t make my own version look good! :slight_smile:

Holy crap really? What kind of god-rig do you have? I get 1300 :stuck_out_tongue:

I’m working on this, debugging is my least favorite part of coding when it takes this long. Even though it’s only been an hour haha.

Do you have any idea what could be making the key-reading so sloppy?

Thanks by the way :slight_smile:
-Nathan

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! :slight_smile:

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;
        }
    }
}

Phew…lots of typing ;D

Oh :o

What makes it so complicated and difficult?

It’s weird because I can understand my code but I was looking through Minicraft and had NO idea what was going on :confused:

Hehehe updated my post.

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 :slight_smile:

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)

Thanks so much! Worked sooooooo well!

I am once again very pleased at your helpfullness ;D

-Nathan