1

When I draw the image and the rect Wof the images, then the upper left corner of the rectangle is exactly in the center of the image.

def blitRotateCenter(image, left, top, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = (left, top)).center)
    screen.blit(rotated_image, new_rect)
self.image = pygame.image.load("Bilder/car.png")   
self.rect = self.image.get_rect()
    
blitRotateCenter(auto.image, auto.rect.x, auto.rect.y, auto.wagen_winkel)
draw.rect(screen,red,auto.rect)
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Joachim
  • 375
  • 1
  • 12

1 Answers1

1

Just return new_rect from blitRotateCenter and use it to draw the rectangle:

def blitRotateCenter(image, left, top, angle):
    rotated_image = pygame.transform.rotate(image, angle)
    new_rect = rotated_image.get_rect(center = image.get_rect(center = (left, top)).center)
    screen.blit(rotated_image, new_rect)
    return new_rect
new_auto_rect = blitRotateCenter(auto.image, auto.rect.x, auto.rect.y, auto.wagen_winkel)
draw.rect(screen,red, new_auto_rect)

However, if you want to draw a rotated rectangle, see Getting rotated rect of rotated image in Pygame.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • But i cannot use the new_auto_rect for collision detected.What can i manage it? – Joachim Dec 03 '21 at 10:20
  • Why can't you use it for collision detection? – Rabbid76 Dec 03 '21 at 10:37
  • Why can't you use it for collision detection? You can store `new_auto_rect` in `self.rect`. `self.rect = new_auto_rect`. Or do you search for [PyGame collision with masks](https://stackoverflow.com/questions/57455811/pygame-collision-with-masks/57499484#57499484)? – Rabbid76 Dec 03 '21 at 11:28
  • i set auto.rect = new_auto_rect. But the the image run over the screen. I think i need to center it? Where? if it works i want to use hits = pygame.sprite.spritecollide( auto, land, False, pygame.sprite.collide_mask) – Joachim Dec 04 '21 at 02:46
  • @Joachim No you do not need to center it. It is centered. It is used to draw a rectangle on the screen. Theres is something messed up in your code. I suggest to ask a new question. – Rabbid76 Dec 04 '21 at 07:20