'TypeError: '<' not supported between instances of 'tuple' and 'int' pygame

when i try running my code i get the TypeError: '<' not supported between instances of 'tuple' and 'int' error for opponent.center. This is pygame btw.

if opponent.center < ball.y:
    opponent.top += opponent_speed
if opponent.center > ball.y:
    opponent.bottom -= opponent_spee

but if i do opponent.top it works just fine is there a reason for this.



Solution 1:[1]

but if i do opponent.top it works just fine is there a reason for this.

Yes. opponent.top is the Y coordinate of the top edge of the opponent rectangle. You can compare that against the Y coordinate of the ball position.

opponent.center is the center position of the opponent rectangle. You can't compare a position (tuple) against a number. Hence why you're getting this error.

Suggested fix: Use opponent.center[1] instead of opponent.center as you seemingly are trying to compare Y coordinates.

Note: ball.y is the same as ball.top.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1