0% found this document useful (0 votes)
17 views11 pages

Snake 1

The document describes a Snake game program written in Java. It includes a SnakeGame class that extends Application and defines methods for starting the game, handling key presses, placing food, and checking for collisions. It also includes a Snake class to represent the snake with methods for movement, eating food, and checking if alive.
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)
17 views11 pages

Snake 1

The document describes a Snake game program written in Java. It includes a SnakeGame class that extends Application and defines methods for starting the game, handling key presses, placing food, and checking for collisions. It also includes a Snake class to represent the snake with methods for movement, eating food, and checking if alive.
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
You are on page 1/ 11

HARINI.

M BTECH IT-311522205009

PROGRAM:
import javafx.application.Application;
import javafx.scene.Scene; import
javafx.scene.input.KeyCode; import
javafx.scene.layout.Pane; import
javafx.scene.paint.Color; import
javafx.scene.shape.Rectangle; import
javafx.stage.Stage;

public class SnakeGame extends Application {


private static final int TILE_SIZE = 20;
private static final int WIDTH = 20; private
static final int HEIGHT = 15;

private int[][] grid = new int[WIDTH][HEIGHT];


private Snake snake;

public static void main(String[] args) {


launch(args);
}

@Override
public void start(Stage primaryStage) {
Pane root = new Pane();
Scene scene = new Scene(root, WIDTH * TILE_SIZE, HEIGHT *
TILE_SIZE); scene.setOnKeyPressed(e ->
handleKeyPress(e.getCode()));
HARINI.M BTECH IT-311522205009

snake = new Snake();

root.getChildren().addAll(snake.getSegments());
placeFood();

primaryStage.setTitle("Snake Game");
primaryStage.setScene(scene); primaryStage.show();

startGame();
}

private void startGame() {


snake.move(); checkCollision();
checkFoodCollision();

if (!snake.isAlive()) {
System.out.println("Game Over! Your Score: " + snake.getScore());
System.exit(0);
}

// Repeat the game loop javafx.animation.KeyFrame keyFrame = new


javafx.animation.KeyFrame( javafx.util.Duration.millis(150), e ->
startGame());
HARINI.M BTECH IT-311522205009

javafx.animation.Timeline timeline = new


javafx.animation.Timeline(keyFrame);
timeline.setCycleCount(javafx.animation.Animation.INDEFINITE);
timeline.play();
}

private void handleKeyPress(KeyCode code) {


switch (code) { case UP:
snake.setDirection(Direction.UP);
break; case DOWN:
snake.setDirection(Direction.DOWN);
break; case LEFT:
snake.setDirection(Direction.LEFT);
break; case RIGHT:
snake.setDirection(Direction.RIGHT);
break;
}
}

private void placeFood() { int x, y;


do { x = (int) (Math.random() *
WIDTH); y = (int) (Math.random() *
HEIGHT); } while (grid[x][y] != 0); //
Ensure the food is not placed on the snake
HARINI.M BTECH IT-311522205009

grid[x][y] = 2; // 2 represents food


Rectangle food = new Rectangle(x * TILE_SIZE, y * TILE_SIZE,
TILE_SIZE, TILE_SIZE);
food.setFill(Color.RED);
snake.getFood().add(food);
}

private void checkFoodCollision() {


for (Rectangle food : snake.getFood()) {
if
(snake.getHead().getBoundsInParent().intersects(food.getBoundsInParent())) {
snake.eatFood(); placeFood();
}
}
}

private void checkCollision() {


if (snake.getHead().getX() < 0 || snake.getHead().getX() >= WIDTH *
TILE_SIZE ||
snake.getHead().getY() < 0 || snake.getHead().getY() >= HEIGHT *
TILE_SIZE) {
snake.setAlive(false);
}

for (int i = 1; i < snake.getSegments().size(); i++) {


if
(snake.getHead().getBoundsInParent().intersects(snake.getSegments().get(i).get
HARINI.M BTECH IT-311522205009

BoundsInParent())) {
snake.setAlive(false);
break;
}
}
}

public enum Direction {


UP, DOWN, LEFT, RIGHT
}

public class Snake { private Direction


direction = Direction.RIGHT; private boolean
alive = true; private int score = 0;

private javafx.scene.shape.Rectangle head; private


java.util.List<javafx.scene.shape.Rectangle> segments = new
java.util.ArrayList<>(); private
java.util.List<javafx.scene.shape.Rectangle> food = new
java.util.ArrayList<>();

public Snake() { head = new


javafx.scene.shape.Rectangle(TILE_SIZE, TILE_SIZE);
head.setFill(Color.GREEN); segments.add(head);
}
HARINI.M BTECH IT-311522205009

public void move() {


if (!alive) return;

javafx.scene.shape.Rectangle newHead = new


javafx.scene.shape.Rectangle(TILE_SIZE, TILE_SIZE);
newHead.setFill(Color.GREEN);

switch (direction) {
case UP:
newHead.setY(head.getY() - TILE_SIZE);
newHead.setX(head.getX()); break;
case DOWN:
newHead.setY(head.getY() + TILE_SIZE);
newHead.setX(head.getX()); break;
case LEFT:
newHead.setX(head.getX() - TILE_SIZE);
newHead.setY(head.getY()); break;
case RIGHT:
newHead.setX(head.getX() + TILE_SIZE);
newHead.setY(head.getY());

break;
}

segments.add(0, newHead); // Add the new head to the front


HARINI.M BTECH IT-311522205009

// Remove the tail if the snake hasn't eaten if (food.isEmpty())


{ root.getChildren().remove(segments.remove(segments.size() -
1));
}

// Update grid grid = new


int[WIDTH][HEIGHT]; for (Rectangle
segment : segments) { int x = (int)
segment.getX() / TILE_SIZE; int y = (int)
segment.getY() / TILE_SIZE; grid[x][y]
= 1; // 1 represents snake body
}

// Check collision
checkCollision();
}

public void eatFood() { score++;


root.getChildren().remove(food.remove(0));
}

public javafx.scene.shape.Rectangle getHead() {


return head;
}
HARINI.M BTECH IT-311522205009

public java.util.List<javafx.scene.shape.Rectangle> getSegments() {


return segments;
}

public java.util.List<javafx.scene.shape.Rectangle> getFood() {


return food;
}

public void setDirection(Direction direction) {


this.direction = direction;
}

public void setAlive(boolean alive) {


this.alive = alive;
}

public boolean isAlive() {


return alive;
}

public int getScore() {


return score;
}
}
}
HARINI.M BTECH IT-311522205009

OUTPUT:
HARINI.M BTECH IT-311522205009
HARINI.M BTECH IT-311522205009

You might also like