Course:CPSC312-2023/Pong

From UBC Wiki
Exclaim.jpg This article is a candidate for deletion.
If you have any concerns, please start a discussion on the talk page.


Authors: Nick Wong, Michelle Kim

Contents

What is the problem?

Pong is a classic arcade game that has been around since the early 1970s. It is a two-player game in which each player controls a paddle on the opposite side of the screen. The goal of the game is to hit the ball back and forth across the screen, and each player must prevent the other from scoring a point by hitting the ball with their paddle.

What is the something extra?

Our ‘something extra’ was the simple AI opponent that can play against the player. As we were advised not to build anything too extensive to scope the project realistically, the AI opponent is designed to follow the y position of the ball in real-time and predict its future position to make quick and accurate movements to defend against the ball and prevent it from reaching the goal. Haskell’s modular design helped organize and simplify the code, making it easier to modify and maintain. For example, the game logic was separated from the rendering module that is responsible for drawing the game objects. So when we first built a two-player game where each player used a different set of keys in the keyboard, then changed the second player to an AI opponent, the other gaming logic and rendering persisted and needed not to be modified.  

What did we learn from doing this?

While it is technically possible to develop the game state by using designing our own tail-recursive game loop or by looking into using state monads, the project’s user interface was created using Gloss library, a widely used Haskell library for drawing simple graphics and provides an easy-to-use API for handling user input and creating animations. Although Gloss is the only library that we have used in the project, we developed a solid understanding of how to work with Haskell libraries in general, including reading and understanding documentations, integrating them into a project and debugging issues that arise throughout.

Our ‘something extra’ was the simple AI opponent that can play against the player. As we were advised not to build anything too extensive to scope the project realistically, the AI opponent is designed to follow the y position of the ball in real-time and predict its future position to make quick and accurate movements to defend against the ball and prevent it from reaching the goal. Haskell’s modular design helped organize and simplify the code, making it easier to modify and maintain. For example, the game logic was separated from the rendering module that is responsible for drawing the game objects. So when we first built a two-player game where each player used a different set of keys in the keyboard, then changed the second player to an AI opponent, the other gaming logic and rendering persisted and needed not to be modified.  

In conclusion, functional programming is suitable for making games like a game of pong. Functional programming makes use of functions that take inputs and return outputs without causing any side effects, making it ideal for developing games, as it allows for code that is reliable, predictable, and easy to debug. Functional programming also provides a lot of flexibility when it comes to game development. It allows for code that is modular, meaning it can be easily broken down into smaller components that can be reused and adapted for different game elements. Lastly, functional programming makes it easy to add new features to games, as the same code can be used in different parts of the game with minimal effort.

Links to code etc

https://github.com/kozr/haskell_pong