Here is a modified version of your code. I commented out the image loading as i do not have the images to load. You can remove the Surface and fill and load the images. As well as the associated color tuple for each.
I assume you mean by choose an xcord you mean random x value?
Drawing an object is just blitting the object to the screen at the position of the rect.
Every time this is ran you will get a random selected object from the list at a random x coordinate. The colors are only to differentiate between each object in the list. As is there is no point whatsoever to have your class inherit pygame.sprite.Sprite.
- Code: Select all
screen = pygame.display.set_mode((800,600))
screen_rect = screen.get_rect()
done = False
def __init__(self,tag,image,color, pos):
#self.image = pygame.image.load(image)
self.image = pygame.Surface((50,50)).convert()
self.tag = tag
self.rect = self.image.get_rect(center=pos)
def draw(self, surf):
objects = [
ObjectClass('tree','tree.png', (255,0,0), (get_rand_width(screen_rect),500)),
ObjectClass('rock','rock.png', (0,255,0), (get_rand_width(screen_rect),500)),
ObjectClass('fence','fence.png', (0,0,255), (get_rand_width(screen_rect),500)),
selected_object = random.choice(objects)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True