You will implement Snake, a classic 2D video game where the objective is to control a line as it grows in length while preventing it from hitting the bounds or itself (Wikipedia).
Your program must be written in C++, using XLib. You may use any of the sample code provided in-class, or this A1 starter code. You are not allowed to share assignment solutions, or use code from any source other than those specified.
- Learn how to build an interactive game using C++, the X Window System and Xlib.
- Learn how to handle real-time 2D drawing and animation with primitive shapes.
- The game screen displays a snake (chain of blocks) always in motion and a fruit (block) at a fixed point on the screen.
- The direction of the snake can be controlled (by arrows keys) in that it can switch its direction by either turning left or right at a time.
- The objective of the snake is to eat the target fruit, which makes it grow in length. Conventionally, there is always only one fruit on the screen.
- As the snake eats the fruit, it disappears, and another one appears at a random location.
- The snake can die by eating itself (when it collides with itself) or by hitting the edge of the screen or any other obstacles.
- Your program must consist of one or more source files, and a
Makefilethat builds and runs your program. (
makeshould build it, and
make runshould run with default arguments).
- Implement Snake of your own design with C++ and Xlib, running on an XServer (Windows XMing, macOS XQuartz, or Linux). You should compile and test with g++ 4.9.4 or later (we will test on the student environment using that version). You are not allowed to use any other third-party libraries.
- The game should run in a window on the desktop. It 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).
- The game must accept two command-line parameters: (1) "frame-rate", which controls how often the screen is repainted, and (2) speed of the snake, describing how fast the snake moves in the game. E.g.
./snake 30 5describes the framerate of 30 and speed of 5.
- The game must play smoothly with proper collision detection in the range of 25 to 60 FPS (while supporting a range of 1-100 FPS for testing).
- The game must use a range of 1-10 to specify the speed of the snake.
- The snake can move 1 pixel at a time, or one "block" at a time (where a block is a fixed number of pixels). Both styles of gameplay are acceptable, but the animation should be smooth in either case.
- The game must use the arrow keys (and/or WASD) to control the snake's movements. Specify in the README which controls to use.
- The game window should be fixed at 800x600 pixels and does not need to support resizing (i.e. it can be a fixed size).
- The game should keep track of a score that updates over the course of the game. It is up to you to decide how and when to update the score.
- The game should have the ability to play, pause, and restart the level.
- Include a
README.txtwith a description of the game, the controls, the enhancements, the development environment, and anything else that TAs should know when grading.
Enhancements (Max 10%, must choose from the list below)
- Use texture graphics for the background, the snake, and other game objects. (10 marks)
- Power-ups (e.g. increase lives, invincibility, etc.) that either appear at a random point on the screen or "fall down" the screen. Be creative with this, an individual power-up is generally worth 5 marks, so implement at least two different power-ups for full marks. (5 marks per power up)
- Creative level design. A classic level is a bounded square region with no obstacles inside. You can enhance this by adding obstacles that the snake must avoid and gaps on the border that lets the snake wrap around to other side of the screen.(10 marks)
- Support for multiple levels: a level can be “beaten” and the game restarts with a new level, at a higher difficulty. The levels should each be different, and the game must increase in complexity and difficulty as the game progresses. (5 marks)
- Multi-player support. Allow two players to play the game simultaneously, each controlling their own snake using distinct controls. Gameplay and animation should remain smooth. (5 marks).
SubmissionSubmit your assignment to your personal repository, under
a1/directory at git.uwaterloo.ca:
- Ensure all files required to compile the program are checked in, including
- System and build-generated files (e.g.
.o) do not need to be checked in. These can specified in the .gitignore file.
Late assignments will not be accepted. Markers will test your application by building and running your code from the Makefile. Your submission will be assessed as follows:
- Deliverables: Code compiles and runs.
- Includes a
README.txtdescribing the development environment, the overall design, and enhancements.
- Technical requirements: event-handling, repainting, support for a reasonable framerate (25-60 FPS).
- Gameplay requirements
1.0. Initial release.
1.1. Added additional link to starter code at the top of the specification.
1.2. Added compiler details (g+ 4.9.4 or later).
1.3. Clarified that snake movement can be one "block" at a time. Added two-player support as an enhancement.