A1: Event Loops and Drawing (C++/Xlib) -- Breakout!
Due Fri Sept 28 at 5:00 PM
Implement a "breakout" game in C or C++ and Xlib.
Breakout is a simple 2D video game, originally published by Atari in 1976. "In the game, a layer of bricks lines the top third of the screen. A ball travels across the screen, bouncing off the top and side walls of the screen. When a brick is hit, the ball bounces away and the brick is destroyed. The player loses a turn when the ball touches the bottom of the screen. To prevent this from happening, the player has a movable paddle to bounce the ball upward, keeping it in play." (see Wikipedia.)
- Learn how the event loop that underlies all GUI programs works.
- Learn how to handle real-time drawing and animation of simple graphics.
- Learn about the painter's algorithm and double buffering.
There are many versions of Breakout playable on the web (here or here). You need to implement a simple version that includes the following features:
- Your game should have at least 5 rows of colored blocks along the top of the screen, laid out in a similar manner to the screenshot above. Where is a single paddle along the bottom, and the player can move the paddle left and right across the screen.
- When the game starts, a ball moves across the screen, bouncing off the side and top walls in a realistic fashion.
- If the ball hits a block, the block disappears. If the ball strikes the paddle, it bounces back up at a realistic angle. If the ball touches the bottom of the screen, the game ends, and you should prompt the user to either play again, or quit the game.
- If the player clears all of the blocks, you should load a new set of blocks and allow them to continue playing. It can be the same configuration as the first set, or a different layout - your choice.
- You should keep track of the player's score, awarding points for every bounce of the ball (this isn't very specific, so just make sure that you accumulate points as they play). Display the current score on-screen during the game.
- Implement a breakout game of your own design using C++ 14 and Xlib, meeting the game requirements described above.
- Your assignment must compile and run on one of the
linux.student.cscomputers, since this is where they will be graded. Your makefile should include correct command-line arguments for g++ and C++ 14 features in that environment (e.g.
g++-5 -std=c++14 program.cc -o program)
- Your game must accept two command-line parameters: (1) "frame-rate", measured in frames per second, which controls how often the screen is painted or redrawn, and (2) the "speed" of the ball in-game. Acceptable values are: 10 to 60 for frame-rate, and 1 to 10 for ball speed. Changing the frame-rate should NOT change the speed of the ball.
- Your game should open with a splash screen that includes your name, userid, and a description of how to play the game (including a description of which keys to use).
- Your game must use the keyboard to move the paddle. In-game actions should include the ability to move the paddle left and right, and quit the game.
- Gameplay should progress as described above.
- Assume that the TA marking your game is a terrible gamer. You'll want to tune the game so that it provides an enjoyable experience for novice gamers.
- You're encouraged to be creative in this assignment! Some portion of your grade is based on building an enjoyable and exciting game.
- Your window should run in a fixed-size (non-resizable) window, sized to be 1280x800 pixels. You should prevent the user from resizing the window.
- Your game should use double-buffering to eliminate any "stuttering" in gameplay.
- Your game should be single-threaded.
- Your event loop must:
- Not consume all of the CPU cycles (test: Run the "top" command in one terminal while running the game in another. Your game plus the X server should consume less than 25% of the CPU -- and even 25% is high).
- Repaint on a regular basis (i.e. repaint using the values from the command-line parameters)
- Respond to events on a timely basis (i.e. can handle many events coming all at once, such as resizing the window, without lag.)
- Be easy to understand (i.e. few nested loops, appropriate use of helper functions, etc.)
- Submit a
makefilesuch that the default target will compile and execute your program.
Submit the following to your personal Git repository, in the
- All files required to compile and run your program.
makefilethat can build and run your program. The default target should compile and execute.
readme.mdfile that explains how to play your game, and includes any other instructions that the TA might need to grade it.
Late assignments will not be accepted. Your submission will be assessed roughly as follows:
- Working game that meets the basic gameplay requirements described above.
- Technical requirements met, including command-line parameters, proper handling of the event loop. There is no lag, and the screen resizes and repaints properly.
- Screen design, aesthetics, enjoyable gameplay.
Versions that have been released:
- 1.0. Sept 7. Initial release.
- 1.1. Sept 12. Removed requirement to handle window resizing. Adjusted mark breakdown.
- 1.2. Sept 12. Added requirement that describes clearing the blocks.
- 1.3. Sept 23. Added range of values for FPS and ball speed.