Python Forum
[PyGame] Sprite image.get_rect() moves sprite to 0, 0
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Sprite image.get_rect() moves sprite to 0, 0
#1
In the update section I have down below, I have this:
self.image = pygame.transform.rotate(self.ship_img, self.dir)
print("before get_rect", self.rect.x, self.rect.y)
self.rect = self.image.get_rect()
print("after get_rect", self.rect.x, self.rect.y)
the output is this:
Hello from the pygame community. https://www.pygame.org/contribute.html
init 293 575
cleaning up Menu state stuff
update top 293 575
before get_rect 293 575
after get_rect 0 0
update top 0 0
before get_rect 0 0
after get_rect 0 0
update top 0 0
I've never faced this before. I want to .get_rect() so the sprite doesn't wiggle when it rotates. Why does this happen? Wall

class Ship(pygame.sprite.Sprite):
    def __init__(self, x, y):
        
        pygame.sprite.Sprite.__init__(self)
        self.ship_img = pygame.Surface((15,10), pygame.SRCALPHA)
        self.ship_img.fill(red)
        self.image = self.ship_img
        self.rect = self.image.get_rect()
        
        self.hit_rect = ship_HIT_RECT
        self.hit_rect.center = self.rect.center
        self.rect.centerx = x
        self.rect.centery = y
        print("init", self.rect.x, self.rect.y)
        self.hspeed = 0
        self.vspeed = 0
        self.dir = 0
        self.rtspd = 0
        self.thrust = False   
        
        
    def update(self):
        print("update top", self.rect.x, self.rect.y)
        speed = math.sqrt(self.hspeed**2 + self.vspeed**2)
        if self.thrust:
            
            if speed + fd_fric < ship_max_speed:
                self.hspeed += fd_fric * math.cos(self.dir * math.pi / 180)
                self.vspeed += fd_fric * math.sin(self.dir * math.pi / 180)
            else:
                self.hspeed = ship_max_speed * math.cos(self.dir * math.pi / 180)
                self.vspeed = ship_max_speed * math.sin(self.dir * math.pi / 180)
        else:
            if speed - bd_fric > 0:
                change_in_hspeed = (bd_fric * math.cos(self.vspeed / self.hspeed))
                change_in_vspeed = (bd_fric * math.sin(self.vspeed / self.hspeed))
                if self.hspeed != 0:
                    if change_in_hspeed / abs(change_in_hspeed) == self.hspeed / abs(self.hspeed):
                        self.hspeed -= change_in_hspeed
                    else:
                        self.hspeed += change_in_hspeed
                if self.vspeed != 0:
                    if change_in_vspeed / abs(change_in_vspeed) == self.vspeed / abs(self.vspeed):
                        self.vspeed -= change_in_vspeed
                    else:
                        self.vspeed += change_in_vspeed
            else:
                self.hspeed = 0
                self.vspeed = 0
        
        self.rect.centerx += self.hspeed
        self.rect.centery += self.vspeed
        self.dir += self.rtspd
        self.image = pygame.transform.rotate(self.ship_img, self.dir)
        print("before get_rect", self.rect.x, self.rect.y)
        self.rect = self.image.get_rect()
        print("after get_rect", self.rect.x, self.rect.y)
        
Reply
#2
Any time you get rect from an image. It will always be Rect(0, 0, width, height).
Just use rect.center before you update it.
self.rect = self.image.get_rect(center=self.rect.center)
99 percent of computer problems exists between chair and keyboard.
Reply
#3
Thanks, got it. It seems broken in 10 other ways, but at least my sprite moves now! Moves all wrong, but it moves lol.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 1,979 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] Pygame is treating blob_group as a surface, when I need it to treat it as a Sprite. Swagford 1 1,301 Jan-24-2023, 09:58 PM
Last Post: metulburr
  [PyGame] Sprite image rotation problem XavierPlatinum 4 2,303 Jul-25-2022, 01:31 PM
Last Post: Ruslan8819
  [PyGame] AttributeError while switching between Sprite Animations paulmills3 2 2,440 Jul-12-2021, 02:24 AM
Last Post: noahcentineo
  FileNotFoundError when I try putting sprite in Pygame zionkozisek 9 16,097 Dec-09-2020, 04:42 AM
Last Post: zionkozisek
  [PyGame] My Pygame Sprite not appearing... noodlespinbot 3 3,831 Oct-30-2020, 06:51 AM
Last Post: robinmurphy
  [PyGame] Sprite Animation to create an explosion Russ_CW 2 3,954 Oct-23-2020, 04:57 PM
Last Post: Russ_CW
  [PyGame] Efficient sprite scaling with Vector3 michael1789 1 1,871 Apr-25-2020, 11:18 AM
Last Post: Windspar
  My Pygame Sprite not appearing... noodlespinbot 1 2,264 Apr-08-2020, 11:25 AM
Last Post: pyzyx3qwerty
  Speed issue with sprite update michael1789 13 4,599 Mar-06-2020, 03:06 AM
Last Post: michael1789

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020