No, not because the questions are stupid or there are too many, it's that I have given you the answer in previous posts and you just rephrase the question in a new post.
One of the lessons in the tutorials goes over how to turn the reticle red in a specific region also, so if you are following along, you should have been able to figure it out pretty easily.
Given that your last post has finally gotten rid of the boolean flags, something I had suggested on a previous post, also
Code: Select all
//Target enemy1x & enemy1y
if (chx >= enemy1x - 55 && chx <= enemy1x - 51 || chy >= enemy1y - 3 && chy <= enemy1y + 5)
{
chg = 0;
chb = 0;
chr = 255;
}
this is pretty close to what I've suggested in a previous post, though I think the || should have been && in this if statement. With the logical OR "||", your cross hair should turn red if the reticle is anywhere along the Y axis if between enemy1x - 55 and enemy1x - 51, which I'm guessing is why you have the two if statements below that.
If you'd taken my suggestion from the earlier post or followed the tutorials, you could avoid this by just checking of the cross hair is between the left side and right side of the graphic bounds as well as the top and bottom of the graphic bounds.
Code: Select all
int left = enemy1x - 55;
int top = enemy1y - 3;
int right = enemy1x - 51;
int bottom = enemy1y + 5;
if( ( chx >= left && chx <= right ) &&
( chy >= top && chy <= bottom ) )
{
chr = 255;
chg = 0;
chb = 0;
}
else
{
chr = 255;
chg = 255;
chb = 255;
}
You could also make functions to make this algorithm reusable.
Code: Select all
bool IsBetween( int test, int low, int high )
{
return ( test >= low && test <= high );
}
bool IsInsideRegion( int X, int Y, int Left, int Top, int Right, int Bottom )
{
return IsBetween( X, Left, Right ) && IsBetween( Y, Top, Bottom );
}
Now, your game code could be:
Code: Select all
if( IsInsideRegion( chx, chy, enemy1x - 55, enemy1y - 3, enemy1x - 51, enemy1y + 5 ) )
{
chr = 255;
chg = 0;
chb = 0;
}
else
{
chr = 255;
chg = 255;
chb = 255;
}
Soon you'll learn about structures and classes. This will help you put pieces together to create a full object like an Enemy or CrossHair object.
Code: Select all
struct CrossHair
{
int x, y;
Color color;
};
struct Enemy
{
int x, y, width, height;
Color color;
};
struct Rect
{
int left, top, right, bottom;
};
bool IsInsideRect( int X, int Y, Rect rect )
{
return IsBetween( X, rect.left, rect.right ) && IsBetween( Y, rect.top, rect.bottom );
}
bool IsEnemyTargeted( CrossHair const& ch, Enemy const& enemy )
{
Rect enemy_rect;
enemy_rect.left = enemy.x;
enemy_rect.top = enemy.y;
enemy_rect.right = enemy.x + enemy.width;
enemy_rect.bottom = enemy.y + enemy.height;
return IsInsideRect( ch.x, ch.y, enemy_rect )
}
Enemy enemy1, enemy2, enemy3;
CrossHair ch;
if( IsEnemyTargeted( ch, enemy1 ) || IsEnemyTargeted( ch, enemy2 ) || IsEnemyTargeted( ch, enemy3 ) )
{
ch.color = Colors::Red;
}
else
{
ch.color = Colors::White;
}
You'll also learn about arrays, which makes it possible to use loops to iterate through your enemies.
Code: Select all
Enemy enemies[3];
bool make_red = false;
for( Enemy const& enemy : enemies )
{
// Can't just do the if statement one at a time since the cross hair is most likely
// only going to cover one enemy at a time. If that heppens, it would immediately
// make the cross hair white again, so we store the value using an OR which builds
// up a true if ANY of them are true.
make_red = IsEnemyTargeted( ch, enemy ) || make_red;
}
if( make_red )
{
ch.color = Colors::Red;
}
else
{
ch.color = Colors::White;
}
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com