How i can set if healt 0 game end?

How i can set if healt 0 game end?

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <Windows.h>
#include <iostream>
#include <BluetoothAPIs.h>
#include <ws2bth.h>



bool Move(int xadj, int yadj);  



void LoadMap();                 
void DrawScreen();              

using namespace std;
    
    




char Map[10][20] =      
{
    "###################",
    "#        *      ^^#",
    "#  #####  #########",
    "#  # Y # *#  ^    #",
    "# *# E #  # ##### #",
    "#  # A #* # # ! # #",
    "#* # H #  #     # #",
    "#  # ! #  ####### #",
    "#@ # ! #          #",
    "###################"
};

int Gamespeed = 100;
int Level = 1;
bool stopgame = false;
int Hp = 100;
int MaxHp = 100;
int Score = 0;
int PlayerX = 0;
int PlayerY = 0;


const char Tile_Wall  = (char)219;
const char Tile_Floor = ' ';
const char Tile_Goal  = '!';
const char Tile_Enemy = '*';    
const char Tile_Bonus = '^';    

                     
                                
int main()
{
    LoadMap();      
    DrawScreen();   

    while(stopgame == false && Level == 1)      
    {
       
        
        bool playermoved = false;

             if(GetAsyncKeyState(VK_UP   ) & 0x8000)  playermoved = Move( 0,-1);
        else if(GetAsyncKeyState(VK_DOWN ) & 0x8000)  playermoved = Move( 0, 1);
        else if(GetAsyncKeyState(VK_LEFT ) & 0x8000)  playermoved = Move(-1, 0);
        else if(GetAsyncKeyState(VK_RIGHT) & 0x8000)  playermoved = Move( 1, 0);
		
        
        if(playermoved)
            DrawScreen();

        
        Sleep(Gamespeed);
    }

    while (stopgame == false && Level == 2)
    {
        system("cls");
        cout << "You Win!!!\n\n";
        cout << "Your score: " << Score << "\n\n";
        cout << "© XDmanne games\n\n";
        system("pause");
        return EXIT_SUCCESS;
    }

    return 0;
}

bool Move(int xadj, int yadj)
{
    
    int x = PlayerX + xadj;
    int y = PlayerY + yadj;

   
    bool canmove = true;      

    switch(Map[y][x])
    {
    case Tile_Wall:     canmove = false;        break;  
    case Tile_Floor:                            break;  
    case Tile_Goal:     ++Level;                break;  
    case Tile_Enemy:    Hp -= 20;               break;  
    case Tile_Bonus:    Score += 20;            break;  
    }

    
    if(canmove)
    {
        
        PlayerX = x;
        PlayerY = y;

        
        
        Map[y][x] = Tile_Floor;
    }

   
    return canmove;
}

void LoadMap()
{
    
    

    for(int y = 0; y < 10; ++y)
    {
        for(int x = 0; x < 20; ++x)
        {
            switch(Map[y][x])
            {
            case '#':                       
                Map[y][x] = Tile_Wall;      
                break;

            case '@':                       
                Map[y][x] = Tile_Floor;     
                PlayerX = x;                
                PlayerY = y;
                break;
            }
        }
    }
}


void DrawScreen()
{
    
    system("cls");
    
    

    Map[PlayerY][PlayerX] = '@';

    
    for (int y = 0; y < 10; y++)
    {
        cout << Map[y] << '\n';     
    }


    Map[PlayerY][PlayerX] = Tile_Floor;    
    
    
    cout << "Hp: " << Hp << "/" << MaxHp << '\n';      
    cout << "Score: " << Score << '\n';
    cout << "@=You" << '\n';
    cout << "*=Enemy" << '\n';
    cout << "!=Goal" << endl;       
}
Well you have a game loop. Just check for health each iteration.

Also, I would recommend putting those variables into a struct to avoid global variables like this. It instantly becomes more readable and flexible in doing so.
I would place this within the first while loop

1
2
if(Hp < 1)
	Level = 2;
so where i put it, i'm noob :DDD



Topic archived. No new replies allowed.