Pro-grammer
Pro-grammer

Reputation: 870

loading random objects, and drawing them onto screen

Im using python, pygame.

Im trying to display a random number of images to a window, heres how:

Class sprite():

def __init__(self):
  self.screen = pygame.display.set_mode((200,200))
  self.sprite=[[500,200]]

def load_sprite(self, image, x,y):
  image = pygame.image.load(image)
  self.screen.blit(image,(x,y))
  pygame.dsiplay.update()

def load_sprites(self, image, x,y):
   image = pygame.image.load(image)
   self.screen.blit(image,(x,y))
   pygame.display.update()

def draw_random_sprites(self, rand1, rand2):
   self.load_spites(0,200,random.randint(rand1,rand2))

When i call the flowing methods from the main class:

class Main():
   
    s = sprite()
    s.load_sprites(self, image, x,y): //here i want the user to determine the number of random sprites they load. 

But, when i do this i get a

cant seek data source error

Any suggestions on how I might achieve this?

EDIT

Heres the stack trace for the program.

Traceback (most recent call last):
  File "C:\Python33\game\Main.py", line 15, in <module>
    s.draw_random_sprites(20,40)
  File "C:\Python33\game\sprite.py", line 29, in draw_random_sprites
    self.load_spites(0,200,random.randint(rand1,rand2))
  File "C:\Python33\game\sprite.py", line 22, in load_sprites
    image = pygame.image.load(image)
pygame.error: Can't seek in this data source

Upvotes: 4

Views: 181

Answers (2)

furas
furas

Reputation: 142641

To get random number of images (no less than 1, no more than 100)

def draw_random_sprites(self, rand1, rand2):
    for i in range(random.randint(1,100)):
        self.load_spites("sprite.png",200,random.randint(rand1,rand2))

by the way: load_sprite() and load_sprites() are identical so you can use load_sprite() in place of load_sprites() and delete load_sprites()

Upvotes: 1

PearsonArtPhoto
PearsonArtPhoto

Reputation: 39698

I spot two errors. The first one is here, where you essentially tell load_sprites to load image 0. You are placing that between 200 and a random integer.

self.load_spites(0,200,random.randint(rand1,rand2))

The tag image is passed as both an input and an output. This likely will cause some issues further down the line, unless that's exactly what you intended.

image = pygame.image.load(image)

Look at fixing these two items, and you should be able to make this work. A method of doing so might be:

self.load_spites("sprite.png",200,random.randint(rand1,rand2))

def load_sprites(self, image_file, x,y):
   image = pygame.image.load(image_file)

Upvotes: 2

Related Questions