0

I have a pygame Sprite which is generated through Font. It's just a 16x16 surface with a letter printed on it and blitted.

The sprite has a timer (it's a powerup) and when it's near the end of it's life I want it to flash a random color on every update. I've been successful doing this with other text but that text isn't a sprite, just a string I blit to the score board. I figured this would be the same, but once the sprite is generated, no matter how much I change the sprite's color, the change doesn't translate to the screen (though if I print(self.color) I can see the updated color tuple in the console).

I've tried putting the random color picker inside the Class as well as trying outside the class in my while loop. I can change the color easily enough, but the sprite on screen doesn't actually change. I am not using an external sprite image, just a Font blitted to a pygame.Surface.

This is my item class.

class Item(pygame.sprite.Sprite):
    def __init__(self, name, pos):
        pygame.sprite.Sprite.__init__(self)
        self.name = name
        self.image = pygame.Surface([16, 16])
        self.image.set_colorkey(black)
        self.font = pygame.font.Font("./fonts/myfont.ttf", 16)
        self.pos = pos
        if self.name == "health":
            self.color = (255, 0, 0)
            self.text = self.font.render("H", True, self.color)

        self.lifespan = 200
        self.lifespan_counter = 0
     
        self.image.blit(self.text, (0, 0))

    def update(self):
        # Update timer
        self.lifespan_counter += 0.1
        if self.lifespan_counter >= self.lifespan:
            self.kill()
        # Update position
        self.rect.center = (int(self.pos[0]), int(self.pos[1]))

And then at the bottom of my def main() in the while loop, I have this stuff:

        random_color_counter += 1
        if random_color_counter > 3:
            random_color = get_random_color()
            random_color_counter = 0

        screen.fill(background)
        text_box.fill(blue)
        game_box.fill(white)

        # Update the sprites positions and then draw them to game_box surface
        player_sprites.update()
        player_bullet_sprites.update()
        enemy_sprites.update()
        enemy_bullet_sprites.update()
        item_sprites.update()

        player_sprites.draw(game_box)
        player_bullet_sprites.draw(game_box)
        enemy_sprites.draw(game_box)
        enemy_bullet_sprites.draw(game_box)
        item_sprites.draw(game_box)

        ...

        for i in item_sprites:
            game_box.blit(i.image, (int(i.pos[0]), int(i.pos[1])))

        # Refresh everything
        pygame.display.update()

And this is the function that picks a new color.

def get_random_color():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    return r, g, b    

And then I can use the color random_color for most things, just not sprites apparently.

Like I said, this displays the sprite just fine at the position it is supposed to (where the baddie died), but I cannot seem to have a change to the item sprites color translate to the screen. I'm just not seeing what I'm doing wrong.

5
  • " no matter how much I change the sprite's color, the change doesn't translate to the screen" - Where do you try to "change" the color. I can't finde this part of the code in your question. Commented Jun 3, 2021 at 6:07
  • Everything I tried was unsuccessful. Based on what you see above, what is the best approach that I should take towards being able to do this? How would you do it? Commented Jun 3, 2021 at 8:14
  • You have to render the text with a new color. You have to show the attempts in your question. Commented Jun 3, 2021 at 8:19
  • Very good. I didn't try re-rendering the text! It gets rendered on init and then stays the same color for life. I assumed that once it became a sprite, I could control the sprites color. Looks like I did ask a good question since I got a good, simple answer. Thanks very much. Very helpful. Commented Jun 3, 2021 at 8:23
  • No your question is not "good". I had to guess what you want. I could only guess because I read a lot of incomplete questions like this one and this is a common problem. Commented Jun 3, 2021 at 8:26

1 Answer 1

0

When you want to change the color of the text, you have to render the text again and update the text Surface. Write a change_color method:

class Item(pygame.sprite.Sprite):
    # [...]

    def change_color(self, color):
        self.image = pygame.Surface([16, 16])
        self.image.set_colorkey(black)
        self.color = color
        self.text = self.font.render("H", True, self.color)
        self.image.blit(self.text, (0, 0))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.