Browse Source

update

main
Justin Senn 2 years ago
parent
commit
6594ea59d6
  1. 28
      src/main/java/Snake/Controller.java

28
src/main/java/Snake/Controller.java

@ -9,6 +9,9 @@ public class Controller {
private enum GameState { Running }; private enum GameState { Running };
private GameView gameView; private GameView gameView;
private Snake snakeModel; private Snake snakeModel;
private SnakeView snakeView;
private Apple appleModel;
private AppleView appleView;
private boolean inputHandled; private boolean inputHandled;
private GameState gameState; private GameState gameState;
@ -25,6 +28,31 @@ public class Controller {
gameView.getActionMap().put("move down", new MoveAction(Snake.SnakeDirection.DOWN)); gameView.getActionMap().put("move down", new MoveAction(Snake.SnakeDirection.DOWN));
gameView.getActionMap().put("move right", new MoveAction(Snake.SnakeDirection.RIGHT)); gameView.getActionMap().put("move right", new MoveAction(Snake.SnakeDirection.RIGHT));
} }
private void selectApplesPosition()
{
do
{
appleModel.selectGridPosition(gameView.getGridSize());
} while(!isApplePositionIsValid());
appleView.setPosition(appleModel.getPosition());
}
private boolean isApplePositionIsValid()
{
var snakeSegments = snakeModel.getBodySegments();
for(var segmentPosition: snakeSegments)
{
if(segmentPosition.equals(appleModel.getPosition()))
return false;
}
return true;
}
private void updateSnakeViewPosition()
{
snakeView.setPositions(snakeModel.getBodySegments());
}
private class MoveAction extends AbstractAction private class MoveAction extends AbstractAction
{ {

Loading…
Cancel
Save