0
\$\begingroup\$

so currerntly i am working on a game just uses visual studio using C# but i have a issue where the player picks up a coin but it is not added to the score any reason why this would be.

void CheckPlayerOnPickup()
    {
        foreach (Pickups pickup in pickups)
        {
            Console.SetCursorPosition(1, 30);
            Console.Write(pickup.x + " " + pickup.y + " " + playerOne.X + " " + playerOne.Y);
            if (playerOne.X == pickup.x && playerOne.Y == pickup.y)
            {
                if (pickup.mychar == 'O')
                {
                    playerOne.Score += 1;
                    
                }
            }
        }
    }

happy to supply more code if needed

\$\endgroup\$
8
  • \$\begingroup\$ When is CheckPlayerOnPickup called? Are you sure that playerOne.Score += 1; is called - that the condition is true? Why do you need to cycle through all pickups when you should know the pickup from the collision event? \$\endgroup\$
    – Zibelas
    Commented May 22, 2023 at 8:47
  • \$\begingroup\$ ive debugged it and the playerOne.score isnt being called i think thats the issue but dont know how to fix it \$\endgroup\$
    – charlie
    Commented May 22, 2023 at 8:49
  • \$\begingroup\$ If you are debugging it: you have 3 conditions before the +1 is getting called. Check what each value is and compared to what it should be. One quite likely error is the location. Your pickup position will never be exact on the same position as the player \$\endgroup\$
    – Zibelas
    Commented May 22, 2023 at 8:54
  • \$\begingroup\$ ah alright how do i go about fixing that \$\endgroup\$
    – charlie
    Commented May 22, 2023 at 8:55
  • 2
    \$\begingroup\$ Are x and y here floating point numbers, or integers? Usually with floats we'd compare them with some tolerance range. \$\endgroup\$
    – DMGregory
    Commented May 22, 2023 at 11:43

1 Answer 1

0
\$\begingroup\$

A simple way to compare position with the Pythagorean theorem:

void CheckPlayerOnPickup() {
    float pickupDistance = 1.0f;
    foreach (Pickups pickup in pickups) {
        if (DistanceFrom(playerOne.X, playerOne.Y, pickup.x, pickup.y)  < pickupDistance) {
            if (pickup.mychar == 'O') {
                playerOne.Score += 1;
            }
        }
    }
}

float DistanceFrom(float x1, float y1, float x2, float y2) {
    float a = x2 - x1;
    float b = y2 - y1;
    float distance = (float)Math.Sqrt(a * a + b * b);
    return distance;
}

Here, we calculate the distance from the player to the pickup using the Pythagorean theorem; if it is less than the pickupDistance threshold, the player can earn a point. Game engines will come with lots of functions for common operations like measuring distance, but if you aren't using a game engine, you either need to write these helper functions yourself, or find a library that includes them.

Note that this approach of comparing position using distance is usually necessary when you have a continuous coordinate system (e.g. coordinates are floats or doubles). If you have a discrete coordinate system (such as on a chessboard) where positions are represented with integers or other discrete values, you should be able to simply perform an equality check.

Put more simply:

int a = 3;
int b = 3;
Debug.WriteLine(a == b); //outputs "True"

float x = 3.0f;
float y = 3.00001f;
Debug.WriteLine(x == y); //outputs "False"
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .