Snake Game using JAVA

Snake Game using JAVA 

Here is the link to play the game in your pc.

https://www.mediafire.com/file/5avqwkgnjxl58zt/snake.jar/file


Source Code: 

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;

public class SnakeGame extends JPanel implements ActionListener, KeyListener{
    private class Tile{
        int x;
        int y;

        Tile(int x, int y){
            this.x = x;
            this.y = y;
        }
    }
    int boardHeight;
    int boardWidth;
    int tileSize = 25;

    //snake
    Tile snakeHead;
    ArrayList<Tile> snakeBody;

    //
    Tile food;
    Random random;

    //game logic
    Timer gameLoop;
    int VelocityX;
    int VelocityY;
    boolean gameOver = false;

    SnakeGame(int boardHeight, int boardWidth){
        this.boardHeight = boardHeight;
        this.boardWidth = boardWidth;
        setPreferredSize(new Dimension(this.boardHeight,this.boardWidth));
        setBackground(Color.black);
        addKeyListener(this);
        setFocusable(true);

        snakeHead = new Tile(5,5);
        snakeBody = new ArrayList<Tile>();

        food = new Tile(10, 10);
        random = new Random();
        placeFood();

        VelocityX = 0;
        VelocityY = 0;

        gameLoop = new Timer(100, this);
        gameLoop.start();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        draw(g);
    }

    public void draw(Graphics g){

        // //Grid
        // for(int i = 0; i<boardWidth/tileSize; i++){
        //     //x1,y1,x2,y2
        //     g.drawLine(i*tileSize,0,i*tileSize,boardHeight);
        //     g.drawLine(0,i*tileSize,boardWidth,i*tileSize);
           
        // }

        //food
        g.setColor(Color.RED);
        // g.fillRect(food.x*tileSize, food.y*tileSize, tileSize, tileSize);
        g.fill3DRect(food.x*tileSize, food.y*tileSize, tileSize, tileSize,true);


        //snake head
        g.setColor(Color.green);
        // g.fillRect(snakeHead.x*tileSize, snakeHead.y*tileSize, tileSize, tileSize);
        g.fill3DRect(snakeHead.x*tileSize, snakeHead.y*tileSize, tileSize, tileSize, true);


        //snake body
        for(int i = 0; i<snakeBody.size(); i++){
            Tile snakePart = snakeBody.get(i);
            // g.fillRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize);
            g.fill3DRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize, tileSize,true);
        }

        //score
        g.setFont(new Font("Arial", Font.PLAIN, 16));
        if(gameOver){
            g.setColor(Color.red);
            g.drawString("Game Over: "+ String.valueOf(snakeBody.size()), tileSize - 16, tileSize);
        }
        else{
            g.drawString("Score: " + String.valueOf(snakeBody.size()), tileSize-16, tileSize);
        }
    }

    public void placeFood(){
        food.x = random.nextInt(boardWidth/tileSize);//600/25 = 24
        food.y = random.nextInt(boardHeight/tileSize);
    }


    public boolean collision(Tile tile1, Tile tile2) {
        return tile1.x == tile2.x && tile1.y == tile2.y;
    }

    public void move(){

        //eat food
        if(collision(snakeHead, food)) {
            snakeBody.add(new Tile(food.x, food.y));
            placeFood();
        }

        //snake body
        for(int i = snakeBody.size()-1; i>=0; i--){
            Tile snakePart = snakeBody.get(i);
            if(i == 0){
                snakePart.x = snakeHead.x;
                snakePart.y = snakeHead.y;
            }
            else{
                Tile prevSnakePart = snakeBody.get(i-1);
                snakePart.x = prevSnakePart.x;
                snakePart.y = prevSnakePart.y;
            }
        }

        //snakeHead
        snakeHead.x += VelocityX;
        snakeHead.y += VelocityY;

        //game over conditions
        for(int i = 0; i<snakeBody.size(); i++){
            Tile snakePart = snakeBody.get(i);
            //collide with snake head
            if(collision(snakeHead, snakePart)){
                gameOver = true;
            }
        }

        if(snakeHead.x*tileSize < 0 || snakeHead.x*tileSize > boardWidth || snakeHead.y < 0 || snakeHead.y > boardHeight){
            gameOver = true;
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        move();
        repaint();
        if(gameOver){
            gameLoop.stop();
        }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_UP && VelocityY != 1){
            VelocityX = 0;
            VelocityY = -1;
        }
        else if(e.getKeyCode() == KeyEvent.VK_DOWN && VelocityY != -1){
            VelocityX = 0;
            VelocityY = 1;
        }
        else if(e.getKeyCode() == KeyEvent.VK_LEFT && VelocityX != 1){
            VelocityX = -1;
            VelocityY = 0;
        }
        else if(e.getKeyCode() == KeyEvent.VK_RIGHT && VelocityX != -1){
            VelocityX = 1;
            VelocityY = 0;
        }

    }


    // we dont need this code below
    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyTyped(KeyEvent e) {}
}



Comments

Popular posts from this blog

AI and Human Evolution

To Do List App (GUI) using C#