0

Im using an if statement to detect whether the player's coordinates after moving up (y coordinate increases) are equal to an open space's coordinates.

Ex:

player_coordinate = player.pos()  # We can say that the coordinate returned is (-10.00,10.00)
space_coordinate = space.pos()  # And the coordinate returned is (-10.00,20.00)
movement = 10  # How far the player can move at once

if (player_coordinate[0], player_coordinate[1] + movement) == space_coordinate:
    player can move

Now, I used this same method, however when the player's position has a negative y value, the statement is false.

For example:

# Works:
if (-90.0, 30.0) == (-90.00,30.00)

# Doesn't Work
if (-90.0, -10.0) == (-90.00,-10.00)

(By the way the first tuple uses the vales stated previously, player_coordinate[0], player_coordinate[1] + movement, so i have no clue why it returns with one decimal place instead of two like in the original .pos() tuple but it shouldn't matter because the problem only occurs when the y-value is negative)

It looks like it is saying that -10.0 is not equal to -10.00. Any ideas on why this might not be working?

Here is the actual code too if it helps. I use 'in' because Im storing all of the space coordinates in a dictionary:

def player_movement(player, total, direction):
    current = player.pos()
    if direction == 'up':
        if (current[0], current[1] + total) in space_coordinates.values():
            player.shape('Player_Up.gif')  # Changes player sprite
            player.goto(current[0], current[1] + total)  # Moves player

I already checked if the coordinate I needed was in the dictionary and it was

Bluffyyy
  • 93
  • 5
  • Probably a case of [floating point weirdness](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). Did you try comparing them with an epsilon/delta/tolerance factor? This isn't really a [mcve] so it's hard to post a working answer. `space_coordinates.values()` looks like a misuse of a dictionary, which are usually accessed by key in O(1), usually strings like `'up'` should be replaced by tuples or enums and functions should usually be verbs, not nouns. – ggorlen Dec 18 '22 at 00:04

1 Answers1

0

Turtles wander a floating point plane so exact comparisons are to be avoided. Instead of asking:

turtle_1.position() == turtle_2.position()

you should consider asking:

turtle_1.distance(turtle_2) < 10

That is, are they in close proximity to each other, not on the exact same coordinate.

cdlane
  • 40,441
  • 5
  • 32
  • 81