Java Snake Game Source Code Download
Sharing is caring! While it won't go into absolute Java programming basics it will introduce gently, concepts like threads. In addition, if you. Everything you need can be copy & pasted or downloaded from the four pages that make up this tutorial. If however you. Looking at the Board class, it appears to be responsible for tying many different parts of the game together. I see the rendering, input, game state, game speed, game setup, game rules, and collision checking all in this one class. It is good programming practice to separate the input and rendering from the. Snake 2d-java snake code *msi_333 *Game *Java *NetBeans *This is the simple snake game that i made. It is a net beans Project so.
Snake is a fun game to make as it doesn't require a lot of code (less than 100 lines with all comments removed). This is a basic implementation of the snake game, but it's missing a few things intentionally and they're left as further exploration for the reader.
Further Exploration
- Score
- When the snake eats an apple, the score should increase by one. Use context.fillText() to display the score to the screen
- Mobile and touchscreen support
- Allow the game to be scalled down to a phone size. See https://codepen.io/straker/pen/VazMaL
- Support touch controls
- Better apple spawning
- Currently the apple spawns in any random grid in the game, even if the snake is already on that spot. Improve it so it only spanws in empty grid locations
License
(CC0 1.0 Universal) You're free to use this game and code in any project, personal or commercial. There's no need to ask permission before using these. Giving attribution is not required, but appreciated.
Other Basic Games
Learn to create a full Snake game in Swing using width and height constraints and the ImageIcon class.
Join the DZone community and get the full member experience.
Join For FreeFirst we will define the constants used in our game. Download office 2003 pro iso.
The B_WIDTH and B_HEIGHT constants determine the size of the board. The DOT_SIZE is the size of the apple and the dot of the snake.
The ALL_DOTS constant defines the maximum number of possible dots on the board (900 = (300*300)/(10*10)). The RAND_POS constant is used to calculate a random position for an apple. The DELAY constant determines the speed of the game.
These two arrays store the x and y coordinates of all joints of a snake.
In the loadImages() method we get the images for the game. The ImageIcon class is used for displaying PNG images.
In the initGame() method we create the snake, randomly locate an apple on the board, and start the timer.
If the apple collides with the head, we increase the number of joints of the snake. We call the locateApple() method which randomly positions a new apple object.
In the move() method we have the key algorithm of the game. To understand it, look at how the snake is moving. We control the head of the snake. We can change its direction with the cursor keys. The rest of the joints move one position up the chain. The second joint moves where the first was, the third joint where the second was etc.
This code moves the joints up the chain.
This line moves the head to the left.
In the checkCollision() method, we determine if the snake has hit itself or one of the walls.
If the snake hits one of its joints with its head the game is over.
The game is finished if the snake hits the bottom of the board.
For full source code: http://geekonjava.blogspot.com/2015/07/make-snake-game-in-swing.html
Like This Article? Read More From DZone
Free Java Game Source Code
Published at DZone with permission of Das Nic . See the original article here.
Snake Game Source Code Java
Opinions expressed by DZone contributors are their own.