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.
There are various versions of Snake playable on the web such as here and here. The general rules of the game are:
- 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 Makefile that builds and runs your program. (make should build it, and make run should 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 5 describes 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 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.txt with a description of the game, the controls, the enhancements, the development environment, and anything else that TAs should know when grading.
- Use texture graphics for the background, the snake, and other game objects.
- 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.
- 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.
- 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.