14.2 The 2D Game Stub

The gamestub.* files are meant to be a possible starting point for your games. Alter the gamestub.* files freely. The basic idea is to have five kinds of critters: the player, the player's bullets, a rival armed critter, the rival's bullets, and a prop critter that can act as a kind of food or health-pack for the player. This means that six classes are prototyped in gamestub.h : cGameStub, cCritterStubPlayer, cCritterStubPlayerBullet, cCritterStubRival, cCritterStubRivalBullet, and cCritterStubProp. There's some more discussion of these classes and a UML diagram (Figure 3.11) of them in the last section of Chapter 3: The Pop Framework.

The cGameStub has an int _rivalcount field in addition to the int _seedcount field it inherits from cGame. These are used in the seedCritters method as follows.

void cGameStub::seedCritters() 
        /* Clean out any old non-player non-wall critters. Although 
            we don't have walls yet, you might want to put some 
            in. */ 
    /* Note that I can have some switch behavior in here depending 
        on _level. */ 

    for (int i=0; i < _seedcount; i++) 
        new cCritterStubProp(this); 
    for (i=0; i<_rivalcount; i++) 
        new cCritterStubRival(this); 

The health-pack behavior of the cCritterStubProp is implemented by overriding the cCritterStubPlayer::collide method. The player controls the collision interaction since the default _collidepriority of a child of the cCritterArmedPlayer class is cCollider::CP_PLAYER = 200.0 and the default _collidepriority of a child of cCritter class is cCollider::CP_CRITTER = 100.0.

BOOL cCritterStubPlayer::collide(cCritter *pcritter) 
    BOOL collideflag = cCritter::collide(pcritter); 
        /* We can sometimes do more stuff here if collideflag is 
            TRUE. In the code below, for instance, we have the 
            player "eat" cCritterStubProp critters, that is, 
            increasing the cCritterStubPlayer health and killing the 
            cCritterStubProp when they touch. */ 
    if (collideflag && 
        setHealth(health() + 1); 
    return collideflag; 

Another feature of the 2D Game Stub game is that it uses a world that's larger than the screen. We talk about the details of how this is done in Exercise 14.2 below.

    Part I: Software Engineering and Computer Games
    Part II: Software Engineering and Computer Games Reference