Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

1

University of Engineering and Technology

Taxila

FMSE
End-semester project

Submitted by: Abdullah Sajid (21-SE-01)

Syed Ali Taqi(21-SE-59)

PROJECT NAME: Break Out Game

Submitted to: Dr Saima Zareen

Software Engineering Department


2021 Session

Due date: June 15th, 2023


2

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

and enhancements, adding power-ups, different brick layouts, and additional


gameplay elements to keep the game engaging and challenging.
That's a basic overview of how a typical Breakout game works. The actual
implementation may vary depending on the specific version or platform of the game.

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

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
END;

MovePaddle(dir) = PRE dir : Direction THEN


IF dir = LEFT & paddlePos > 0 THEN
paddlePos := paddlePos - PaddleSpeed
ELSIF dir = RIGHT & paddlePos < Width - 1 THEN
paddlePos := paddlePos + PaddleSpeed
END
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;

HitBlock = PRE existingBricks > 0 & killedBricks < total_bricks THEN


existingBricks, killedBricks := existingBricks - 1, killedBricks + 1
END
END

Description of machine file code:


The provided code appears to be a formal representation of a Breakout
game using a language or notation specific to modeling systems. Here's a
description of the code:
The code begins with a machine declaration for the Breakout game,
indicating that it is the primary entity being modeled.
The code defines three sets: Color, Object, and Direction. These sets are
used to categorize different elements within the game.
Next, there are several constants defined, including Width, Height,
BallSpeed, and PaddleSpeed. These constants determine various
properties of the game, such as the dimensions of the game area and the
speed of the ball and paddle.
Following the constants, there are several properties defined using the "="
operator. These properties assign specific values to the constants defined
earlier.
The code then declares several variables, including total_bricks,
paddlePos, ballPos, ballDir, existingBricks, and killedBricks. These
variables represent different aspects of the game state, such as the number
of bricks, the position of the paddle and ball, the direction of the ball, and
the count of existing and destroyed bricks.
An invariant is specified, which sets constraints and limits on the values of
the variables. For example, it ensures that the total number of bricks is
between 1 and 20, the paddle position is within the game's width, and the
sum of existing and killed bricks is always 20.
The initialization section assigns initial values to the variables, setting up
the game's initial state.
6

The code then defines several operations:


1. InitGame: This operation initializes the game by resetting the paddle
position, ball position, ball direction, existing bricks count, and
killed bricks count.
2. MovePaddle: This operation allows the player to move the paddle in
a specified direction (left or right) within the bounds of the game
area.
3. MoveBall: This operation moves the ball in its current direction,
checking for boundaries and adjusting its position accordingly.
4. HitBlock: This 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 code uses "PRE" statements to define preconditions for each
operation, specifying conditions that must be met for the operations to be
valid and executed.
HINT: In zip folder the name of machine file is Breakout
7

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;

MovePaddle(dir) = PRE dir : Direction THEN


IF (dir = LEFT & paddlePos > 0) or (dir = RIGHT & paddlePos < Width
- 1) THEN
IF dir = LEFT THEN
paddlePos := paddlePos - PaddleSpeed
ELSE
paddlePos := paddlePos + PaddleSpeed
END
END
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;

HitBlock = PRE existingBricks > 0 & killedBricks < total_bricks THEN


existingBricks, killedBricks := existingBricks - 1, killedBricks + 1
END
9

END

Description of REFINEMENT file code:


The provided code is a refinement of the previous Breakout game model.
It refines the original model by adding more details and refining the
behavior of certain operations. Here's a description of the refined code:
The code begins with a refinement declaration, stating that it refines the
original Breakout model.
The code then redefines the same set of variables and invariant from the
original model, which represent the game state and maintain the
constraints on the variables.
The initialization section remains the same, initializing the variables to
their initial values.
The code defines three operations, similar to the original model, but with
some modifications:
1. InitGame: This operation initializes the game state by resetting the
paddle position, ball position, ball direction, existing bricks count,
and killed bricks count. The modifications in this operation include
setting the paddle position to the middle of the width and the ball
position to the second row from the top.
2. MovePaddle: This operation allows the player to move the paddle in
a specified direction (left or right) within the bounds of the game
area. The modification in this operation includes changing the
conditional statement to check both the left and right directions
simultaneously. If the condition is true, the paddle position is
updated accordingly.
3. MoveBall: This operation moves the ball in its current direction,
checking for boundaries and adjusting its position accordingly.
Similar to the MovePaddle operation, the modifications here include
changing the conditional statement to check all four directions
simultaneously. If the condition is true, the ball position is updated
based on its current direction.
4. HitBlock: This 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. This operation remains the same as in the
original model.
10

HINT: In zip folder the name of REFINEMENT file is Breakout_r.

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

Description of Implementation file code:


The provided code is an implementation of the Breakout game, refining
the original model. This implementation adds concrete values and assigns
specific types to the variables. Here's a description of the implementation
code:
The code begins with an implementation declaration, stating that it
implements the refined Breakout model.
The code introduces concrete variables that specify the types of the
variables in the model. These concrete variables include total_bricks,
paddlePos, ballPos, ballDir, existingBricks, killedBricks, expression1, and
expression2.
The code defines concrete values for Height, Width, BallSpeed, and
PaddleSpeed, specifying their specific numeric values.
An invariant is provided to maintain constraints on the concrete variables,
similar to the original model.
The initialization section initializes the concrete variables, setting their
initial values.
The code defines four operations: InitGame, MovePaddle, MoveBall, and
HitBlock. These operations have the same behavior as in the refined
14

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

HINT: In zip folder the name of REFINEMENT file is Breakout_i.


15

Generated “C” code:


/* WARNING if type checker is not performed, translation could
contain errors ! */

#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 */

static int32_t Breakout__total_bricks;


static int32_t Breakout__paddlePos;
static int32_t Breakout__ballPos;
static Breakout__Direction Breakout__ballDir;
static int32_t Breakout__existingBricks;
static int32_t Breakout__killedBricks;
static bool Breakout__expression1;
static bool Breakout__expression2;
/* Clause INITIALISATION */
void Breakout__INITIALISATION(void)
{

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;
}

void Breakout__MovePaddle(Breakout__Direction dir)


{
{
int32_t S1;

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:

You might also like