Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/java/com/codecool/snake/GameFacade.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.codecool.snake;

public class GameFacade {
private Game game;
private Display display;
private GameTimer gameTimer;

public GameFacade() {

Globals.getInstance().setupResources();
game = new Game();
display = Globals.getInstance().display;
gameTimer = new GameTimer();
}

public void initializeGame() {

game.init();
game.start();
}

public void startGameLoop() {

GameLoop gameLoop = Globals.getInstance().getGameLoop();
gameTimer.setup(gameLoop::step);
gameTimer.play();
}

public void stopGame() {
// Detiene el juego
gameTimer.stop();
System.out.println("Game stopped.");
}


public Game getGame() {
return game;
}

public Display getDisplay() {
return display;
}

public GameTimer getGameTimer() {
return gameTimer;
}
}
23 changes: 16 additions & 7 deletions src/main/java/com/codecool/snake/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

public static void main(String[] args) {
launch(args);
}

public class Main extends Application {
private GameFacade gameFacade;

@Override
public void start(Stage primaryStage) {
Game game = new Game();
Scene mainScene = new Scene(game, Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT);
// Inicializa la Facade y el juego
gameFacade = new GameFacade();
gameFacade.initializeGame();

// Crea la escena principal
Scene mainScene = new Scene(gameFacade.getGame(), Globals.WINDOW_WIDTH, Globals.WINDOW_HEIGHT);
primaryStage.setTitle("Snake Game");
primaryStage.setScene(mainScene);
primaryStage.show();

game.start();
// Inicia el bucle del juego
gameFacade.startGameLoop();
}

@Override
public void stop() throws Exception {
// Detiene el juego cuando se cierra la ventana
gameFacade.stopGame();
System.out.println("Exiting..");
}

public static void main(String[] args) {
launch(args);
}
}
85 changes: 54 additions & 31 deletions src/main/java/com/codecool/snake/entities/snakes/Snake.java
Original file line number Diff line number Diff line change
@@ -1,81 +1,104 @@
package com.codecool.snake.entities.snakes;

import com.codecool.snake.DelayedModificationList;
import com.codecool.snake.Globals;
import com.codecool.snake.entities.Animatable;
import com.codecool.snake.entities.GameEntity;
import com.codecool.snake.eventhandler.InputHandler;

import javafx.geometry.Point2D;
import javafx.scene.input.KeyCode;
import java.util.ArrayList;
import java.util.List;


public class Snake implements Animatable {
public class Snake implements Animatable, SnakePart {
private static final float speed = 2;
private int health = 100;

private SnakeHead head;
private DelayedModificationList<GameEntity> body;

private List<SnakePart> parts = new ArrayList<>();

public Snake(Point2D position) {
head = new SnakeHead(this, position);
body = new DelayedModificationList<>();

addPart(4);
SnakeHead head = new SnakeHead(this, position);
parts.add(head);
addParts(4);
}

@Override
public void step() {
SnakeControl turnDir = getUserInput();
head.updateRotation(turnDir, speed);

((SnakeHead) parts.get(0)).updateRotation(turnDir, speed);
updateSnakeBodyHistory();
checkForGameOverConditions();

body.doPendingModifications();
}

private SnakeControl getUserInput() {
SnakeControl turnDir = SnakeControl.INVALID;
if(InputHandler.getInstance().isKeyPressed(KeyCode.LEFT)) turnDir = SnakeControl.TURN_LEFT;
if(InputHandler.getInstance().isKeyPressed(KeyCode.RIGHT)) turnDir = SnakeControl.TURN_RIGHT;
if (InputHandler.getInstance().isKeyPressed(KeyCode.LEFT)) turnDir = SnakeControl.TURN_LEFT;
if (InputHandler.getInstance().isKeyPressed(KeyCode.RIGHT)) turnDir = SnakeControl.TURN_RIGHT;
return turnDir;
}

public void addPart(int numParts) {
GameEntity parent = getLastPart();
public void addParts(int numParts) {
SnakePart parent = getLastPart();
Point2D position = parent.getPosition();

for (int i = 0; i < numParts; i++) {
SnakeBody newBodyPart = new SnakeBody(position);
body.add(newBodyPart);
parts.add(newBodyPart);
parent = newBodyPart;
}
Globals.getInstance().display.updateSnakeHeadDrawPosition(head);
Globals.getInstance().display.updateSnakeHeadDrawPosition((GameEntity) parts.get(0));
}

public void changeHealth(int diff) {
health += diff;
}

private void checkForGameOverConditions() {
if (head.isOutOfBounds() || health <= 0) {
if (((GameEntity) parts.get(0)).isOutOfBounds() || health <= 0) {
System.out.println("Game Over");
Globals.getInstance().stopGame();
}
}

private void updateSnakeBodyHistory() {
GameEntity prev = head;
for(GameEntity currentPart : body.getList()) {
currentPart.setPosition(prev.getPosition());
prev = currentPart;
updatePartPosition(parts.get(0), parts.get(0).getPosition());
}

private void updatePartPosition(SnakePart part, Point2D newPosition) {
for (SnakePart child : parts) {
if (child != part) {
Point2D oldPosition = child.getPosition();
child.updatePosition(newPosition);
updatePartPosition(child, oldPosition);
}
}
}

private GameEntity getLastPart() {
GameEntity result = body.getLast();
private SnakePart getLastPart() {
return parts.get(parts.size() - 1);
}

@Override
public void updatePosition(Point2D newPosition) {
parts.get(0).updatePosition(newPosition);
}

@Override
public Point2D getPosition() {
return parts.get(0).getPosition();
}

public void add(SnakePart part) {
parts.add(part);
}

public void remove(SnakePart part) {
parts.remove(part);
}

public SnakePart getChild(int index) {
return parts.get(index);
}

if(result != null) return result;
return head;
public List<SnakePart> getChildren() {
return parts;
}
}
21 changes: 14 additions & 7 deletions src/main/java/com/codecool/snake/entities/snakes/SnakeBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
import com.codecool.snake.entities.GameEntity;
import com.codecool.snake.Globals;
import javafx.geometry.Point2D;

import java.util.LinkedList;
import java.util.Queue;



public class SnakeBody extends GameEntity {
public class SnakeBody extends GameEntity implements SnakePart {
private Queue<Point2D> history = new LinkedList<>();
private static final int historySize = 10;

Expand All @@ -25,9 +22,19 @@ public SnakeBody(Point2D coord) {

@Override
public void setPosition(Point2D pos) {
Point2D currentPos = history.poll(); // remove the oldest item from the history
Point2D currentPos = history.poll();
setX(currentPos.getX());
setY(currentPos.getY());
history.add(pos); // add the parent's current position to the beginning of the history
history.add(pos);
}

@Override
public Point2D getPosition() {
return new Point2D(getX(), getY());
}

@Override
public void updatePosition(Point2D newPosition) {
setPosition(newPosition);
}
}
}
24 changes: 15 additions & 9 deletions src/main/java/com/codecool/snake/entities/snakes/SnakeHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
import com.codecool.snake.entities.Interactable;
import com.codecool.snake.entities.enemies.Enemy;
import com.codecool.snake.entities.powerups.SimplePowerUp;

import javafx.geometry.Point2D;


public class SnakeHead extends GameEntity implements Interactable {
public class SnakeHead extends GameEntity implements SnakePart, Interactable {
private static final float turnRate = 2;
private Snake snake;

Expand Down Expand Up @@ -39,18 +37,26 @@ public void updateRotation(SnakeControl turnDirection, float speed) {

@Override
public void apply(GameEntity entity) {
if(entity instanceof Enemy){
System.out.println(getMessage());
if (entity instanceof Enemy) {
snake.changeHealth(((Enemy) entity).getDamage());
}
if(entity instanceof SimplePowerUp){
System.out.println(getMessage());
snake.addPart(4);
if (entity instanceof SimplePowerUp) {
snake.addParts(4);
}
}

@Override
public void updatePosition(Point2D newPosition) {
setPosition(newPosition);
}

@Override
public Point2D getPosition() {
return new Point2D(getX(), getY());
}

@Override
public String getMessage() {
return "IMMA SNAEK HED! SPITTIN' MAH WENOM! SPITJU-SPITJU!";
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/codecool/snake/entities/snakes/SnakePart.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package com.codecool.snake.entities.snakes;

import javafx.geometry.Point2D;

public interface SnakePart {
void updatePosition(Point2D newPosition);
Point2D getPosition();
}