0% found this document useful (0 votes)
38 views4 pages

Snake Game

This document contains code for a Snake game applet written in Java. The applet tracks the position and movement of the snake and apples on a board. It handles snake movement, collision detection, and scoring when apples are eaten.

Uploaded by

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

Snake Game

This document contains code for a Snake game applet written in Java. The applet tracks the position and movement of the snake and apples on a board. It handles snake movement, collision detection, and scoring when apples are eaten.

Uploaded by

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

import java.applet.

*;
import java.awt.*;
import java.awt.event.*;

public class SnakeGame extends Applet implements KeyListener, Runnable {


private static final long serialVersionUID = 1L;
private final int BOARD_WIDTH = 400;
private final int BOARD_HEIGHT = 400;
private final int UNIT_SIZE = 20;
private final int DELAY = 75;
private final Color APPLE_COLOR = Color.RED;
private final Color SNAKE_COLOR = Color.GREEN;
private final Color BODY_COLOR = new Color(45, 180, 0);

private int[] x = new int[100];


private int[] y = new int[100];
private int bodyParts = 6;
private int applesEaten;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Thread thread;

public void init() {


this.setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.addKeyListener(this);
startGame();
}

public void startGame() {


newApple();
running = true;
thread = new Thread(this);
thread.start();
}

public void run() {


while (running) {
move();
checkApple();
checkCollisions();
repaint();
try {
Thread.sleep(DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public void paint(Graphics g) {


if (running) {
// Draw apple
g.setColor(APPLE_COLOR);
g.fillOval(appleX, appleY, UNIT_SIZE, UNIT_SIZE);
// Draw snake
for (int i = 0; i < bodyParts; i++) {
if (i == 0) {
g.setColor(SNAKE_COLOR);
} else {
g.setColor(BODY_COLOR);
}
g.fillRect(x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}

// Draw score
g.setColor(Color.WHITE);
g.drawString("Score: " + applesEaten, 10, 10);
} else {
gameOver(g);
}
}

public void newApple() {


appleX = (int) (Math.random() * (BOARD_WIDTH / UNIT_SIZE)) * UNIT_SIZE;
appleY = (int) (Math.random() * (BOARD_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 checkApple() {


if ((x[0] == appleX) && (y[0] == appleY)) {
bodyParts++;
applesEaten++;
newApple();
}
}

public void checkCollisions() {


// Check if head collides with body
for (int i = bodyParts; i > 0; i--) {
if ((x[0] == x[i]) && (y[0] == y[i])) {
running = false;
}
}
// Check if head touches left border
if (x[0] < 0) {
running = false;
}
// Check if head touches right border
if (x[0] >= BOARD_WIDTH) {
running = false;
}
// Check if head touches top border
if (y[0] < 0) {
running = false;
}
// Check if head touches bottom border
if (y[0] >= BOARD_HEIGHT) {
running = false;
}
}

public void gameOver(Graphics g) {


// Display score
g.setColor(Color.RED);
g.setFont(new Font("Ink Free", Font.BOLD, 40));
g.drawString("Game Over", BOARD_WIDTH / 2 - 100, BOARD_HEIGHT / 2 - 20);
g.drawString("Score: " + applesEaten, BOARD_WIDTH / 2 - 80, BOARD_HEIGHT /
2 + 20);
}

public void keyTyped(KeyEvent e) {}

public void keyPressed(KeyEvent e) {


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 static void main(String[] args) {


// Create an instance of SnakeGame
SnakeGame snakeGame = new SnakeGame();
// Create a frame to hold the applet
Frame frame = new Frame("Snake Game");

// Add the applet to the frame


frame.add(snakeGame);

// Set the size of the frame


frame.setSize(400, 400);

// Make the frame visible


frame.setVisible(true);

// Initialize the applet


snakeGame.init();

// Start the game


snakeGame.start();
}
}

You might also like