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

Message

This document contains the implementation of a simple Snake game using Java's Swing library. It defines the game's board, snake, and food, and includes methods for movement, collision detection, and rendering graphics. The game runs in a loop, responding to keyboard inputs for direction and stopping when the snake collides with itself.

Uploaded by

visaruta57
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)
2 views4 pages

Message

This document contains the implementation of a simple Snake game using Java's Swing library. It defines the game's board, snake, and food, and includes methods for movement, collision detection, and rendering graphics. The game runs in a loop, responding to keyboard inputs for direction and stopping when the snake collides with itself.

Uploaded by

visaruta57
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 javax.swing.

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

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;
}
}

Tile snakeHead;
ArrayList<Tile> snakeBody;

Tile food;

Random random;
Timer gameLoop;
int velocityX;
int velocityY;

boolean gameOver = false;

int tileSize = 25;


int boardWidth = 600;
int boardHeight = 600;

JFrame frame = new JFrame();

public void initialBoard() {


frame.setTitle("Snake");
frame.setSize(boardWidth, boardHeight);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setPreferredSize(new Dimension(boardWidth, boardHeight));


setBackground(Color.BLACK);

addKeyListener(this);
setFocusable(true);

frame.add(this);
frame.pack();

frame.setVisible(true);
}

public void SnakeGame() {


snakeHead = new Tile(5, 5);
snakeBody = new ArrayList<Tile>();
food = new Tile(10, 10);
random = new Random();
placeFood();

velocityX = 0;
velocityY = 1;

gameLoop = new Timer(130, this);


gameLoop.start();
}

public void paintComponent(Graphics g) {


super.paintComponent(g);
draw(g);
}

public void draw(Graphics g) {


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

//Snake Head
g.setColor(Color.GREEN);
g.fill3DRect(snakeHead.x * tileSize, snakeHead.y * tileSize, tileSize,
tileSize, true);

for (int i = 0; i < snakeBody.size(); i++) {


Tile snakePart = snakeBody.get(i);
g.fill3DRect(snakePart.x * tileSize, snakePart.y * tileSize, tileSize,
tileSize, true);
}

public void placeFood() {


food.x = random.nextInt(boardWidth/tileSize);
food.y = random.nextInt(boardHeight/tileSize);
}

public boolean collision (Tile tileSnake, Tile tileFood) {


return tileSnake.x == tileFood.x && tileSnake.y == tileFood.y;
}

public void move() {

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

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.x += velocityX;
snakeHead.y += velocityY;

for (int i = 0; i <snakeBody.size(); i++) {


Tile snakePart = snakeBody.get(i);
if (collision(snakeHead, snakePart)) {
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) {
velocityX = 0;
velocityY = -1;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
velocityX = 0;
velocityY = 1;
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
velocityX = -1;
velocityY = 0;
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
velocityX = 1;
velocityY = 0;
}

@Override
public void keyTyped(KeyEvent e) {

@Override
public void keyReleased(KeyEvent e) {

}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy