When programming a game it is normal to use programming techniques that allow the tightest possible control over what happens on the screen, and when it happens. The architecture appropriate for such programming is the event loop: a single-threaded flow of control checks for an event, responds appropriately to it, then returns to check for the following event, and so on. There are two types of event loop.
Blocking event loops are paced by user actions; non-blocking event loops must be paced by some other method, usually calls to a real-time clock.
Game programming is usually done assuming that program performance will not be degraded by other users of the computer on which the program runs, which will, unfortunately, not be true for you, at least when you program in the undergraduate environment. (Please remember that we will run your submission in the undergraduate environment, so allow yourself time to port your sbumission, and to tune it, in the undergraduate environment.)
In addition to the event loop you will need a graphics model so that you can draw things to predictable places on the screen and get input from the mouse.
Thus, the purpose of this assignment is to give you some implementation experience with three important concepts:
In doing this assignment you start with a program that does simple mouse/keyboard interaction. This program is written in C, using the Xlib library, which implements a 2D graphics model similar to the one described in class. You are assumed to be familiar with C programming and I expect you to acquire information about Xlib functions you wish to use from documentation like the following examples.
It is also useful to have some example programs to examine. Here are a few I have found on the web.
This process is quite simple: you know roughly what you want to do on the screen, and look at functions with suggestive names until you find the right one.
The game is played in a window, which can be moved around the screen, iconified and resized without influencing the progress of the game. (Except the player's ability to figure out what to do next, of course.)
The window contains a crate of bottles, each of which contains a jinn. (A jinn is a magic being, usually evil, who is able to operate without regard for the normal laws of physics. Bottles containing jinn should be kept closed at all cost. Escaped jinn -- the plural of jinn is jinn -- often try to do very bad things to the person who helps them escape. Typical jinn reasoning goes like this
During the first century after the strong magician imprisoned me in the bottle I promised myself that the person to release me would be given the right to the fulfilment of any three wishes they would choose to make. During the second century, I promised myself that the person to release me would be given every benefit I can provide -- riches, beauty, respect, love -- and that I would be their slave for an entire century. But during the third century I became impatient that the person to release me was being so slow to do it. I decided not to provide them with anything, but as a reward for releasing me I would let them live. Then, during the fourth century I became very angry with the laziness of my releaser, and I swore to eradicate them and their children, and their children's children, until the end of time. But as a reward for releasing me I would allow them to choose the manner of their death. Now, how would you like to die?
Obviously, the player wants to keep the jinn in their bottles.)
Unfortunately, the crate has been jostled, and some of the magical seals loosened. A loosened seal allows the cork of the bottle slowly to push out of the bottle, like the loosened cork of a bottle of champagne. The player has to move the mouse over the cork, and click the button to push it back into the bottle. If this is too slow then the jinn escapes and the game is lost. The process running the game, and its children and its children's children, are all terminated with utmost prejudice, but only after the jinn has a good laugh at the losing player. When all corks have been pushed back into their bottles the game is won.
Here's what you have to get yourself started.
These are all in the course account in
/u/cs498r/assignments/a1
on any CS undergraduate machine.
Here is a rough outline of the game, as implemented in the binary.
Three factors increase and decrease the difficulty of the game.
For the latter two program hot keys ( cork speed: `<' to increase, `>' to decrease; average interval: `,' to increase, `.' to decrease) so that you can experiment to get a feeling for the region in which the game is playable.
Erin's implementation is deliberately programmed to have a mediocre appearance. Choose how big things are, and where you put them, to approve the appearance. An implementation identical to hers, with perfectly answered questions is worth about 80%. Do not use any colours but black and white in your implementation, and don't bother with fancy textures either.
Here are a few possibly helpful hints.
void napms( int duration )
, from the
curses library, to get constantly moving corks and jinn. To use it I
#include <curses.h>
gcc -o game -lX11 game.c -lcurses
Work out a value for duration by experimenting in the undergrad
environment. (There are many other ways of getting the time with a fine
enough granularity, but also lots of machine dependence. For example, I
believe that napms
does not actually work on all the
undergraduate machines.)
XStringWidth
will tell you
how long a string is in pixels.When your implementation is complete, experiment informally with the speed, interval and number of bottles, to find ranges of values that are `playable' in the undergrad environment. Pay attention to what makes the game harder and what makes it easier.
Here's what we would like you to hand in.
Makefile
that compiles it. We will compile your code using
your makefile.answers.txt
, containing answers to the following
questions.
To keep you from spending too much time on this part of the assignment
we will truncate submitted answers to 200 words. (wc
is
handy for knowing how much you have written.)You need not reproduce the
questions, but please number your answers.
The assignment will be marked as follows.
When considering `quality of code' we will look at two factors
The first is more important.