How to get the reticle color to change to red when hovering over a certain pixel?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
TheCollector
Posts: 41
Joined: August 1st, 2019, 9:57 pm

How to get the reticle color to change to red when hovering over a certain pixel?

Post by TheCollector » August 3rd, 2019, 3:13 am

I tried this with (but doesn't work bc I don't know what I'm doing =])
Enemy1x is a stick figure looking dude for which I did not code the color change for y coordinates yet bc it didn't work for the x.
I would like the reticle to turn red when over the enemy. I probably don't need all these else statements bc EnemyTargeted is already set to false. Red in the gfx for the reticle is chr and g and b are chgb:

//Color change for reticle
if (EnemyTargeted)
{
chgb = 0;
chr = 250;
}

//Enemy1x targeted
if (chx == 52)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 49)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 50)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 51)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx== 52)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 53)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 54)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
if (chx == 55)
{
EnemyTargeted = true;
}
else
{
EnemyTargeted = false;
}
Last edited by TheCollector on August 3rd, 2019, 3:53 pm, edited 1 time in total.

albinopapa
Posts: 3790
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: How to get the reticle color to change to red when hovering over a certain pixel?

Post by albinopapa » August 3rd, 2019, 9:04 am

Try to think about what you want to accomplish and think through it logically.

Let's say you want the reticle to turn red if it is in a bounding box around some object.
Let's say this imaginary bounding box's top left corner is at ( 390, 290 ) and it's bottom right corner is at ( 410, 310 )

int left = 390, top = 290, right = 410, bottom = 310;

Now, you want the reticle to be red if the cross hair is greater than or equal to ( left, top ) and less than ( right, bottom ). How would that look as a set of if statements?

With what you have, if X == 49, set target, that parts fine, then if X != 50 set target as false, so each frame it is checking if X is at 49 ( yes set target enabled ), X is 50 ( no, set target disabled ) and so on, so it never stays.

Not sure at what point chili goes over 'else if' and the different types of logical operations like ( && ) for logical AND, ( || ) for logical OR and ( ! ) for logical NOT.

So for instance:
int a = 1;
int b = 1;
bool and_test_0 = ( a == 1 && b == 1 );
bool and_test_1 = ( a == 1 && b == 0 );
bool and_test_2 = ( a == 0 && b == 1 );
bool and_test_3 = ( a == 0 && b == 0 );
Try working out the results. When you AND two trues together, ( if this AND that are true ) the result should be true. If either case if false or both are false, then the result is false.
Spoiler:
and_test_0 = true;
and_test_1 = false;
and_test_2 = false;
and_test_3 = false;
bool or_test_0 = ( a == 1 || b == 1 );
bool or_test_1 = ( a == 1 || b == 0 );
bool or_test_2 = ( a == 0 || b == 1 );
bool or_test_3 = ( a == 0 || b == 0 );
For OR, it's like saying ( if this is or that is true ). So, if either side is true, then the result is true.
Spoiler:
or_test_0 = true;
or_test_1 = true;
or_test_2 = true;
or_test_3 = false; // false because neither is true
bool not_test_0 = ( a != 0 );
bool not_test_1 = ( a != 1 );
The logical NOT means, take the result and flip it. ( if NOT true, or if NOT false )
Spoiler:
not_test_0 = true; // True because 'a' is equal to zero returning false, then gets flipped to true
not_test_1 = false; // False because 'a' is equal to zero returning true, then gets flipped to false
Another way of writing it would be
bool not_test_0 = !( a == 0 );
bool not_test_1 = !( a == 1 );
So here, is 'a' equal to zero? NO, so the result of == is false. Then the NOT operator flips it to true.
Is 'a' equal to one? YES, so the result of == is true. Then the NOT operator flips it to false
You don't have to use flags for everything. For instance, instead of setting a flag that the enemy has been targeted, just change the color of the cross hairs. Do any further processing inside the selected if statement and move on. The only reason to have a flag in this scenario would be to pass along that information to a function that needs to know that the enemy has been targeted. For instance, if you have a firing mechanism and it should NOT allow you to fire a shot unless it's targeted something, then you might need a flag. Just changing state like changing colors and such shouldn't have to be so painful. The reason I bring this up is the more flags you have that determine state or behavior the more complicated things become because of all the if/else if/else statements you'll need to keep track of.

if player_presses_up move_forward()
---if player_presses_shift and player_has_weapon && weapon_is_equipped && weapon_has_ammo && enemy_is_targeted
else if player_presses_down move_backward()
---if player_presses_shift and player_has_weapon && weapon_is_equipped && weapon_has_ammo && enemy_is_targeted
if player_presses_left strafe_left()
---if player_presses_shift and player_has_weapon && weapon_is_equipped && weapon_has_ammo && enemy_is_targeted
else if player_presses_right strafe_right()
---if player_presses_shift and player_has_weapon && weapon_is_equipped && weapon_has_ammo && enemy_is_targeted
---else if player_presses_control && player_has_shield && shield_has_energy raise_shield()

For now I suppose it's unavoidable in some cases, however, try figuring out ways to avoid so many flags and just let the flow of the program take shape. Eventually, you'll learn some really neat ways to deal with state using enums and inheritance through polymorphism. I don't remember how much further down the road that is to be honest, perhaps toward the end of the beginner series or around the beginning~ish of the intermediate series.


NOTE: By the way, you can surround your code using the code tags
[ code ] // some code [ /code ]
Just remove the spaces between the brackets around code and around /code.

Or just highlight the code and press the button marked </>, it will automatically put the tags at the beginning and end of your code for you.
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

Post Reply