I come across problems like this a lot, but here's an example.
Let's say I'm making a main menu for a game. There are 5 buttons, play, settings, level select, tutorial, and more from this developer. I want to use the same class for each of these buttons. Each of them will have different text to display, positions, and colors. I could do the time consuming if statements, but is there any faster way?
def __init__(self): self.button=button if self.button=="play": self.text="play" elif self.button=="settings": self.text="settings" #and so on... The method I've been using right now is lists.
text=["play","settings","tutorial"] colors=[black,white,red] for i in range(3): button=button_class() button.text=text[i] button.color=colors[i] It's been working pretty good, but I still haven't done anything that involves a lot more specific data for each class. I was wondering if anyone has any insight on this? This is Python after all, I'm confident there's a simple solution to this. Oh and if it's any help, I'm using pygame.
https://stackoverflow.com/questions/65588587/most-efficient-way-to-assign-different-things-to-objects-made-from-the-class January 06, 2021 at 08:35AM
没有评论:
发表评论