How can I just draw a specific sprite in a Group with pygame draw()?
Solution 1
First, add a self.type
to your each of your sprites. Give it a value, such as self.type = 1
. Then call this when you want to draw a certain sprite or group of sprites:
for sprite in group: #Replace group with your sprite group
if sprite.type == 1: #Or whatever type you want
sprite.draw(screen)
elif sprite.type == 2:
sprite.draw(screen)
#Etc...
Next, in your sprite
class make sure you inherit from pygame.sprite.Sprite
:
class my_sprite_class(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__()
#Etc...
#Add this draw function so we can draw individual sprites
def draw(self, screen):
screen.blit(self.image, self.rect)
I forgot that individual sprites don't have a draw function, just the groups. @pmoleri @jackdh my bad
Please read this post here to get an idea of what the class can do.
If you have any more questions just comment below.
Solution 2
You could blit any individual sprite, but I don't recommend it. This would be the way:
sprite = stuff_to_draw.sprites()[0]
screen.blit(sprite.image, sprite.rect)
My recommendation is to keep in the Sprites Group what you want to draw at a moment, and keep the rest in a separate list or group.
Jack
Updated on June 05, 2022Comments
-
Jack almost 2 years
<Group(4 sprites)>
I have 4 sprites in a group they all ready to draw with the line,
stuff_to_draw.draw(screen)
They all currently draw at the same time.
Is it possible to do something like this
stuff_to_draw[0].draw(screen)
I've looked at documentation for pygame Groups, but can not find any helpful information about selecting a specific element in the group.
What am I trying to achieve?
The sprite group currently holds a answer sprite which consists of a location (x,y) and a image of the letter to blit.
Currently across all maps the answer is blitted instead of on the current question. So I would like to be able to say on question one
stuff_to_draw[0]
then question twostuff_to_draw[1]
. -
Jack about 10 yearsWhy would you not recommend it? Would you mind showing me how I would do it anyway for educational purposes?
-
pmoleri about 10 yearsBecause Sprites Groups are meant for drawing all its elements, and some advanced groups (ex: RenderUpdates) can even keep track of every updated area to make optimizations in the update of the screen.
-
pmoleri about 10 yearsSprite.draw() doesn't exist.
-
Remolten about 10 yearsYes it does, if you're using
pygame.sprite.Sprite
. You usually usepygame.sprite.Sprite
with pygame's sprite Groups. And please don't downvote an answer because it isn't yours. Thank you :) -
Jack about 10 yearsHi I tried this, and I pass it correctly as seen gist.github.com/jackdh/58732728834f2b8f7338 although I get a error. gist.github.com/jackdh/a398887c329ec7fb9aaa Any idea why?
-
pmoleri about 10 years@Remolten it doesn't:
pygame.sprite.Sprite().draw(None)
gives:'Sprite' object has no attribute 'draw'
. And your assumption of me downvoting because it isn't my answer, is really far from true. -
Remolten about 10 yearsPlease read my new edited post above. It should work. @pmoleri no offense intended bro :)
-
pmoleri about 10 years@Remolten I read the updated post but it still doesn't have a draw() method. If you want one you need to implement it in
my_sprite_class
. -
Remolten about 10 yearsPlease read the link. When you subclass
pygame.sprite.Sprite
you are makingmy_sprite_class
a "child" of it. That mean it inherits all of thepygame.sprite.Sprite
class' functions etc. One of these is thedraw(screen)
function. -
Jack about 10 yearsI've tried using your edited version although It still gives me the errors Enzone has no attribute draw. gist.github.com/jackdh/ee17a9042604e5c4dd33 Thanks for the help so far!
-
Remolten about 10 yearsMy bad guys, didn't write my answer correctly. Should work now I hope.