I've just finished the course, and am doing bug-fixes. I have no idea how to make the snake move correctly, here's the code relating to my issue:
//snake.java
package com.codegym.games.snake;
import com.codegym.engine.cell.*;
import java.util.ArrayList;
import java.util.List;
public class Snake extends GameObject{
private static final String HEAD_SIGN = ("\uD83D\uDC7E");
private static final String BODY_SIGN = ("\u26AB");
private List<GameObject> snakeParts = new ArrayList<>();
public boolean isAlive = true;
private Direction direction = Direction.RIGHT;
public Snake(int x, int y)
{
super(x, y);
GameObject snakeP1 = new GameObject(x, y);
GameObject snakeP2 = new GameObject(x-1, y);
GameObject snakeP3 = new GameObject(x-2, y);
snakeParts.add(snakeP1);
snakeParts.add(snakeP2);
snakeParts.add(snakeP3);
}
public GameObject createNewHead()
{
GameObject head = null;
int headX = snakeParts.get(0).x;
int headY = snakeParts.get(0).y;
if (direction == Direction.LEFT){
head = new GameObject(headX-1, headY);
}
else if (direction == Direction.RIGHT){
head = new GameObject(headX+1, headY);
}
else if (direction == Direction.UP){
head = new GameObject(headX, headY+1);
}
else if (direction == Direction.DOWN){
head = new GameObject(headX, headY-1);
}
return head;
}
public void removeTail(){
snakeParts.remove(snakeParts.size()-1);
}
public void draw(Game game)
{
if (isAlive){
game.setCellValueEx(snakeParts.get(0).x, y, Color.NONE, HEAD_SIGN, Color.GREEN, 75);
for
(
int P = 1;
P < snakeParts.size();
P++
)
{
game.setCellValueEx(snakeParts.get(P).x, y, Color.NONE, BODY_SIGN, Color.GREEN, 75);
}
}
else{
game.setCellValueEx(snakeParts.get(0).x, y, Color.NONE, HEAD_SIGN, Color.RED, 75);
for
(
int P = 1;
P < snakeParts.size();
P++
)
{
game.setCellValueEx(snakeParts.get(P).x, y, Color.NONE, BODY_SIGN, Color.RED, 75);
}
}
}
public void setDirection(Direction swap)
{
boolean sameSnakeX = snakeParts.get(0).x == snakeParts.get(1).x;
boolean sameSnakeY = snakeParts.get(0).y == snakeParts.get(1).y;
if (swap == Direction.UP && this.direction != Direction.DOWN && sameSnakeY){
direction = swap;
}
else if (swap == Direction.DOWN && this.direction != Direction.UP && sameSnakeY){
direction = swap;
}
if (swap == Direction.LEFT && this.direction != Direction.RIGHT && sameSnakeX){
direction = swap;
}
else if (swap == Direction.RIGHT && this.direction != Direction.LEFT && sameSnakeX){
direction = swap;
}
}
public void move(Apple apple) {
GameObject newHead = createNewHead();
boolean checkCol = checkCollision(newHead);
if(checkCol) isAlive = false;
else if ((newHead.x<0) || (newHead.y<0) || (newHead.x==15) || (newHead.y==15) ) isAlive=false;
else {
if (apple.x == newHead.x && apple.y == newHead.y){
apple.isAlive = false;
snakeParts.add(0,newHead);
}
else{
snakeParts.add(0, newHead);
removeTail();
}
}
}
public boolean checkCollision(GameObject collisionThing){
boolean collisionCheck = false;
for (GameObject object:snakeParts){
if (object.x == collisionThing.x && object.y == collisionThing.y){
collisionCheck = true;
}
}
return collisionCheck;
}
public int getLength(){
return snakeParts.size();
}
}
//snakeGame.java
package com.codegym.games.snake;
import com.codegym.engine.cell.*;
public class SnakeGame extends Game{
private Snake snake;
public static final int WIDTH = 15;
public static final int HEIGHT = 15;
private int turnDelay;
private Apple apple;
private boolean isGameStopped;
private static final int GOAL = 28;
private int score = 0;
@Override
public void initialize()
{
setScreenSize(WIDTH, HEIGHT);
createGame();
}
private void createGame()
{
snake = new Snake(WIDTH/2, HEIGHT/2);
snake.draw(this);
//apple = new Apple(5, 5);
createNewApple();
isGameStopped = false;
drawScene();
turnDelay = 300;
setTurnTimer(turnDelay);
score = 0;
setScore(score);
//Apple apple = new Apple(7, 7);
//apple.draw(this);
}
private void drawScene()
{
for (int x = 0;
x < WIDTH; x++)
{
for (int y = 0;
y < HEIGHT; y++)
{
setCellValueEx(x, y, Color.BLACK, "");
}
}
snake.draw(this);
apple.draw(this);
}
@Override
public void onTurn(int step){
snake.move(apple);
if (snake.getLength() > GOAL){
win();
}
if (apple.isAlive == false){
createNewApple();
score = score + 5;
setScore(score);
turnDelay = turnDelay - 10;
setTurnTimer(turnDelay);
if (snake.isAlive == false){
gameOver();
}
}
drawScene();
}
@Override
public void onKeyPress(Key key){
if (key == Key.LEFT){
snake.setDirection(Direction.LEFT);
}
if (key == Key.RIGHT){
snake.setDirection(Direction.RIGHT);
}
if (key == Key.UP){
snake.setDirection(Direction.UP);
}
if (key == Key.DOWN){
snake.setDirection(Direction.DOWN);
}
if (key == key.SPACE && isGameStopped == true){
createGame();
}
}
private void createNewApple(){
Apple newApple;
do{
newApple = new Apple(getRandomNumber(WIDTH),getRandomNumber(HEIGHT));
}
while(snake.checkCollision(newApple));
this.apple = newApple;
/*while (true){
int randomAppleX = getRandomNumber(WIDTH);
int randomAppleY = getRandomNumber(HEIGHT);
apple = new Apple(randomAppleX,randomAppleY);
boolean appleColision = snake.checkCollision(apple);
if (appleColision == true){
break;
}
}*/
}
private void gameOver(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK, "\n game over", Color.RED, 5);
}
private void win(){
stopTurnTimer();
isGameStopped = true;
showMessageDialog(Color.BLACK, "\n you win", Color.RED, 5);
}
}
Dima Faraj
Level 0
Help with Snake Collision
Under discussion
Comments
- Popular
- New
- Old
You must be signed in to leave a comment
This page doesn't have any comments yet