Course:CPSC312-2023-Tetris

From UBC Wiki

Tetris

Authors: Laura, Iris

What is the problem?

Given that there already exists Tetris games implemented in Haskell, we decided to build on one by adding additional features. We used this implementation as our code base, and added the following features that the original Tetris game has:

  • Displaying the number of lines cleared
  • Added levels and displaying the current level
  • Added a next piece feature that displays the piece that will fall next
  • Allow the user to restart the game by pressing the space bar

What is the something extra?

In addition to the above features, we will also implement the more complex features of putting a piece on hold and keeping track of a scoreboard. The hold feature will allow users to put the current piece on hold, which they can then take out at a later point in the game to use. The scoreboard will keep track of the top 5 scores and display them when the game is over.

What did we learn from doing this?

We were initially skeptical about using a functional language to implement a game because games involve a lot of interaction with the user and we were worried that this could make keeping the "no side effect" rule hard to maintain. However, we soon realized that the Haskell type State provides an easy way for us to do so. The game state and the process of updating it was maintained in Main.hs, which contains the IO, and we provide this module with functions that update the state by taking in a game state and outputting another game state. By doing so, we were able to easily update the state in a safe and pure way.

In addition, we also found the Gloss library to be very helpful for rendering 2D designs. The library comes with a lot of built in functions like pictures and rectangleSolid, which made it easy to create the different tetrominoes (Tetris blocks).

Having now finished the project, we would say that Haskell is suitable for building 2D games like Tetris. Although there were some tricky aspects, such as adhering to the strict type declarations we set out, we were quite impressed with how concise the overall code is. Each file contains about 160 lines of code or less (with comments!) and this makes it easy and fairly quick for someone to understand and digest the whole code base.

Submission

https://github.students.cs.ubc.ca/iris413/Tetris