|
@ -3,17 +3,23 @@ package Snake; |
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
import javax.swing.*; |
|
|
import java.awt.event.ActionEvent; |
|
|
import java.awt.event.ActionEvent; |
|
|
|
|
|
import java.awt.event.ActionListener; |
|
|
|
|
|
|
|
|
public class Controller { |
|
|
public class Controller { |
|
|
|
|
|
|
|
|
private enum GameState { Running }; |
|
|
|
|
|
|
|
|
private enum GameState { Running, Paused, PlayerLosed }; |
|
|
private GameView gameView; |
|
|
private GameView gameView; |
|
|
private Snake snakeModel; |
|
|
private Snake snakeModel; |
|
|
private SnakeView snakeView; |
|
|
private SnakeView snakeView; |
|
|
private Apple appleModel; |
|
|
private Apple appleModel; |
|
|
private AppleView appleView; |
|
|
private AppleView appleView; |
|
|
|
|
|
private TextView scoreView; |
|
|
|
|
|
private TextView messageView; |
|
|
|
|
|
|
|
|
private boolean inputHandled; |
|
|
private boolean inputHandled; |
|
|
|
|
|
private Timer timer; |
|
|
private GameState gameState; |
|
|
private GameState gameState; |
|
|
|
|
|
private int score; |
|
|
|
|
|
|
|
|
private void initializeInputHandling() |
|
|
private void initializeInputHandling() |
|
|
{ |
|
|
{ |
|
@ -28,7 +34,52 @@ 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 addButtonActionListeners(Window window) |
|
|
|
|
|
{ |
|
|
|
|
|
window.addStartActionListener(new ActionListener() |
|
|
|
|
|
{ |
|
|
|
|
|
@Override |
|
|
|
|
|
public void actionPerformed(ActionEvent e) |
|
|
|
|
|
{ |
|
|
|
|
|
if(gameState == GameState.Paused) |
|
|
|
|
|
{ |
|
|
|
|
|
messageView.setVisibility(false); |
|
|
|
|
|
gameState = GameState.Running; |
|
|
|
|
|
timer.start(); |
|
|
|
|
|
} |
|
|
|
|
|
else if(gameState == GameState.PlayerLosed) |
|
|
|
|
|
{ |
|
|
|
|
|
messageView.setVisibility(false); |
|
|
|
|
|
restartGame(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
window.addPauseActionListener(new ActionListener() |
|
|
|
|
|
{ |
|
|
|
|
|
@Override |
|
|
|
|
|
public void actionPerformed(ActionEvent e) |
|
|
|
|
|
{ |
|
|
|
|
|
if(gameState == GameState.Running) |
|
|
|
|
|
{ |
|
|
|
|
|
messageView.setText("Game Paused."); |
|
|
|
|
|
messageView.setVisibility(true); |
|
|
|
|
|
gameView.repaint(); |
|
|
|
|
|
gameState = GameState.Paused; |
|
|
|
|
|
timer.stop(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
} |
|
|
|
|
|
private void restartGame() |
|
|
|
|
|
{ |
|
|
|
|
|
snakeModel.reset(); |
|
|
|
|
|
score = 0; |
|
|
|
|
|
scoreView.setText("Score: " + Integer.toString(score)); |
|
|
|
|
|
selectApplesPosition(); |
|
|
|
|
|
gameState = GameState.Running; |
|
|
|
|
|
timer.start(); |
|
|
|
|
|
} |
|
|
private void selectApplesPosition() |
|
|
private void selectApplesPosition() |
|
|
{ |
|
|
{ |
|
|
do |
|
|
do |
|
|