Class Model
- java.lang.Object
-
- Model
-
public class Model extends Object
-
-
Field Summary
Fields Modifier and Type Field Description intBFirst,a collection of useful values for calculating sizes and layouts etc.GameObjballThe game 'model' - these represent the state of the game and are used by the View to display itintBALL_MOVEDistance to move bat on each keypressintBALL_SIZEHeight of menu bar space at the topGameObjbatintBAT_MOVEintBRICK_HEIGHTBrick sizeintBRICK_WIDTHBall sideGameObj[]bricks(package private) ControllercontrollerbooleanfastSet to "finished" to end the gameStringgameStatevariables that control the gameintheightWidth of gameintHIT_BOTTOMScore for hitting a brickintHIT_BRICKUnits to move the ball on each stepintMBorder round the edge of the panelGameObjOutlineintscore(package private) ViewviewThe other parts of the model-view-controller setupintwidthinitialisation parameters for the model
-
Constructor Summary
Constructors Constructor Description Model(int w, int h)CONSTRUCTOR - needs to know how big the window will be
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddToScore(int n)update the scoreGameObjgetBall()return ball objectGameObjgetBat()Return bat objectGameObj[]getBricks()return bricksBooleangetFast()Return game speed - false is normal speed, true is fastStringgetGameState()Return game running stateintgetScore()return scorevoidinitialiseGame()Initialise the game - reset the score and create the game objectsvoidmodelChanged()This is how the Model talks to the View Whenever the Model changes, this method calls the update method in the View.voidmoveBat(int direction)move the bat one step - -1 is left, +1 is rightvoidrunGame()The main animation loopvoidsetFast(Boolean value)Change game speed - false is normal speed, true is fastvoidsetGameState(String value)Methods for accessing and updating values these are all synchronized so that the can be called by the main thread or the animation thread safely Change game state - set to "running" or "finished"voidstartGame()// Animating the game The game is animated by using a 'thread'.voidupdateGame()updating the game - this happens about 50 times a second to give the impression of movement
-
-
-
Field Detail
-
B
public int B
First,a collection of useful values for calculating sizes and layouts etc.
-
M
public int M
Border round the edge of the panel
-
BALL_SIZE
public int BALL_SIZE
Height of menu bar space at the top
-
BRICK_WIDTH
public int BRICK_WIDTH
Ball side
-
BRICK_HEIGHT
public int BRICK_HEIGHT
Brick size
-
BAT_MOVE
public int BAT_MOVE
-
BALL_MOVE
public int BALL_MOVE
Distance to move bat on each keypress
-
HIT_BRICK
public int HIT_BRICK
Units to move the ball on each step
-
HIT_BOTTOM
public int HIT_BOTTOM
Score for hitting a brick
-
view
View view
The other parts of the model-view-controller setup
-
controller
Controller controller
-
ball
public GameObj ball
The game 'model' - these represent the state of the game and are used by the View to display it
-
bricks
public GameObj[] bricks
-
bat
public GameObj bat
-
score
public int score
-
Outline
public GameObj Outline
-
gameState
public String gameState
variables that control the game
-
fast
public boolean fast
Set to "finished" to end the game
-
width
public int width
initialisation parameters for the model
-
height
public int height
Width of game
-
-
Method Detail
-
startGame
public void startGame()
// Animating the game The game is animated by using a 'thread'. Threads allow the program to do two (or more) things at the same time. In this case the main program is doing the usual thing (View waits for input, sends it to Controller, Controller sends to Model, Model updates), but a second thread runs in a loop, updating the position of the ball, checking if it hits anything (and changing direction if it does) and then telling the View the Model changed. When we use more than one thread, we have to take care that they don't interfere with each other (for example, one thread changing the value of a variable at the same time the other is reading it). We do this by SYNCHRONIZING methods. For any object, only one synchronized method can be running at a time - if another thread tries to run the same or another synchronized method on the same object, it will stop and wait for the first one to finish. Start the animation thread
-
initialiseGame
public void initialiseGame()
Initialise the game - reset the score and create the game objects
-
runGame
public void runGame()
The main animation loop
-
updateGame
public void updateGame()
updating the game - this happens about 50 times a second to give the impression of movement
-
modelChanged
public void modelChanged()
This is how the Model talks to the View Whenever the Model changes, this method calls the update method in the View. It needs to run in the JavaFX event thread, and Platform.runLater is a utility that makes sure this happens even if called from the runGame thread
-
setGameState
public void setGameState(String value)
Methods for accessing and updating values these are all synchronized so that the can be called by the main thread or the animation thread safely Change game state - set to "running" or "finished"
-
getGameState
public String getGameState()
Return game running state
-
setFast
public void setFast(Boolean value)
Change game speed - false is normal speed, true is fast
-
getFast
public Boolean getFast()
Return game speed - false is normal speed, true is fast
-
getBat
public GameObj getBat()
Return bat object
-
getBall
public GameObj getBall()
return ball object
-
getBricks
public GameObj[] getBricks()
return bricks
-
getScore
public int getScore()
return score
-
addToScore
public void addToScore(int n)
update the score
-
moveBat
public void moveBat(int direction)
move the bat one step - -1 is left, +1 is right
-
-