[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Suggestion: make Rect use float coordinates



On Fri, 15 Apr 2005, Tobias Pfeiffer wrote:
> Tomi Kyöstilä schrieb:
> > I made a tetris block which has a rect and a speed variable. On every
> > step I do rect.move_ip(0, speed), but I noticed that if the speed is 1,
> What is the use of using 0.x if the screen cannot display half pixels or
> the like? Try to put a time.sleep() call before every move, so you can
> rather control the speed, I think?

but if you have n things moving at different speeds,
or something in the game logic related to speed,
that wouldn't give the desired effect

i mean, if you e.g. have things A and B moving across the screen from 
left to right, with different speeds, it is kinda nice to do something 
like: (this all being totally untested pseudocode stuff)

class Mover:
    def __init__(self, speed):
        self.speed = speed

    def update(self):
        self.pos += speed

a = Mover(2)
b = Mover(2.5)
movers = [a,b]

while 1:
    [m.update() for m in movers]
    
.. the supposed coordinate system being pixels as in SDL, so that A would 
move 2 pixels every turn, but B would move 3 on every other

i'm not sure tho if the conversion from positioning floats to pixel ints 
should be done in pygame, or if it should be left for the application 
programmer to do, as it is currently .. pygame being a low level library, 
Rect.x & y are about pixels, not positions of things in the gameworld, or?
at least in systems with scrolling, and also to get screenres 
independence, a separate coordinate system is needed anyhow (like in some 
tools we use that the position is a float from 0 to 1)

one thing that came to mind back then on irc when Tomi mentioned that was 
a Rect subclass that would handle that, something like:

FloatRect(pygame.Rect):
    def set_x(self, x):
        self._x = x #accepts float, no flooring
    def get_x(self):
        return round(self._x) #or just int(), whatever is desired
    x = property(get_x, set_x)

which perhaps would give the effect he was supposing from pygame 
initially ('ve no clue about the performance tho)

> Tobias

~Toni