Rect does not support floating point coordinates

Issue #12 open
Daniel Pope
repo owner created an issue

This is confusing:

>>> from pygame import Rect
>>> Rect(1.5, 2.6, 7, 0.1)
<rect(1, 2, 7, 0)>

It is inconsistent with Python's own number types, because Python tries to minimise the distinction between integral and float types.

Comments (6)

  1. Tim Golden

    Hmmm. I'm not clear how to proceed on this: we can easily use the Rect subclass I've just created for Issue #11 but we'd have to reimplement every operation to allow for underlying floats, and at some point we'll have to pass the rect to, eg, screen.blit, at which point pygame will presumably complain because the numbers are floats.

    Or are you simply suggesting that we store (and display) floats but that all the properties (x, y, topleft etc.) return integers as at present?

  2. Daniel Pope reporter

    I was thinking the Rect class could store and return floats and we round them to integers to pass to Pygame's APIs. Either that or pursue a fix in Pygame itself.

  3. Tim Golden

    Seems to the me that the fix in pygame would be too far-reaching and possibly break long-standing assumptions. At best you might get away with a FloatingRect class (or whatever). However, I'm not a pygame dev so my intuition might well be off here.

    I'll try sketching out a pgzero on the back of Issue #11 which holds floats and see what it looks like.

  4. first last

    My guess is probably the solution might be to implement own wrapper which does all the stuff with floats but rounds the coordinates to integers when being used from PyGame point of view.

  5. Daniel Pope reporter

    The ZRect class has been implemented to fix this. @Tim Golden and I had a chat offline about the implications of bringing this in and the compatibility problems of doing so. We decided to hold off for the moment. ZRect is available if you need it, but it is clearly different to the default Rect objects in Pygame and Pygame Zero.

  6. Log in to comment