C++代写:CS349 Drawing and Event-driven Programming with Xlib



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.

Learning Goals

  • 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.

Game Description

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.


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


  1. Use texture graphics for the background, the snake, and other game objects.
  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.
  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.
  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.