Snake游戏 - Snake已构建但未移动,尽管从教程

时间:2018-05-13 13:57:55

标签: java android-studio coordinates

编辑:

我尝试让它重新开始工作,但我无法弄明白,我想知道是否有什么遗漏,所以这就是整个代码

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

我尝试了下面的答案,但仍然存在错误,我无法理解原因。

当我试图放入更新地图时,建议地图总是加下划线,获取功能也是如此

0 个答案:

没有答案