Vedi
Vedi
Semester – 4
Guided by – K.D.Bhuchara
PROJECT REPORT
Project Title – Snake Game
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public SnakeGame() {
add(new GamePanel());
setTitle("Snake Game");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setResizable(false);
pack();
setLocationRelativeTo(null);
setVisible(true);
new SnakeGame();
public GamePanel() {
setBackground(Color.black);
setFocusable(true);
addKeyListener(this);
startGame();
bodyParts = 6;
score = 0;
direction = 'R';
x[i] = 0;
y[i] = 0;
newFood();
running = true;
timer.start();
}
switch (direction) {
bodyParts++;
score++;
highScore = score;
newFood();
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);
if (running) {
// Draw food
g.setColor(Color.red);
// Draw snake
if (i == 0) {
g.setColor(Color.green);
} else {
// Draw score
g.setColor(Color.white);
} else {
gameOver(g);
g.setColor(Color.red);
g.setColor(Color.white);
@Override
if (running) {
move();
checkFood();
checkCollisions();
repaint();
@Override
startGame();
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_UP:
case KeyEvent.VK_DOWN:
}
}
OUTPUT: