Snake game is famous classic game, starting from handset mobile to latest mobile, it always has its own admirers.
The goal of the game is the snake needs to catch its food and grow to a maximum.
In this example we will see how we can implement the logic in Java.
Logic:
The game follows the below logic
- Create a board of width & height
- Create a List to store the points(x,y) of snake.
- Create a direction(LEFT, RIGHT, TOP, BOTTOM) and for each step(screen refresh or repaint()) add a movement to points of x,y based on direction
- Based on arrow button press,
- Create a new Point(x,y) based on direction, add it to the top of the List.
- Remove the last point in the List.(if no food taken)
- If the snake eats food,
- Increment all the current points of snake in list. (based on Direction)
- add one more points to Snake List copying the point of head before increment(top of array) -> (list.add(point)
- Else
- Just increment all the current points of snake in list. (based on Direction)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
package com.tutorialflow; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.JFrame; import javax.swing.Timer; public class SnakeGame extends JFrame implements ActionListener, KeyListener{ List<Point> snakePoints = new ArrayList<Point>(); final static int width=500, height = 500, boxSize=5; static int direction =1; final static int LEFT = 1, RIGHT=2, TOP = 3, BOTTOM = 4; //1 - LEFT //2 - RIGHT //3 - TOP //4 - BOTTOM Timer timer; Point food = new Point(); public SnakeGame(){ addKeyListener(this); timer = new Timer(50, this); timer.setInitialDelay(20); timer.start(); Point point = new Point((width/boxSize)/2, (height/boxSize)/2); snakePoints.add(point); getNewFoodLocation(); } @Override public void paint(Graphics g) { Graphics2D g2D = (Graphics2D)g; g.clearRect(0, 0, width, height); logic(); drawSnake(g2D); drawFood(g2D); } private void drawSnake(Graphics2D g2d) { g2d.setColor(Color.BLUE); for(Point pt: snakePoints) { g2d.fillRect(pt.x*boxSize, pt.y*boxSize, boxSize, boxSize); } } private void drawFood(Graphics2D g2d) { g2d.setColor(Color.RED); g2d.fillRect(food.x*boxSize, food.y*boxSize, boxSize, boxSize); } private void logic() { Point head = snakePoints.get(0); if(food.x == head.x && food.y == head.y) { getNewFoodLocation(); Point lastPoint = snakePoints.get(snakePoints.size()-1); snakePoints.add(new Point(lastPoint.x, lastPoint.y)); } moveSnake(); } private void moveSnake() { Point head = snakePoints.get(0); Point pt = new Point(head.x, head.y); int xMovement = 1; int yMovement = 0; switch(direction) { case LEFT: xMovement = -1; yMovement = 0;break; case RIGHT: xMovement = 1; yMovement = 0;break; case TOP: xMovement = 0; yMovement = -1;break; case BOTTOM: xMovement = 0; yMovement = 1;break; default: xMovement =yMovement = 0;break; } pt.setLocation(pt.x+xMovement, pt.y+yMovement); snakePoints.add(0, pt); snakePoints.remove(snakePoints.size()-1); handleCrossBorder(); } private void handleCrossBorder() { Point head = snakePoints.get(0); double x = head.getX(); double y = head.getY(); if(x>width/boxSize && direction == RIGHT) { x = 0; } if(x<0 && direction == LEFT) { x = width/boxSize; } if(y>height/boxSize && direction == BOTTOM) { y = 0; } if(y<0 && direction == TOP) { y = height/boxSize; } head.setLocation(x, y); } private void getNewFoodLocation() { Random rand = new Random(); int delta =boxSize*2; food.setLocation(rand.nextInt(width/boxSize-2*delta)+delta, rand.nextInt(height/boxSize-2*delta)+delta); } public static void main(String[] args) { SnakeGame frame = new SnakeGame(); frame.setSize(width, height); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } @Override public void keyTyped(KeyEvent e) {} @Override public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_UP: if(direction!=4) { direction=3; } break; case KeyEvent.VK_DOWN: if(direction!=3) { direction=4; }; break; case KeyEvent.VK_LEFT: direction=(direction!=2)?1:direction; break; case KeyEvent.VK_RIGHT: direction=(direction!=1)?2:direction; break; case KeyEvent.VK_P: direction=0; break; } } @Override public void keyReleased(KeyEvent e) {} @Override public void actionPerformed(ActionEvent e) { repaint(); } } |