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.
the sounds and images are referenced as
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++)
case 1: if (y - 15 == snakeBody.get(i).getY() && x == snakeBody.get(i).getX()) return true;
case 2: if (x + 15 == snakeBody.get(i).getX() && y == snakeBody.get(i).getY()) return true;
case 3: if (y + 15 == snakeBody.get(i).getY() && x == snakeBody.get(i).getX()) return true;
case 4: if (x - 15 == snakeBody.get(i).getX() && y == snakeBody.get(i).getY()) return true;
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
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.