Skip to content
Advertisement

Pygame doesn’t let me use float for rect.move, but I need it

I’ve recently recreated a version of Lunar Lander (you know, the old retro game) in Python 3 and Pygame: my lander moves (̀̀̀rect.move) each frame along the y axis because of gravity.

Problem:
Until I hit 1 m/s, the y value added to rect.move is a float under 1: I must use int() to round it up, as pygame doesn’t like floats.
In a previous version with Tkinter, the y coord of the lander was like this:

0.01
0.02
...
0.765
1.03
1.45
...

In pygame it’s

0
0
0
...
1
1
1
2
2
...

This is really annoying, as the movement isn’t fluid. Does someone know how to solve this? Like, input a float to rect.move? Thanks in advance!

Advertisement

Answer

Since pygame.Rect is supposed to represent an area on the screen, a pygame.Rect object can only store integral data:

The coordinates for Rect objects are all integers. […]

If you want to store object positions with floating point accuracy, you have to store the location of the object in separate variables respectively attributes and to synchronize the pygame.Rect object. round the coordinates and assign it to the location (e.g. .topleft) of the rectangle:

x, y = # floating point coordinates
rect.topleft = round(x), round(y)

Thus (x, y) is the exact position and (rect.x, rect.y) contains the integers closest to this position.

See the following example. While the red object is moved by directly changing the position of the pygame.Rect object, the position of the green object is stored in separate attributes and the movement is calculated with floating point precision. You can clearly see the inaccuracy of the red object in terms of direction and speed:

import pygame

class RedObject(pygame.sprite.Sprite):
    def __init__(self, p, t):
        super().__init__()
        self.image = pygame.Surface((20, 20), pygame.SRCALPHA)
        pygame.draw.circle(self.image, "red", (10, 10), 10)
        self.rect = self.image.get_rect(center = p)
        self.move = (pygame.math.Vector2(t) - p).normalize()
    def update(self, window_rect):
        self.rect.centerx += self.move.x * 2
        self.rect.centery += self.move.y * 2
        if not window_rect.colliderect(self.rect):
            self.kill()

class GreenObject(pygame.sprite.Sprite):
    def __init__(self, p, t):
        super().__init__()
        self.image = pygame.Surface((20, 20), pygame.SRCALPHA)
        pygame.draw.circle(self.image, "green", (10, 10), 10)
        self.rect = self.image.get_rect(center = p)
        self.pos = pygame.math.Vector2(self.rect.center)
        self.move = (pygame.math.Vector2(t) - p).normalize()
    def update(self, window_rect):
        self.pos += self.move * 2
        self.rect.center = round(self.pos.x), round(self.pos.y)
        if not window_rect.colliderect(self.rect):
            self.kill()

pygame.init()
window = pygame.display.set_mode((400, 400))
clock = pygame.time.Clock()

start_pos = (200, 200)
all_sprites = pygame.sprite.Group()

run = True
while run:
    clock.tick(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.MOUSEBUTTONDOWN:
            all_sprites.add(RedObject(start_pos, event.pos))
            all_sprites.add(GreenObject(start_pos, event.pos))

    all_sprites.update(window.get_rect())

    window.fill(0)
    all_sprites.draw(window)
    pygame.draw.circle(window, "white", start_pos, 10)
    pygame.draw.line(window, "white", start_pos, pygame.mouse.get_pos())
    pygame.display.flip()

pygame.quit()
exit()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement