0% found this document useful (0 votes)
14 views7 pages

Vedi

This document is a project report for a Snake Game developed in Java, guided by K.D. Bhuchara. It includes the complete source code for the game, detailing the game's mechanics, user interface, and controls. The game features a snake that grows as it consumes food, with a scoring system and collision detection for game over conditions.

Uploaded by

vedikapithadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views7 pages

Vedi

This document is a project report for a Snake Game developed in Java, guided by K.D. Bhuchara. It includes the complete source code for the game, detailing the game's mechanics, user interface, and controls. The game features a snake that grows as it consumes food, with a scoring system and collision detection for game over conditions.

Uploaded by

vedikapithadia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

JAVA (4343203)

Semester – 4
Guided by – K.D.Bhuchara

PROJECT REPORT
Project Title – Snake Game

Name – Vedika Pithadia (236020332054)


Program:
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.Random;

public class SnakeGame extends JFrame {

public SnakeGame() {

add(new GamePanel());

setTitle("Snake Game");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setResizable(false);

pack();

setLocationRelativeTo(null);

setVisible(true);

public static void main(String[] args) {

new SnakeGame();

class GamePanel extends JPanel implements ActionListener, KeyListener {

private final int WIDTH = 600;

private final int HEIGHT = 600;

private final int UNIT_SIZE = 20;

private final int GAME_UNITS = (WIDTH * HEIGHT) / (UNIT_SIZE * UNIT_SIZE);

private final int[] x = new int[GAME_UNITS];

private final int[] y = new int[GAME_UNITS];

private int bodyParts = 6;

private int foodX, foodY;

private char direction = 'R';

private boolean running = false;

private Timer timer;

private Random random;


private int score = 0;

private int highScore = 0;

public GamePanel() {

setPreferredSize(new Dimension(WIDTH, HEIGHT));

setBackground(Color.black);

setFocusable(true);

addKeyListener(this);

random = new Random();

startGame();

public void startGame() {

bodyParts = 6;

score = 0;

direction = 'R';

for (int i = 0; i < bodyParts; i++) {

x[i] = 0;

y[i] = 0;

newFood();

running = true;

timer = new Timer(200, this);

timer.start();

public void newFood() {

foodX = random.nextInt(WIDTH / UNIT_SIZE) * UNIT_SIZE;

foodY = random.nextInt(HEIGHT / UNIT_SIZE) * UNIT_SIZE;

public void move() {

for (int i = bodyParts; i > 0; i--) {

x[i] = x[i - 1];

y[i] = y[i - 1];

}
switch (direction) {

case 'U': y[0] -= UNIT_SIZE; break;

case 'D': y[0] += UNIT_SIZE; break;

case 'L': x[0] -= UNIT_SIZE; break;

case 'R': x[0] += UNIT_SIZE; break;

public void checkFood() {

if (x[0] == foodX && y[0] == foodY) {

bodyParts++;

score++;

if (score > highScore) {

highScore = score;

newFood();

public void checkCollisions() {

for (int i = bodyParts; i > 0; i--) {

if ((x[0] == x[i]) && (y[0] == y[i])) {

running = false;

break;

if (x[0] < 0 || x[0] >= WIDTH || y[0] < 0 || y[0] >= HEIGHT) {

running = false;

if (!running) {

timer.stop();

}
public void paintComponent(Graphics g) {

super.paintComponent(g);

draw(g);

public void draw(Graphics g) {

if (running) {

// Draw food

g.setColor(Color.red);

g.fillOval(foodX, foodY, UNIT_SIZE, UNIT_SIZE);

// Draw snake

for (int i = 0; i < bodyParts; i++) {

if (i == 0) {

g.setColor(Color.green);

} else {

g.setColor(new Color(45, 180, 0));

g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);

// Draw score

g.setColor(Color.white);

g.setFont(new Font("Arial", Font.BOLD, 18));

g.drawString("Score: " + score, 10, 20);

g.drawString("High Score: " + highScore, WIDTH - 150, 20);

} else {

gameOver(g);

public void gameOver(Graphics g) {

g.setColor(Color.red);

g.setFont(new Font("Arial", Font.BOLD, 40));


FontMetrics metrics = getFontMetrics(g.getFont());

g.drawString("Game Over", (WIDTH - metrics.stringWidth("Game Over")) / 2, HEIGHT / 2);

g.setColor(Color.white);

g.setFont(new Font("Arial", Font.PLAIN, 20));

g.drawString("Your Score: " + score, (WIDTH - 120) / 2, HEIGHT / 2 + 40);

g.drawString("High Score: " + highScore, (WIDTH - 140) / 2, HEIGHT / 2 + 70);

g.drawString("Press ENTER to Restart", (WIDTH - 180) / 2, HEIGHT / 2 + 100);

@Override

public void actionPerformed(ActionEvent e) {

if (running) {

move();

checkFood();

checkCollisions();

repaint();

@Override

public void keyPressed(KeyEvent e) {

if (!running && e.getKeyCode() == KeyEvent.VK_ENTER) {

startGame();

switch (e.getKeyCode()) {

case KeyEvent.VK_LEFT:

if (direction != 'R') direction = 'L'; break;

case KeyEvent.VK_RIGHT:

if (direction != 'L') direction = 'R'; break;

case KeyEvent.VK_UP:

if (direction != 'D') direction = 'U'; break;

case KeyEvent.VK_DOWN:

if (direction != 'U') direction = 'D'; break;

}
}

public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {}

OUTPUT:

You might also like