Ending a game

I followed the 3DBuzz tutorials to make a game, and want to make it to that if the character is dead or all the enemies are dead the game ends.

So if isAlive, under sprite.h, us false - but only for character, or only for the enemies- then the game ends. I think I would add this onto the while at line 41 of game.ccp but I don't know exactly how. Please help!

There are lots of files in the game, but I think only the game.h, game.ccp, sprite.h, Sprite.ccp, character.h, and character.ccp are needed.

I hope this is enough, or not so much that you are scared away and don't read this. Thank you very much!

game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "character.h"
#include "mage.h"

class Game
{
public:
    bool run(void);
private:
    Level *level;
    Mage *player;

    double frameCount;
    double startTime;
    double lastTime;

    int posx;

    DrawEngine drawArea;

protected:
    bool getInput(char *c);
    void timerUpdate(void);
};

#endif // GAME_H_INCLUDED  


game.ccp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "game.h"
#include "drawEngine.h"
#include "sprite.h"

#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace std;

const int GAME_SPEED = (1000/30);

bool Game::run(void)
{
    drawArea.createBackgroundTile(TILE_EMPTY, ' '); 
    drawArea.createBackgroundTile(TILE_WALL, 219); 



    drawArea.createSprite(SPRITE_ENEMY, '$');
    drawArea.createSprite(SPRITE_PLAYER, 1); 
    drawArea.createSprite(SPRITE_FIREBALL, '*');

    level = new Level(&drawArea, 30, 20);
    player = new Mage(level, &drawArea, 0);

    level->draw(); 
    level->addPlayer(player);
    level->addEnemies(3);

    char key = ' ';

    startTime = timeGetTime();
    frameCount = 0;
    lastTime = 0;


    posx = 0;

    player->move(0,0); //starts player off at 0,0
    while ( key != 'q')


        {
            while (!getInput(&key))
            {
                timerUpdate();
            }

            level->keyPress(key);

        }
        delete player;

        return true;
}

bool Game::getInput(char *c)
{
    if (kbhit())
    {
        *c = getch();
        return true;
    }

   return false;
}

void Game::timerUpdate(void)
{
    int currentTime = timeGetTime() - lastTime;

    if (currentTime < GAME_SPEED)
        return;

    level->update();

    frameCount ++;
    lastTime = timeGetTime();
}


sprite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#ifndef SPRITE_H_INCLUDED
#define SPRITE_H_INCLUDED

#include "drawEngine.h"
#include "level.h"

enum
{
    SPRITE_CLASSID,
    CHARACTER_CLASSID,
    ENEMY_CLASSID,
    FIREBALL_CLASSID,
    MAGE_CLASSID
};

struct vector
{
    float x;
    float y;
};

class Sprite
{
public:
    Sprite(Level *l, DrawEngine *de, int s_index, float  x = 1, float y = 1, int i_lives = 1);
    ~Sprite();

    vector getPosition(void);
    float getX(void);
    float getY(void);

    virtual void addLives(int num = 1);
    int getLives(void);
    bool isAlive(void);

    virtual void idleUpdate(void);

    virtual bool move(float x, float y);


    int classID;

protected:
    Level *level;

    DrawEngine *drawArea;
    vector pos;
    int spriteIndex;
    int numLives;

    vector facingDirection;

    void draw(float x, float y);
    void erase(float x, float y);

    bool isValidLevelMove(int xpos, int ypos);
};


#endif // SPRITE_H_INCLUDED  


Sprite.ccp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "sprite.h"

Sprite::Sprite(Level *l, DrawEngine *de, int s_index, float x, float y, int i_lives)
{
    drawArea = de;

    pos.x = x;
    pos.y = y;

    spriteIndex = s_index;

    numLives = i_lives;

    facingDirection.x = 1;
    facingDirection.y = 0;

    classID = SPRITE_CLASSID;

    level = l;
}

Sprite::~Sprite()
{
    //erase the dieing sprite
    erase(pos.x, pos.y);
}

vector Sprite::getPosition(void)
{
    return pos;
}

float Sprite::getX(void)
{
    return pos.x;
}

float Sprite::getY(void)
{
    return pos.y;
}

void Sprite::addLives(int num)
{
    numLives += num;
}
int Sprite::getLives(void)
{
    return numLives;
}
bool Sprite::isAlive(void)
{
    return (numLives > 0);
}

bool Sprite::move(float x, float y)
{
    int xpos = (int)(pos.x + x);
    int ypos = (int)(pos.y + y);

    if (isValidLevelMove(xpos, ypos))
    {
    //erase sprite
    erase(pos.x, pos.y);

    pos.x += x;
    pos.y += y;

    facingDirection.x = x;
    facingDirection.y = y;

    //draw sprite
    draw(pos.x, pos.y);

    return true;
    }

    return false;
}
    void Sprite::draw(float x, float y)
    {
        drawArea ->drawSprite(spriteIndex, (int)x, (int)y);
    }
    void Sprite::erase(float x, float y)
    {
        drawArea ->eraseSprite((int)x, (int)y);
    }

bool Sprite::isValidLevelMove(int xpos, int ypos)
{
    if (level->level[xpos][ypos] != TILE_WALL) //level->level accesses protected data so in level.h public I made
        return true;                           //sprite a friend 'friend' class Sprite

    return false;
}

void Sprite::idleUpdate(void)
{
    //this is for the inherited classes
}


character.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED

#include "sprite.h"

class Character : public Sprite
{
public:
    Character(Level *l, DrawEngine *de, int s_index, float x = 1, float y = 1,
              int lives = 3, char up_key = 'w', char down_key = 's', char left_key = 'a', char right_key = 'd');

    virtual bool keyPress(char c);

    virtual void addLives(int num = 1);

protected:
    char up_key;
    char down_key;
    char right_key;
    char left_key;
};


#endif // CHARACTER_H_INCLUDED  


character.ccp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include "character.h"

Character::Character(Level *lvl, DrawEngine *de, int s_index, float x, float y, int lives,
                     char u, char d, char l, char r)
                     : Sprite(lvl, de, s_index, x, y, lives)
{
    up_key = u;
    down_key = d;
    left_key = l;
    right_key = r;

    classID = CHARACTER_CLASSID;
}

bool Character::keyPress(char c)
{
    if (c == up_key)
    {
        return move(0, -1);
    }
    else if (c == down_key)
    {
        return move(0, 1);
    }
    else if ( c == right_key)
    {
        return move(1, 0);
    }
    else if (c == left_key)
    {
        return move(-1, 0);
    }

    return false;
}

void Character::addLives(int num)
{
    Sprite::addLives(num);

    if (Sprite::isAlive())
    {
        pos.x = 1;
        pos.y = 1;
        move(0,0);
    }
}
Did You Define Player?
Topic archived. No new replies allowed.