3 Replies - 1137 Views - Last Post: 28 December 2011 - 07:03 AM Rate Topic: -----

#1 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Java Snake Game : from Martyr2's List

Posted 28 December 2011 - 06:30 AM

Hey everyone last semester an assignment we had was to create a snake game, my partner and I scored well on this assignment so I figured I could see what you all thought. I have uploaded the .java and the sounds, images file.

when running, just make sure all your files are in the proper directories.
i.e.
the sounds and images are referenced as
filepath = "sounds/soundfile";
filepath = "images/imagefile";



Feel free to let me know what you think!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Snake Game : from Martyr2's List

#2 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Java Snake Game : from Martyr2's List

Posted 28 December 2011 - 06:36 AM

Apparently I did not attach the file, Lets see if it works this time. Also, sound file is WAV format so it is too big to upload

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 994
  • View blog
  • Posts: 2,208
  • Joined: 05-April 11

Re: Java Snake Game : from Martyr2's List

Posted 28 December 2011 - 06:56 AM

Separate your logic from view. I see a lot of code that shouldn't be in the GameField class.

Also you are using a lot of hardcoded constants. It is better to make a variable to hold them.

You are mixing Swing and threads, which is not a good idea (Swing isn't thread safe for most parts). You have already made a swing timer, why are you not using this as a game tick?

Instead of having your direction as an int, you could use an enum. You possibly haven't learned about them yet, but you should go and read about it :)
I don't like your Snake class being run in a new thread. At every game tick you should get user input, update snake coordinates, and repaint. There is no need for yet another thread for the snake.


Why don't you save the snake before the switch, instead of calling the get method over and over again? Same goes for snake x and y.
        //checks to see if snake runs into itself
        for(int i = 2; i < snakeBody.size(); i++)
        {
            switch(dir){
            case 1: if (y - 15 == snakeBody.get(i).getY() && x == snakeBody.get(i).getX()) return true;
                    break;
            case 2: if (x + 15 == snakeBody.get(i).getX() && y == snakeBody.get(i).getY()) return true;
                    break;
            case 3: if (y + 15 == snakeBody.get(i).getY() && x == snakeBody.get(i).getX()) return true;
                    break;
            case 4: if (x - 15 == snakeBody.get(i).getX() && y == snakeBody.get(i).getY()) return true;
                    break;
            }
        }


Was This Post Helpful? 2
  • +
  • -

#4 Amatore  Icon User is offline

  • New D.I.C Head

Reputation: 9
  • View blog
  • Posts: 47
  • Joined: 27-December 11

Re: Java Snake Game : from Martyr2's List

Posted 28 December 2011 - 07:03 AM

I also found it strange that the instructor wanted us to incorporate these two topics. She wanted us to use multi-threading so the snake can be constantly moving, and at the same time we could be placing apples. I have read a little on Enums but have never tried to put them in use.

and you said

Quote

At every game tick you should get user input, update snake coordinates, and repaint.


With how the game is moving, only the direct can be changed by the user, so it needs to be constantly moving and accepts a change in direct at all times.

And I understand about the too much logic in the design, this was our first swing application, I suppose I could have cleaned it up a little.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1