-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
56 lines (51 loc) · 1.92 KB
/
Controller.java
File metadata and controls
56 lines (51 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// The breakout controller converts key presses from the user (received by the View object)
// into commands for the game (in the Model object)
// we need to use on JavaFX class
import javafx.scene.input.KeyEvent;
public class Controller
{
// instance variables for the two other components of the MVC model
public Model model;
public View view;
// we don't really need a constructor method, but include one to print a
// debugging message if required
public Controller()
{
Debug.trace("Controller::<constructor>");
}
// This is how the View talks to the Controller
// AND how the Controller talks to the Model
// This method is called by the View to respond to key presses in the GUI
// The controller's job is to decide what to do. In this case it converts
// the keypresses into commands which are run in the model
public void userKeyInteraction(KeyEvent event )
{
// print a debugging message to show a key has been pressed
Debug.trace("Controller::userKeyInteraction: keyCode = " + event.getCode() );
// KeyEvent objects have a method getCode which tells us which key has been pressed.
// KeyEvent also provides variables LEFT, RIGHT, F, N, S (etc) which are the codes
// for individual keys. So you can add keys here just by using ther name (which you
// can find out by googling 'JavaFX KeyCode')
switch ( event.getCode() )
{
case LEFT: // Left Arrow
model.moveBat( -1); // move bat left
break;
case RIGHT: // Right arrow
model.moveBat( +1 ); // Move bat right
break;
case F :
// Very fast ball movement
model.setFast(true);
break;
case N :
// Normal speed ball movement
model.setFast(false);
break;
case S :
// stop the game
model.setGameState("finished");
break;
}
}
}