(Not really a) Tutorial: example game
=====================================
A network game for 2 or 3 players (or one player and one virtual player),
moving an UFO and shooting asteroids ("rocks").
The game is located under game/.
Type "make" to compile it.
The master player calls "game_master",
the client players call "game_client".
There are 3 objects:
- the rocks
- the ships (UFOs)
- the shots of the ships
The main programs
(refer to game/src/game_master.c and game/src/game_client.c).
- create the window
- start the sound-server
- show an intro
- create the 3 objects, which set the network variables
- (start and) connect to the network-server
- play the game loop:
+ get the keystrokes, eventually quit the game
+ the master moves the objects and calls vg_nw_sendcommon()
+ the clients get the positions of the objects
+ give out the objects
- before ending the credits of the player are shown
The rock object:
(refer to game/src/o_rock.c
game/src/o_rock.h
game/src/ifo_rock.c
)
It is created by game_master and game_client by calling new_rock(),
passing the number of rocks.
This function creates some network arrays, one element of them for each rock
(with vg_nw_setcommon()).
After connecting, game_master and game_client call set_network()
to set the struct pointers (declared in o_rock.h) to the network variables.
The master's next call move() moves all rocks, checking for overlapping
with other objects, eventually destroying a rock, playing a sound
and set the network variables with the new positions of all rocks.
To check a rock with another object (whether hit by a shot or a ship),
iforock_move() in ifo_rock.c is called. This function checks with
the overlapped() function in each object for bitmap-overlapping
(whether a hit occured). If a hit is found, for both objects the function
set_dead() / set_inactive() / change_direction() is called
and eventually a sound is played. This sound play-information is also set
into a network-variable for the clients play this sound, too.
The clients call getpos() instead of move() to set the
network variables into a the o_rock-struct. If a sound is to be played,
it is played here.
At last the function giveout() is called by game_master and game_client
to draw the rocks onto screen.
The rock object is destroyed by game_master and game_client with free_rock().
The shot object:
(refer to game/src/o_shot.c
game/src/o_shot.h
game/src/ifo_shot.c
)
It is nearly used in the same way as the rock object.
A new shot is created by the ships calling activate(), using
the ship's interface function ifoship_shot() in ifo_ship.c.
In the variable "status" every shot stores the ship's number in order to give
this ship a credit, if the shot destroys a rock (ifo_shot.c: inc_credit()).
The ship object:
(refer to game/src/o_ship.c
game/src/o_ship.h
game/src/ifo_ship.c
)
It is nearly used in the same way as the rock object.
The network variables are stored with vg_nw_setvar().
The number of ships is passed in set_network(), because it is known
not before the players have connected.
The master's move() function distinguishes between
- the master's player, who is moved with keystrokes
- the virtual player, who is moved as a second master's player
- the client players, who are moved according to the network-sent keystrokes
And it sets again the new network variables and calls vg_nw_senddata().
While pressing the fire-key a sound is played repeatedly,
which is paused, when releasing the key.
The clients play this sound in the function getpos().
[Previous] - [Index] - []