![]() ![]() Pg.draw.rect(screen, (250, 30, 0), sprite.hitbox, 2)Īlternatively, you could give your sprite two offset vector attributes and add the current offset to the rect.topleft position when you blit the sprite. # Pass the custom collided callback function to spritecollide.Ĭollided_sprites = pg.sprite.spritecollide(player, enemies, False, collided) """Check if the hitboxes of the two sprites collide.""" # This callback function is passed as the `collided`argument Self.offset = -self.offset # Invert the offset. Self.image = pg.transform.flip(IMAGE, True, False) # Add the offset to the pos to center the hitbox Self.pos = Vector2(pos) # Actual center position. ![]() # A deflated copy of the rect as the hitbox. The images are blue and gray (with the hitboxes centered at the blue area), the hitboxes red and the outer rects ( self.rect for the blitting) are green. When the character and its image are flipped, you also have to invert the self.offset vector. You can do this by defining a vector (called offset here) which you have to add to the self.hitbox every update. In your case you also need to shift the hitbox because the weapon of the character is so large. In this function you can use the colliderect method of the hitbox rect to check if it collides with the rect (or hitbox) of the other sprite. Then define a custom collision detection function which will be passed as the collided argument to or groupcollide. ![]() When the pos is updated in the update method, you have to assign it to the center of the rect and the hitbox as well in order to move them. I also give my sprites a pos attribute (a 2) which represents the actual center position of the sprites. So create the hitbox either by scaling the original rect with the inflate method or create a new one with the desired size. (The darker area represents the scaled rect and the brighter area the image.) _ You have to do that because the coordinates are used as the blit position and if you shrink the rect, it will be smaller but only fill the upper left area of the image. #current state: 0 is idle, 1 is moving right, 2 is moving left, 3 is dashingįirst of all, you have to give your sprites a second rect attribute, the scaled hitbox which will be used for the collision detection. #references to the list of sprite we bumped against (24 / 5) Self.image = ("Malahayati.jpg").convert_alpha() #because there is only one character right now, it will have no case for character selection (23 / 5) I want to adjust the collision hitbox size so that when her feet is not on the platform, she falls to the ground.īelow is the code for sprite initiation: class Player(): So I coded a 2d sidescrolling platform, everything is fine except this:Īs you can see, the sprite is standing at the platform when her feet is not in the platform anymore. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |