Professional Documents
Culture Documents
BreakOut Game (21-SE-01, 21-SE-59)
BreakOut Game (21-SE-01, 21-SE-59)
Taxila
FMSE
End-semester project
BreakOut Game
Introduction of Game:
Breakout is a classic arcade game that was first released in 1976. The objective of the
game is to use a paddle to hit a ball and break a wall of bricks at the top of the
screen. As the bricks are destroyed, the player earns points and moves on to the next
level. Here's a breakdown of how the game works:
1. Game setup: The game starts with a screen displaying a wall of bricks at the
top and a paddle at the bottom. A ball is placed on the paddle.
2. Controls: The player controls the paddle using a joystick or arrow keys. The
paddle can move horizontally along the bottom of the screen.
3. Ball launch: To start the game, the player launches the ball by pressing a
button or releasing the ball from the paddle. The ball then starts moving
upward towards the bricks.
4. Collision detection: The game constantly checks for collisions between the
ball, the bricks, and the paddle. When a collision occurs, the game determines
the direction of the ball's movement based on the surface it hit.
5. Ball and paddle interaction: If the ball hits the paddle, it bounces off and
changes direction accordingly. The angle at which the ball hits the paddle
affects its trajectory.
6. Brick destruction: When the ball collides with a brick, the brick is destroyed,
and the ball bounces off. The player earns points for each brick destroyed.
Some bricks may require multiple hits to be destroyed.
7. Level progression: The player progresses to the next level when all the bricks
in the current level are destroyed. Each level typically increases the difficulty
by introducing new arrangements of bricks or special bricks with unique
properties.
8. Game over: The game continues until the player fails to hit the ball with the
paddle. If the ball reaches the bottom of the screen without being intercepted
by the paddle, the player loses a life. The player usually starts with a limited
number of lives, and the game ends when all lives are lost.
9. Scoring: The player earns points for various actions, such as destroying
bricks, completing levels, and sometimes even catching power-ups that may
drop from destroyed bricks. The objective is to achieve the highest score
possible.
10.Game variations: Over the years, Breakout has inspired numerous variations
3
Machine file:
Code:
/* Breakout
* Author: moon computer
* Creation date: 6/15/2023
*/
MACHINE
Breakout
SETS
Color = {BLACK, WHITE, RED, GREEN, BLUE, YELLOW};
Object = {BLOCK, PADDLE, BALL};
Direction = {LEFT, RIGHT, UP, DOWN}
CONSTANTS
Width, Height, BallSpeed, PaddleSpeed
PROPERTIES
Width = 10 & Height = 20 & BallSpeed = 1 & PaddleSpeed = 1
VARIABLES
total_bricks,
paddlePos,
ballPos,
ballDir,
existingBricks,
killedBricks
INVARIANT
total_bricks:1..20 &
paddlePos : 0..Width-1 &
4
INITIALISATION
total_bricks, paddlePos, ballPos, ballDir, existingBricks, killedBricks :=
20, 0, 0, LEFT, 20, 0
OPERATIONS
InitGame = PRE existingBricks > 0 THEN
paddlePos, ballPos, ballDir, existingBricks, killedBricks := Width / 2, 1,
DOWN, 20, 0
END;
MoveBall = PRE ballDir : Direction & ballPos > 0 & ballPos < Width *
Height THEN
IF ballDir = UP & ballPos > Width THEN
ballPos := ballPos - Width
ELSIF ballDir = DOWN & ballPos < Width * (Height - 1) THEN
ballPos := ballPos + Width
ELSIF ballDir = LEFT & ballPos mod Width > 0 THEN
ballPos := ballPos - 1
ELSIF ballDir = RIGHT & ballPos mod Width < Width - 1 THEN
ballPos := ballPos + 1
ELSE
ballPos := 1
5
END
END;
Refinement file:
Code:
/* Breakout_r
* Author: moon computer
* Creation date: 6/15/2023
*/
REFINEMENT Breakout_r
REFINES Breakout
VARIABLES
total_bricks,
paddlePos,
ballPos,
ballDir,
existingBricks,
killedBricks
INVARIANT
total_bricks:1..20 &
paddlePos : 0..Width-1 &
ballPos : 0..Width * Height &
paddlePos <= Width-1 &
ballPos <= Width * Height &
ballDir : Direction &
existingBricks : 0..20 &
killedBricks : 0..total_bricks &
existingBricks + killedBricks = 20
INITIALISATION
total_bricks, paddlePos, ballPos, ballDir, existingBricks, killedBricks :=
20, 0, 0, LEFT, 20, 0
OPERATIONS
InitGame = PRE existingBricks > 0 THEN
paddlePos, ballPos, ballDir, existingBricks, killedBricks := Width / 2, 1,
DOWN, 20, 0
8
END;
MoveBall = PRE ballDir : Direction & ballPos > 0 & ballPos < Width *
Height THEN
IF (ballDir = UP & ballPos > Width) or (ballDir = DOWN & ballPos <
Width * (Height - 1)) or
(ballDir = LEFT & ballPos mod Width > 0) or (ballDir = RIGHT &
ballPos mod Width < Width - 1) THEN
IF ballDir = UP THEN
ballPos := ballPos - Width
ELSIF ballDir = DOWN THEN
ballPos := ballPos + Width
ELSIF ballDir = LEFT THEN
ballPos := ballPos - 1
ELSE
ballPos := ballPos + 1
END
ELSE
ballPos := 1
END
END;
END
Implementation file:
Code:
/* Breakout_i
* Author: moon computer
* Creation date: 6/15/2023
*/
IMPLEMENTATION Breakout_i
11
REFINES Breakout
CONCRETE_VARIABLES
total_bricks,
paddlePos,
ballPos,
ballDir,
existingBricks,
killedBricks,
expression1,
expression2
VALUES
Height=20;
Width=10;
BallSpeed=1;
PaddleSpeed=1
INVARIANT
total_bricks: 1..20 &
paddlePos: INTEGER &
ballPos: INTEGER &
ballDir: {UP, DOWN, LEFT, RIGHT} &
existingBricks: INTEGER &
killedBricks: INTEGER&
expression1:BOOL &
expression2:BOOL
INITIALISATION
expression1:=FALSE;
expression2:=FALSE;
total_bricks := 20;
paddlePos := 0;
ballPos := 0;
ballDir := LEFT;
existingBricks := 20;
killedBricks := 0
12
OPERATIONS
InitGame=
BEGIN
paddlePos := Width / 2;
ballPos := 1;
ballDir := DOWN;
existingBricks := 20;
killedBricks := 0
END;
MovePaddle(dir)=
BEGIN
VAR S1 IN S1:=Width - 1;
IF (dir = LEFT & paddlePos > 0) or (dir = RIGHT & paddlePos < S1)
THEN
IF dir = LEFT THEN
paddlePos := paddlePos - PaddleSpeed
ELSE
paddlePos := paddlePos + PaddleSpeed
END
END
END
END;
MoveBall=
BEGIN
VAR S1,S2,S3 IN S1:=Width * (Height - 1);S2:= ballPos mod
Width;S3:=Width - 1;
IF (ballDir = UP & ballPos > Width) or (ballDir = DOWN & ballPos
< S1) or
(ballDir = LEFT & S2 > 0) or (ballDir = RIGHT & S2 < S3) THEN
IF ballDir = UP THEN
ballPos := ballPos - Width
ELSIF ballDir = DOWN THEN
ballPos := ballPos + Width
ELSIF ballDir = LEFT THEN
13
ballPos := ballPos - 1
ELSE
ballPos := ballPos + 1
END
ELSE
ballPos := 1
END
END
END;
HitBlock=
BEGIN
existingBricks := existingBricks - 1;
killedBricks := killedBricks + 1
END
END
model, but with the addition of concrete variable types and values.
The InitGame operation initializes the game state by resetting the paddle
position, ball position, ball direction, existing bricks count, and killed
bricks count. The implementation assigns specific values to these
variables based on the concrete values defined.
The MovePaddle operation allows the player to move the paddle in a
specified direction within the bounds of the game area. The
implementation uses concrete values and conditions to check and update
the paddle position.
The MoveBall operation moves the ball in its current direction, checking
for boundaries and adjusting its position accordingly. The implementation
uses concrete values and conditions to handle the ball's movement.
The HitBlock operation is triggered when the ball hits and destroys a
brick. It reduces the count of existing bricks and increases the count of
killed bricks. The implementation updates the values of the existingBricks
and killedBricks variables.
Overall, the implementation code refines the model by providing concrete
values and types for the variables, aligning them with the specific
requirements of the Breakout game. The implementation retains the
behavior of the refined model, ensuring the game functions as intended
#include "Breakout.h"
/* Clause CONCRETE_CONSTANTS */
/* Basic constants */
#define Breakout__Width 10
#define Breakout__Height 20
#define Breakout__BallSpeed 1
#define Breakout__PaddleSpeed 1
/* Array and record constants */
/* Clause CONCRETE_VARIABLES */
Breakout__expression1 = false;
Breakout__expression2 = false;
Breakout__total_bricks = 20;
Breakout__paddlePos = 0;
Breakout__ballPos = 0;
Breakout__ballDir = Breakout__LEFT;
Breakout__existingBricks = 20;
Breakout__killedBricks = 0;
}
/* Clause OPERATIONS */
16
void Breakout__InitGame(void)
{
Breakout__paddlePos = Breakout__Width / 2;
Breakout__ballPos = 1;
Breakout__ballDir = Breakout__DOWN;
Breakout__existingBricks = 20;
Breakout__killedBricks = 0;
}
S1 = Breakout__Width-1;
if((((dir == Breakout__LEFT) &&
((Breakout__paddlePos) > (0)))) ||
(((dir == Breakout__RIGHT) &&
((Breakout__paddlePos) < (S1)))))
{
if(dir == Breakout__LEFT)
{
Breakout__paddlePos = Breakout__paddlePos-
Breakout__PaddleSpeed;
}
else
{
Breakout__paddlePos =
Breakout__paddlePos+Breakout__PaddleSpeed;
}
}
}
}
void Breakout__MoveBall(void)
{
{
int32_t S1;
int32_t S2;
int32_t S3;
S1 = Breakout__Width * (Breakout__Height-1);
17
S2 = Breakout__ballPos % Breakout__Width;
S3 = Breakout__Width-1;
if((((((Breakout__ballDir == Breakout__UP) &&
((Breakout__ballPos) >
(Breakout__Width)))) ||
(((Breakout__ballDir == Breakout__DOWN) &&
((Breakout__ballPos) < (S1))))) ||
(((Breakout__ballDir == Breakout__LEFT) &&
((S2) > (0))))) ||
(((Breakout__ballDir == Breakout__RIGHT) &&
((S2) < (S3)))))
{
if(Breakout__ballDir == Breakout__UP)
{
Breakout__ballPos = Breakout__ballPos-
Breakout__Width;
}
else if(Breakout__ballDir == Breakout__DOWN)
{
Breakout__ballPos =
Breakout__ballPos+Breakout__Width;
}
else if(Breakout__ballDir == Breakout__LEFT)
{
Breakout__ballPos = Breakout__ballPos-1;
}
else
{
Breakout__ballPos = Breakout__ballPos+1;
}
}
else
{
Breakout__ballPos = 1;
}
}
}
void Breakout__HitBlock(void)
{
Breakout__existingBricks = Breakout__existingBricks-1;
Breakout__killedBricks = Breakout__killedBricks+1;
18
Proof Obligation:
Graphical Representation: