0% found this document useful (0 votes)
4 views9 pages

Pf Projec Report n

This project report details the development of a Tetris game using C++ and the Raylib library, focusing on replicating the classic gameplay with a graphical user interface. Key features include block movement, rotation, line clearing, scoring, and sound effects, all designed to enhance user experience. The report also discusses the programming concepts utilized, system architecture, and potential future improvements for the game.

Uploaded by

242888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views9 pages

Pf Projec Report n

This project report details the development of a Tetris game using C++ and the Raylib library, focusing on replicating the classic gameplay with a graphical user interface. Key features include block movement, rotation, line clearing, scoring, and sound effects, all designed to enhance user experience. The report also discusses the programming concepts utilized, system architecture, and potential future improvements for the game.

Uploaded by

242888
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

FACULTY OF COMPUTING & ARTIFICAL INTELLIGENCE

Department of Creative Technologies

PROGRAMMING FUNDAMENTALS
PROJECT REPORT

TETRIS GAME with GUI DEVELOPMENT

PROJECT CREW;
HAMZA AMEEN (242894) AYYAN IZHAR (242889)

AHMED ALI (242888) HASSAN AHMED (242844)

 Abstract

Page | 1
This report presents the development of a Tetris game built using C++ and
the Raylib library. The objective was to replicate the classic Tetris game while
incorporating a graphical user interface (GUI) to enhance user interaction. The
game includes core gameplay mechanics such as block movement, rotation, line
clearing, scoring, and level progression. Additional features like sound effects
and customizable themes were also implemented. This report also highlights the
key libraries, functions, and design considerations involved in the development
process.

 Introduction

 Background
Tetris, developed in 1984 by Alexey Pajitnov, has become one of the
most iconic and enduring puzzle games. In Tetris, players must manipulate
falling blocks to clear horizontal lines. The game ends when the blocks stack
up to the top of the screen. This project aimed to recreate the gameplay
mechanics of Tetris using C++, incorporating a GUI with the help of the Raylib
library to enhance the overall experience.

 Objectives
The Tetris Game project was developed as part of the Programming
Fundamentals course to apply basic and intermediate C++ programming
concepts. The primary goal was to create a functional and engaging game
that showcases problem-solving, logic, and creativity while providing a fun
user experience.

 Theoretical Background: Programming Concepts

This section explains the key programming concepts and structures used in
the development of the Tetris game.

1. Arrays
Arrays are one of the foundational data structures used in this game. The grid
that holds the blocks is represented by a two-dimensional array, where each
element corresponds to a cell on the Tetris board.

Purpose in Tetris: The grid is a 10x20 matrix where each cell can either be
empty or occupied by a part of a Tetrimino. The array is used to store the current
state of the game and is essential for detecting when a row is full and when the
game ends.

Page | 2
2. Loops
Loops are crucial for tasks such as rendering the game grid, moving blocks, and
clearing lines. In Tetris, loops are used to:

 Continuously check for user input (e.g., arrow keys to move or rotate blocks).
 Update the game state by moving blocks down the grid.
 Scan the grid to detect full lines and clear them.

Purpose in Tetris: Loops are used for repeatedly executing actions in the game
(e.g., moving blocks, checking collisions). This ensures that the game runs
smoothly and updates at a constant rate.

3. Functions
Functions are used to break down the game logic into manageable, reusable
parts. The game's logic is modularized into functions that handle specific tasks
such as block movement, rotation, line clearing, and collision detection.

Purpose in Tetris: Functions simplify the game’s structure and make the code
easier to manage. For example, the rotateBlock() function rotates the current block,
and the clearLine() function clears any full line from the grid.

4. Conditional Statements
Conditional statements are used extensively in Tetris for detecting events such
as when a block collides with the grid or when a line is full.

Purpose in Tetris: These statements are used to handle different situations in


the game, such as deciding whether the block can move further down or whether
the game is over.

5. Collision Detection
Collision detection is essential to the game mechanics, preventing blocks from
overlapping or going out of bounds.

Purpose in Tetris: When a block reaches the bottom of the screen or touches
other blocks, the game needs to check if the block can move any further. This is
done using collision detection, where the current block's position is compared
against the occupied positions in the grid.

6. Arrays and Matrix Transformation for Block Rotation


The rotation of blocks involves transforming their coordinates. Blocks are
represented as 4x4 matrices, and rotation is achieved by applying a matrix
transformation to the block’s current coordinates.

Purpose in Tetris: Rotation is handled through matrix transformations, where


each block's coordinates are adjusted according to the rotation.

Page | 3
 Design and Implementation

System Architecture
The system architecture of the game consists of three major components:

1. Game Logic: This module handles the core mechanics of the game, such as
the block movement, rotation, line clearing, and scoring.
2. GUI Rendering: Responsible for rendering the game grid, blocks, and UI
elements such as the score, and game-over screen, etc.
3. Event Handling: Manages user input, particularly key presses for moving and
rotating blocks, pausing and restarting the game, and handling the game-over
condition.

Each component works together to ensure that the game operates


smoothly, with constant communication between the event handling system and
the game logic.

Libraries and Built-in Functions Used


The game was developed using C++ and Raylib. Raylib provided the necessary
functions for graphical rendering, input handling, and sound management. Here
are some of the key libraries and functions used:

 Raylib: A simple and easy-to-use library for 2D game development.


o InitWindow(): Initializes the game window.
o BeginDrawing()/EndDrawing(): Used to begin and end the drawing process
for rendering.
o ClearBackground(): Clears the screen with a specified color before each
frame is drawn.
o DrawRectangle(): Draws a filled rectangle on the screen. Used for
rendering blocks on the grid.
o DrawText(): Displays text on the screen, used for showing the score and
game-over messages.
o IsKeyPressed(): Detects if a key has been pressed, such as the arrow
keys for moving and rotating blocks.
o LoadSound(), PlaySound(): Used to load and play sound effects like block
rotations and line clearing.
o SetTargetFPS(): Sets the frame rate of the game to ensure smooth
animation.

Raylib’s functions allowed for efficient rendering and input management,


making it ideal for this project.

Page | 4
 Core Components

The core components of the game include the following functionalities:

 Block Generation and Rotation:


Blocks (Tetriminos) are randomly generated from a set of seven shapes. Each
block has four possible rotations. The rotation is handled using a 2D matrix
transformation. The block's shape and its current rotation are stored in a 2D
array.
o Built-in Function: IsKeyPressed(KEY_UP): Detects when the player
presses the up arrow key to rotate the block.
 Block Movement:
Blocks can be moved left, right, and down. Movement is controlled by
detecting the appropriate key presses using Raylib’s IsKeyPressed() function.
Collision detection ensures that blocks cannot move outside the grid or
overlap with existing blocks.
o Built-in Function: IsKeyPressed(KEY_LEFT), IsKeyPressed(KEY_RIGHT):
Detects left and right arrow key presses for block movement.
 Line Clearing:
When a row in the grid is completely filled with blocks, it is cleared, and the
rows above it shift down. This is done by checking each row for filled blocks,
clearing it, and then shifting the remaining blocks downward.
o Core Logic: The grid is represented as a 2D array. After each block is
placed, the program scans each row for filled cells and clears any
complete rows.
 Scoring:
The player earns points based on the number of lines cleared at once.
Clearing one line grants 100 points, clearing two lines at once (a double)
grants 300 points, and clearing three lines at once (a triple) grants 500 points.
A "Tetris" (clearing four lines simultaneously) grants 800 points.
o Built-in Function: DrawText(): Displays the current score on the screen.

 Features

Core Features

 Moving and Rotating Blocks:


Blocks are moved left, right, and down, and can be rotated 90 degrees using
the arrow keys. The game logic prevents invalid rotations (such as rotating the
block into another block or outside the grid).
 Line Clearing and Scoring:
As lines are cleared, the score is updated. Clearing multiple lines at once
awards bonus points. The level also increases as the player clears more lines,
speeding up the block drop rate.
 Game Over Condition:
The game ends when new blocks cannot be placed on the grid because it is
filled up. The game-over screen appears, showing the final score and offering
the option to restart or quit.

Page | 5
Bonus Features

 Sound Effects:
Sound effects were added to enhance the gameplay experience. Sounds
were played when rotating blocks, clearing lines, and triggering the game-over
event. Raylib’s LoadSound() and PlaySound() functions were used to implement
this feature.

 Results

The following results were achieved:

 Performance:
The game runs smoothly at a steady 60 frames per second (FPS) even on
systems with lower hardware configurations. The use of Raylib’s efficient
drawing functions ensures that the game performs well.
 Gameplay:
The gameplay experience is intuitive, with the player using the arrow keys to
control the blocks, and the game ends when the grid is filled up.
 User Experience:
The game is easy to control, and the GUI is designed to display essential
information like the score. Sound effects add to the immersion of the game.
 Below is a screenshot showing the "Game Over" screen after a player
completes a game session:

This image captures the final state of


the game, where no more moves can be made,
and the game ends.

 Conclusion

Page | 6
The Tetris game was successfully developed, replicating the core
mechanics of the classic game while introducing a graphical user interface. The
use of C++ and the Raylib library enabled smooth gameplay and efficient
rendering. The additional features, such as sound effects provided a richer
experience for the player. Future improvements could include adding a
multiplayer mode, level progression and more advanced visual effects.

 References

 Raylib Documentation: https://www.raylib.com


 C++ Programming Language: https://cplusplus.com
 Tetris Wikipedia: https://en.wikipedia.org/wiki/Tetris

 Appendix

Page | 7
Page | 8
Page | 9

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy