So I've gotten to the point in my program where I need to create a group for some sprites that the player can collide with without dying (like some other sprites I may have on screen).
I've scoured Google but it appears that the official pygame documentation is useless and/or hard to comprehend. I'm looking for just a wee bit of help from anyone who knows a bit about this.
First, I need to find out how to create a group. Does it go in the initial game setup?
Then adding a sprite to a group upon its creation. The pygame site has this to say on the subject:
Sprite.add(*groups)
So... how does one use this? Let's say I have an sprite named gem. I need to add gem to the gems group. Is it:
gem = Sprite.add(gems)
I doubt it, but without any examples to go off of on the site, I am at a loss.
Furthermore, I would love to be able to edit attributes for a certain group. Is this done by defining a group like I would a class? Or is it something I define within the definition for the existing sprite, but with an 'if sprite in group'?
pygame includes a class called the sprite. Group . It's a very useful class. 00:10 It allows you to hold and manage multiple Sprite objects.
A sprite sheet is a single file that contains many smaller images, all on a plain or transparent background. To use a sprite sheet, you load the sprite sheet as a single large image, and then you load the individual images from the sprite sheet image.
(Press C to combine the sprites.)
We can easily add collisions in Pygame shapes using the colliderect( ). For this, we are going to draw two rectangles then we will check if the rectangles are colliding or not. Parameters: It will take two rects as its parameters. Returns: Returns true if any portion of either rectangle overlap.
To answer your first question; to create a group you would do something like this:
gems = pygame.sprite.Group()
Then to add a sprite:
gems.add(gem)
Regarding the attributes for the group you'd like to edit it depends what they are. For example you could define something like this to indicate the direction of the group:
gems.direction = 'up'
I know this question has already been answered, but the best method is like what kelwinfc suggested. I'll elaborate so it's more understandable.
# First, create you group
gems = pygame.sprite.Group()
class Jewel (pygame.sprite.Sprite): # Inherit from the Sprite
def __init__ (self, *args): # Call the constructor with whatever arguments...
# This next part is key. You call the super constructor, and pass in the
# group you've created and it is automatically added to the group every
# time you create an instance of this class
pygame.sprite.Sprite.__init__(self, gems)
# rest of class stuff after this.
>>> ruby = Jewel()
>>> diamond = Jewel()
>>> coal = Jewel()
# All three are now in the group gems.
>>> gems.sprites()
[<Jewel sprite(in 1 groups)>, <Jewel sprite(in 1 groups)>, <Jewel sprite(in 1 groups)>]
You can also add more with gems.add(some_sprite)
and likewise remove them with gems.remove(some_sprite)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With