While working with sprites in pygame, I eventually realized (due to minimal difference) that one cannot move a sprite by a fraction of a pixel. Albeit being a logical process, I came to the conclusion that equal velocity is unattainable at different angles.
For example:
xmove = 5
ymove = -5
I define the movement increments of the two coordinates of the sprite ball that leaves a platform on the bottom of the screen traveling at a 45° angle. I wish for this direction to change relative to the obstacles that it will encounter.
The following determines the collision of the sprite with the "walls" of the screen:
if ball.rect.left<=0:
xmove = 5
elif ball.rect.right>=700:
xmove = -5
elif ball.rect.top<=0:
ymove = 5
elif ball.rect.bottom>=400:
ymove = -5
Here is some code to decide what happens after a collision with a player sprite (the platform) is detected:
if pygame.sprite.spritecollide(ball, player_sprite, False):# and ball.rect.bottom>=player.rect.top:
ymove=-5
if ball.rect.bottomright [0] in range (player.rect.topleft [0], player.rect.topleft [0] + 15):
xmove = -5
elif ball.rect.bottomleft [0] in range (player.rect.topright [0] - 14, player.rect.topright [0] + 1):
xmove = +5
elif ball.rect.bottomright [0] in range (player.rect.topleft [0] + 15, player.rect.topleft [0] + 26):
xmove = -4
ymove = -5.83
elif ball.rect.bottomleft [0] in range (player.rect.topright [0] - 26, player.rect.topright [0] - 15):
xmove = +4
ymove = -5.83
The idea here is to have the ball bounce at different angles based on where it hits the platform, all while keeping the same velocity (distance traveled per tick)
So the problem here is that to compensate the decrease of one pixel on the x coordinate I use the Pythagorean theorem to output the y coordinate (-5.83). Being that pygame doesn't register fractions of a pixel, this will actually decrease the velocity of the ball significantly.
So my question is, how do I get around this (as I don't want to be limited to 45° angles...)?