Example attached. This might get you going. Gumm
On 5/23/2014 07:02, Skorpio wrote:
Thanks for the help everybody, but it still doesn't work. I've tried these functions as collided values (as Jeffrey Kleykamp suggested): pygame.sprite.collide_rect() takes only sprites as values and then uses their rects. That means I can't use the sprite's scaled "hitbox" attribute, otherwise I get this error: AttributeError: 'pygame.Rect' object has no attribute 'rect' And if I do sprite.rect = sprite.hitbox, the image gets blitted at the top left corner again. pygame.Rect.colliderect() doesn't work. pygame.sprite.collide_rect_ratio() works as intended but causes a huge performance drop, so it's not useable for me. I have quite a lot of sprites on the screen (sometimes over 100). Christopher Night is right. It would be a lot easier for me, if Pygame sprites had an additional (optional) hitbox rect. Maybe I should rewrite my collision detection passage and just use pygame.Rect.colliderect() instead of pygame.sprite.spritecollide(). -- View this message in context: http://pygame-users.25799.x6.nabble.com/pygame-Scaling-the-hitbox-rect-of-a-sprite-tp1227p1237.html Sent from the pygame-users mailing list archive at Nabble.com.
import pygame from pygame.locals import * screen = pygame.display.set_mode((800, 600)) screen_rect = screen.get_rect() clock = pygame.time.Clock() avatar_group = pygame.sprite.Group() blob_group = pygame.sprite.Group() class Thing(pygame.sprite.Sprite): def __init__(self, rect, color): super(Thing, self).__init__() self.rect = rect self.color = color self.image = pygame.Surface(rect.size) self.image.fill(color) self.hitbox = rect.inflate(20, 20) def update(self): self.hitbox.center = self.rect.center def collided(this, other): return this.hitbox.colliderect(other.hitbox) avatar = Thing(Rect(screen_rect.center, (20, 20)), Color('blue')) avatar_group.add(avatar) for p in ((100, 100),(100, 400),(400, 400)): blob_group.add(Thing(Rect(p, (20, 20)), Color('red'))) while True: screen.fill((0, 0, 0)) clock.tick(30) if any([e.type == KEYDOWN for e in pygame.event.get()]): quit() pos = pygame.mouse.get_pos() avatar.rect.center = pos avatar_group.update() blob_group.update() blob_group.draw(screen) avatar_group.draw(screen) hits = pygame.sprite.groupcollide( avatar_group, blob_group, False, False, collided) for g in blob_group, avatar_group: for s in g: if s in hits: pygame.draw.rect(screen, Color('red'), s.hitbox, 1) else: pygame.draw.rect(screen, Color('grey'), s.hitbox, 1) pygame.display.flip()