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

[pygame] Float precision



Hi all, I've been trying to rotate and move forward a tank in a game I'm developing but something strange happens.
When the tank is rotating and moving it tends to move to the up-left corner and I don't know why.

Anyone knows what's happen?

Thank you all!

Here is the code:



    def do_actions(self):
        all_keys = pygame.key.get_pressed()
        if all_keys[K_w]:
            self.dir = +1
            self.tank_move()
        elif all_keys[K_s]:
            self.dir = -1
            self.tank_move()
        if all_keys[K_a]:
            self.rot_dir = +1
            self.tank_rotate()
        elif all_keys[K_d]:
            self.rot_dir = -1
            self.tank_rotate()
   
    def tank_move(self):
        dx = sin(self.angle * pi/180) * self.dir * self.speed
        dy = cos(self.angle *pi/180) * self.dir * self.speed
        add_pos = (self.dir * self.speed * dx, self.dir * self.speed * dy)
        self.rect.centerx += dx
        self.rect.centery += dy
   
   
   
    def tank_rotate (self):
        self.angle = self.angle + self.vel_rot * self.rot_dir
        aux_center = self.rect.center
        self.image = pygame.transform.rotate(self.copy, self.angle)
        self.rect = self.image.get_rect()
        self.rect.center = aux_center
        self.rot_dir = 0