RPG random position problem...
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
|
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int main()
{
int x = 5, y = 4, x2, y2;
int height = 15, width = 20;
int health = 20;
char input;
srand ( time(NULL));
x2 = rand() % 15 + 1;
y2 = rand() % 20 + 1;
do
{
for(int r = 0; r < height; ++r)
{
for(int c = 0; c < width; ++c)
{
if(r == y && c == x)
{
cout << (char)1;
}
else if(r == y2 && c == x2)
{
cout << (char)3;
}
else if(r == y && r == y2 && c == x && c == x2)
{
health += 5;
srand ( time(NULL));
x2 = rand () % 15 + 1;
y2 = rand () % 20 + 1;
}
else
{
cout << '.';
}
}
cout << endl;
}
cout << endl;
cout << "Health:" << health;
input = getch();
switch(input)
{
case 'w': y--; break;
case 'a': x--; break;
case 's': y++; break;
case 'd': x++; break;
}
cout << string( 100, '\n');
}
while(input != 'q');
return 0;
}
|
Why:
The Health never changes?
And why the heart dosen't change it's position?
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
|
#include <iostream>
#include <conio.h>
#include <cstdlib>
#include <time.h>
#include <string>
using namespace std;
int main()
{
int x = 5, y = 4, x2, y2;
int height = 15, width = 20;
int health = 20;
char input;
srand ( time(NULL));
x2 = rand() % 15 + 1;
y2 = rand() % 20 + 1;
do
{
system("CLS"); // This will clear the screen each step
for(int r = 0; r < height; ++r)
{
for(int c = 0; c < width; ++c)
{
if(r == y && c == x)
{
cout << (char)1;
}
else if(r == y2 && c == x2)
{
cout << (char)3;
}
else if (x==x2 && y==y2)// <-- changed line
{
health += 5;
srand ( time(NULL));
x2 = rand () % 15 + 1;
y2 = rand () % 20 + 1;
}
else
{
cout << '.';
}
}
cout << endl;
}
cout << endl;
cout << "Health:" << health;
input = getch();
switch(input)
{
case 'w': y--; break;
case 'a': x--; break;
case 's': y++; break;
case 'd': x++; break;
}
cout << string( 100, '\n');
}
while(input != 'q');
return 0;
}
|
You should check the collision between the face and the heart to increase health and then change the heart position
Last edited on
Thanks!
Topic archived. No new replies allowed.