0% found this document useful (0 votes)
2 views5 pages

Snake_Java - Rosetta game

This document contains the Java code for a simple Snake game implemented using Swing. The game features a snake that moves around the screen, eats apples to grow, and ends when it collides with itself or the borders. The code includes methods for game initialization, drawing the game components, handling user input, and managing game logic.

Uploaded by

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

Snake_Java - Rosetta game

This document contains the Java code for a simple Snake game implemented using Swing. The game features a snake that moves around the screen, eats apples to grow, and ends when it collides with itself or the borders. The code includes methods for game initialization, drawing the game components, handling user input, and managing game logic.

Uploaded by

Shm Jj200
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Snake/Java

< Snake

import [Link].*;
import [Link].*;
import [Link].*;

public class SnakeGame extends JPanel implements ActionListener {


private static final int WIDTH = 300;
private static final int HEIGHT = 300;
private static final int UNIT_SIZE = 10;
private static final int GAME_UNITS = (WIDTH * HEIGHT) /
(UNIT_SIZE * UNIT_SIZE);
private static final int DELAY = 75;

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


private final int[] y = new int[GAME_UNITS];
private int bodyParts = 6;
private int applesEaten;
private int appleX;
private int appleY;
private char direction = 'R';
private boolean running = false;
private Timer timer;

public SnakeGame() {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setBackground([Link]);
setFocusable(true);
addKeyListener(new MyKeyAdapter());
startGame();
}

private void startGame() {


newApple();
running = true;
timer = new Timer(DELAY, this);
[Link]();
}

@Override
public void paintComponent(Graphics g) {
[Link](g);
draw(g);
}
private void draw(Graphics g) {
if (running) {
[Link]([Link]);
[Link](appleX, appleY, UNIT_SIZE, UNIT_SIZE);

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


if (i == 0) {
[Link]([Link]);
[Link](x[i], y[i], UNIT_SIZE, UNIT_SIZE);
} else {
[Link](new Color(45, 180, 0));
[Link](x[i], y[i], UNIT_SIZE, UNIT_SIZE);
}
}

[Link]([Link]);
[Link](new Font("Arial", [Link], 14));
FontMetrics metrics = getFontMetrics([Link]());
[Link]("Score: " + applesEaten, (WIDTH -
[Link]("Score: " + applesEaten)) / 2,
[Link]().getSize());
} else {
gameOver(g);
}
}

private void newApple() {


appleX = (int) ([Link]() * (WIDTH / UNIT_SIZE)) *
UNIT_SIZE;
appleY = (int) ([Link]() * (HEIGHT / UNIT_SIZE)) *
UNIT_SIZE;
}

private 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] = y[0] - UNIT_SIZE;
break;
case 'D':
y[0] = y[0] + UNIT_SIZE;
break;
case 'L':
x[0] = x[0] - UNIT_SIZE;
break;
case 'R':
x[0] = x[0] + UNIT_SIZE;
break;
}
}

private void checkApple() {


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

private 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] >= WIDTH) {
running = false;
}

// Check if head touches top border


if (y[0] < 0) {
running = false;
}

// Check if head touches bottom border


if (y[0] >= HEIGHT) {
running = false;
}

if (!running) {
[Link]();
}
}

private void gameOver(Graphics g) {


[Link]([Link]);
[Link](new Font("Arial", [Link], 20));
FontMetrics metrics1 = getFontMetrics([Link]());
[Link]("Game Over", (WIDTH - [Link]("Game
Over")) / 2, HEIGHT / 2);

[Link]([Link]);
[Link](new Font("Arial", [Link], 14));
FontMetrics metrics2 = getFontMetrics([Link]());
[Link]("Score: " + applesEaten, (WIDTH -
[Link]("Score: " + applesEaten)) / 2,
[Link]().getSize());
}

@Override
public void actionPerformed(ActionEvent e) {
if (running) {
move();
checkApple();
checkCollisions();
}
repaint();
}

private class MyKeyAdapter extends KeyAdapter {


@Override
public void keyPressed(KeyEvent e) {
switch ([Link]()) {
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 static void main(String[] args) {


JFrame frame = new JFrame("Snake Game");
SnakeGame game = new SnakeGame();
[Link](game);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](false);
[Link]();
[Link](null);
[Link](true);
}
}

You might also like