Assignment 1: Drawing and Event-driven Programming with Xlib


Due Fri Jan 27 at 5:00 PM
This is a firm deadline, no extensions!

Starter code can be downloaded from here.


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.

Snake game.

Learning Goals

Game Description

There are various versions of Snake playable on the web such as here and here. The general rules of the game are:


  1. 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).
  2. 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.
  3. 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).
  4. 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.
  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).
  6. The game must use a range of 1-10 to specify the speed of the snake.
  7. 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.
  8. The game must use the arrow keys (and/or WASD) to control the snake's movements. Specify in the README which controls to use.
  9. The game window should be fixed at 800x600 pixels and does not need to support resizing (i.e. it can be a fixed size).
  10. 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.
  11. The game should have the ability to play, pause, and restart the level.
  12. 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.

Enhancements (Max 10%, must choose from the list below)

  1. Use texture graphics for the background, the snake, and other game objects. (10 marks)
  2. 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)
  3. 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)
  4. 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)
  5. 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).


Submit your assignment to your personal repository, under a1/ directory at


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.txt describing 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.