Змеиная игра - Змея построена, но не движется, координаты не обновляются, несмотря на то, что они написаны дословно из учебника - PullRequest
0 голосов
/ 13 мая 2018

Edit:

Я попытался заставить это работать снова, но я не мог понять это, мне было интересно, не пропало ли что-то, так что это весь код

public class GameEngine {

public static final int GameWidth = 28;
public static final int GameHeight = 42;

private List<Coordinate> walls = new ArrayList<>();
private List<Coordinate> snake = new ArrayList<>();

private Direction currentDirection  = Direction.East;

private GameState currentGameState = GameState.Running;

public void Update(){
    //snake
    switch (currentDirection){

        case North:
            UpdateSnake(0,-1);
            break;
        case East:
            UpdateSnake(1,0);
            break;
        case South:
            UpdateSnake(0,1);
            break;
        case West:
            UpdateSnake(-1,0);
            break;
    }

    for (Coordinate W: walls){
        if (snake.get(0).equals(W)){
            currentGameState = GameState.Lost;
        }
    }
}

public GameEngine() {


}

public void initGame() {



    AddSnake();
    AddWalls();
}



public TileType[][] getMap (){
    TileType[][] map = new TileType[GameWidth][GameHeight];

    for (int x = 0; x < GameWidth; x++){
        for (int y = 0; y < GameHeight; y++)
        map [x][y] = TileType.Nothing;
    }

    for (Coordinate s: snake){
        map [s.getX()][s.getY()] = TileType.SnakeTail;
    }
    map[snake.get(0).getX()][snake.get(0).getY()] = TileType.SnakeHead;

    for (Coordinate wall: walls){
        map[wall.getX()][wall.getY()] = TileType.Wall;
    }
    return map;
}



private void UpdateSnake (int x, int y){
    TileType[][] map = getMap();

    for (int i = snake.size() -1 ; i > 0 ; i--){
        map[snake.get(0).getX()][snake.get(0).getY()] = TileType.SnakeTail;
        snake.get(i).setY(snake.get(i-1).getY());

    }


snake.get(0).setX(snake.get(0).getX()+x);
snake.get(0).setY(snake.get(0).getY()+y);
}

private void AddSnake(){

    snake.clear();


    snake.add(new Coordinate(7,7));
    snake.add(new Coordinate(6,7));
    snake.add(new Coordinate(5,7));
    snake.add(new Coordinate(4,7));
    snake.add(new Coordinate(3,7));
    snake.add(new Coordinate(2,7));
}

private void AddWalls() {
    for (int x = 0; x < GameWidth; x++) {
        walls.add(new Coordinate(x, 0));
        walls.add(new Coordinate(x, GameHeight - 1));
    }

    for (int y = 1; y < GameHeight; y++) {
        walls.add(new Coordinate(0, y));
        walls.add(new Coordinate(GameWidth - 1, y));
    }
}

public GameState getCurrentGameState() {
    return currentGameState;
}

Я попробовал ответ ниже, но ошибки продолжались, и я не могу понять, почему.

Когда я пытался вставить карту обновления, предполагалось, что карта всегда подчеркивалась, как и функции get

1 Ответ

0 голосов
/ 13 мая 2018

Функция private void UpdateSnake (int x, int y){ не обновляет плитку текущей карты. Итак, вы видите, что ничего не происходит вообще.

Вам необходимо обновить карту тайлов (ту, которая была возвращена функцией public TileType[][] getMap ()):

TileType[][] map = getMap(); // Do this when you start the game. Where you call `getMap()`, just store the reference to a variable `map`

private void UpdateSnake (int x, int y){

    // Set title of snake.get(snake.size() -1) to TileType.Nothing;
    Snake tail = snake.get(snake.size() - 1);
    map[tail.getX()][tail.get(Y)] = TileType.SnakeTail;

    for (int i = snake.size()-1; i > 0; i--){
         snake.get(i).setX(snake.get(i-1).getX());
         snake.get(i).setY(snake.get(i-1).getY());

    }
    snake.get(0).setX(snake.get(0).getX()+x);
    snake.get(0).setY(snake.get(0).getY()+y);

    // Set title of snake.get(1) to TileType.SnakeTail;
    map[snake.get(0).getX()][snake.get(0).get(Y)] = TileType.SnakeTail;
    // Set title of snake.get(0) to TileType.SnakeHead;
    map[snake.get(0).getX()][snake.get(0).get(Y)] = TileType.SnakeHead;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...