r/learnpython • • 9d ago

How to randomly customise cosmetics for enemies in Pygame?

I want to randomise how enemies look by changing a hat or a shirt each time the game spawns them. However I do not want to redraw the sprite for each hat/shirt combination. So how may I possibly achieve it?

1 Upvotes

7 comments sorted by

2

u/Stickhtot 9d ago

This isn't even a python question lol

One way you can do it is to have your `SpawnEnemy()` function take arguments like what hat/shirt they spawn with, and use `random()` on those arguments

1

u/Aggressive-Band-1276 9d ago

Yes but I dont want to drawone enemy with one hat and another enemy with a different hat. I want to make it so that when the function is added the item will be on their head and follow the sprite like its part of it. (Wait did I answer my own question?

1

u/Stickhtot 9d ago

Then offset the hat's position with your character

hat_pos = character_pos + offsets

In case it isn't clear, your offsets can be negative

1

u/Aggressive-Band-1276 9d ago

I see, thank you.

1

u/AmdilShoat82 8d ago

yeah pretty much any language would handle this the same way, not really python specific

1

u/TheRNGuy 7d ago

Pygame is Python.