|
@ -2,6 +2,7 @@ package Snake; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
import javax.swing.*; |
|
|
|
|
|
import java.awt.*; |
|
|
import java.awt.event.ActionEvent; |
|
|
import java.awt.event.ActionEvent; |
|
|
import java.awt.event.ActionListener; |
|
|
import java.awt.event.ActionListener; |
|
|
|
|
|
|
|
@ -21,6 +22,42 @@ public class Controller { |
|
|
private GameState gameState; |
|
|
private GameState gameState; |
|
|
private int score; |
|
|
private int score; |
|
|
|
|
|
|
|
|
|
|
|
Controller(Window window, Snake snakeModel, SnakeView snakeView, Apple appleModel, AppleView appleView) |
|
|
|
|
|
{ |
|
|
|
|
|
gameView = window.getGameView(); |
|
|
|
|
|
this.snakeModel = snakeModel; |
|
|
|
|
|
this.snakeView = snakeView; |
|
|
|
|
|
this.appleModel = appleModel; |
|
|
|
|
|
this.appleView = appleView; |
|
|
|
|
|
|
|
|
|
|
|
scoreView = new TextView("Scores: 9", new Point(15, 15)); |
|
|
|
|
|
messageView = new TextView("Click 'Start Game' to Begin . (W-up, S-down, A-left, D-Right)", |
|
|
|
|
|
new Point(gameView.getWidth()/2, gameView.getHeight() - 15)); |
|
|
|
|
|
|
|
|
|
|
|
scoreView.setAnchor(TextView.AnchorType.Left); |
|
|
|
|
|
|
|
|
|
|
|
gameView.addView(scoreView); |
|
|
|
|
|
gameView.addView(messageView); |
|
|
|
|
|
|
|
|
|
|
|
initializeInputHandling(); |
|
|
|
|
|
addButtonActionListeners(window); |
|
|
|
|
|
|
|
|
|
|
|
timer = new Timer(10, new ActionListener() |
|
|
|
|
|
{ |
|
|
|
|
|
@Override |
|
|
|
|
|
public void actionPerformed(ActionEvent e) |
|
|
|
|
|
{ |
|
|
|
|
|
timerTick(); |
|
|
|
|
|
} |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
gameState = GameState.Paused; |
|
|
|
|
|
inputHandled = true; |
|
|
|
|
|
score = 0; |
|
|
|
|
|
|
|
|
|
|
|
selectApplesPosition(); |
|
|
|
|
|
updateSnakeViewPosition(); |
|
|
|
|
|
} |
|
|
private void initializeInputHandling() |
|
|
private void initializeInputHandling() |
|
|
{ |
|
|
{ |
|
|
final int CONDITION = JComponent.WHEN_IN_FOCUSED_WINDOW; |
|
|
final int CONDITION = JComponent.WHEN_IN_FOCUSED_WINDOW; |
|
|