Course:CPSC312-2024/Pong

From UBC Wiki

Authors: Martin, Inaki, & Jade

What is the problem?

Pong is a classic arcade game that incorporates player interaction alongside machine reaction. In the game, a ball is hit back and forth and you are tasked with never allowing it to pass your paddle. We will design this game using the functional programming language Haskell, where the user will have an interaction pane through which they can control Pong intuitively. We plan to include most features of the game, where the 'w' key moves the player's paddle up, the 's' key moves the player's paddle down, and the 'q' key ends the game. Once the ball passes either the player's paddle or the computer's paddle, the game ends and your score is displayed in the window.

What is the something extra?

We implement a fully functional GUI component to our Pong game that allows for smoother and easier interaction between the player and the game. This extends what we have learned in class as all that we have covered so far does not involve any external ways of interacting with our programs. Our goal is that having a GUI that facilitates the game creates a better user experience and an overall more interesting way to play Pong.

What did we learn from doing this?

The main takeaway we have gotten from this project are the benefits of using functional programming for a big task like making a game. We see that we can make use of Haskell's functional features in order to construct Pong as efficiently as possible.

Pattern matching was a key part in making use of the Haskell language for this project. One example of pattern matching is in the movePaddle function, one of the most fundamental parts of our code. Haskell allowed us to efficiently write different cases and different code for each case based on the behavior we wanted. This way, functionality can be extended while keeping just one function. Similar pattern matching was also used in the drawCommands and moveBall functions.

Haskell's IO type is notably monadic, meaning it allows for input/output actions to happen in a sequenced and reliable way. Our main function makes use of this by facilitating many different types of interactions when running the game, such as hiding the cursor, drawing the game frame and more.

In general, Haskell offers many features to improve the ability of a programmer to create projects like ours, such as lazy evaluation or pattern matching to reduce the amount of explicit code. Similar functions can be boiled down to one abstract function, saving work for the programmer and increasing the comprehensibility of the code.

We found that functional programming is in fact suitable for our project, but that there are limitations alongside the benefits of using it. One major benefit of using a functional language is the immutability of data across our code, which allows for easier debugging and reasoning about the code. While we didn't make very much use of it, Haskell's lazy evaluation can be a major advantage in using a functional language, as it can save time and processing power. However, Haskell can be beaten pretty easily on the front of games when existing infrastructure exists for game efficiency which exist in languages like C or Java. Similarly there aren't a lot of public resource libraries readily available for Haskell, at least not as many as there are for more popular languages. One final issue that we encountered is that it can be a lot less intuitive to debug in Haskell, although this is more of a personal preference and has to do with our individual experience as programmers.

Work division

Work was divided mostly evenly; Inaki formed the idea and started coding while Martin updated the wiki and helped debug and add new features, and Jade completed the GUI component of the project. We all agree that work was evenly divided, and all feel that the workload was fair for this project. We all communicated and worked as efficiently as we could, and stayed active on the project despite busy schedules.

Links to code etc.

https://github.students.cs.ubc.ca/jadefink/cpsc312-group.git