Attack + health system!

Pages: 12
just move the attack = rand() % 6 inside of the while loop

EDIT:
lines 12 and 27 are your attack functions put it at like like 31 or so inside the while loop (while they are alive it keeps repeating the loop and you need to keep getting a new random attack value)
Last edited on
here's an example:
1
2
3
4
5
6
7
8
9
10
11
unsigned int random1, random2, times = 0; //you don't need unsigned I just like to use because I know it will never be a negative number.
//define two new variables random 1 and random 2 and then define times and set it to 0
srand(time(NULL));
while(times > 5)
{
     random1 = rand() % 6; //give random1 anew value
     random2 = rand() % 6; //give random 2 a new value
     std::cout << "random 2: " << random1 << std::endl;
     std::cout << "random 2: " << random2 << std::endl;
     times++; //increment by 1
}
Thank you all! I solved all of my questions with your help! I have learned many new commands!
I'm gonna post all of my code one more time to see if there are any ways to clean it up/improve it...

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
//  My First Game

#include <iostream>
#include <windows.h>
#include <cstdlib>
#include <cstdio>
#include <ctime>

int main(int argc, char** argv)
{  //  beggining
    using std::cout;
    using std::cin;
    using std::endl;
    srand( time( NULL ) );
    
    cout  << "Welcome to you life!\n";
    std::string userName;
    cout << "Please enter your name- ";
    cin >> userName;
    cout << "\n";
    cout << "Your journey begins " << userName << "!\n";
    system("pause");
    cout << "\n";
    
    cout << "You are an average human.\n";
    system("pause");
    cout << "\n";
    
    cout << "You have never been on an adventure before.\n";
    system("pause");
    cout << "\n";
    
    cout << "You did not even plan to ever go on an adventure.\n";
    system("pause"); 
    cout << "\n";
    
    cout << "As you step out of your door for the first time in your life...";
    Sleep(5000);
    
    
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    Sleep(50);
    system("color 7d");  //  white
    system("color 4c");  //  red
    
    system("color 07");  //  white
    std::cout << "...a sword falls from above!\n";
    cout << "\n";
    
    cout << "     0 \n";  //  sword picture
    cout << "     | \n";
    cout << "    -O-\n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    cout << "     | \n";
    system("pause");
    cout << "\n";
    
    
    cout << "As you pick up the sword...\n";
    system("pause");
    cout << "\n";
    
    cout << "You notice a monster out side of your house...\n";
    system("pause");
    cout << "\n";
    
    
    int health;  //  player health
    health = 20;
    int attack;  //  player attack
    attack = (rand() % 6);
    
    int kills;
    
    struct Enemy  //  enemy stats
    {
    int health;
    int attack;
    };  
    
    
    cout << "You approach a slime...\n";
    system("pause");
    cout << "\n";
    
    
    Enemy slime;  //  enemy slime stats
    slime.health = (rand() % 6 ) + 5;  
    slime.attack = (rand() % 6);  
    
    
    while(health > 0 && slime.health > 0) //  || means logical or
    {  //  battle sequence
    attack = (rand() % 6);
    slime.attack = (rand() % 6);
    cout << "You have " << health << " health\n";
    system("pause");
    cout << "You attack it...\n";
    system("pause");
    slime.health = slime.health - attack;
    cout << "The slime has " << slime.health << " health\n";
    cout << "The slime attacks...\n";
    health = health - slime.attack;
    }  //  end battle sequence

    if ( health <= 0 )  //  lose
    {
    cout << "You died...\n";
    cout << "You killed " << kills << " before you died";
    system("pause");
    return 0;
    }  //  end

    if ( slime.health <= 0 )  //  win
    {
    cout << "You defeated the slime!\n";
    cout << "You continue on your journey.\n";
    cout << "You have killed " << kills << " creatures";
    cout << "\n";
    }  //  end


    system("pause");
    return 0;
}  //  end
    
I can think of one. inside of your sturct you could put the players info also maybe unless you only want that for the monsters
eg
player.health
slime.health;
player.attack;
slime.attack;
up to you though

and I don't think you really need the (int argc, char** argv) on your main function because you aren't even using those variable. maybe try
int main( void )

and I wouldn't suggest using system either.
for pausing you could do something like
1
2
#include <conio.h>
getch();

(there are other methods also by std::cin.get(), ignore, clear)

and for coloring text once again I wouldn't suggest system("color");
try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
    void textColor( const unsigned int color )
    {
        HANDLE outHandle = GetStdHandle( STD_OUTPUT_HANDLE );
        SetConsoleTextAttribute( outHandle , color );
    }
int main( void )
{
    enum
    {
        black , blue , green , cyan , red , magenta , brown , normal , darkgrey , lightblue , lightgreen , lightcyan , lightred , lightmagenta , yellow , white
    };

     textColor( blue );
     std::cout << "Hello world! I am blue!" << std::endl;
     textColor( red );
     std::cout << "Hello world! I am red!" << std::endl;
     textColor( normal );
     std::cout << "Hello world! I am normal!" << std::endl;
     for(unsigned int i = 0; i < 256; i++)
     {
           textColor( i );
           std::cout << "Color " << i << std::endl;
     }
}


enum just turns the "words" into numbers and I didn't set any of them so they are the default values starting from 0 working up to 14 since there are 15 of them.
Last edited on
Thank you! All of those were helpfull.
I was gonna change the background color...
Topic archived. No new replies allowed.
Pages: 12