加载随机对象,并将其绘制到屏幕上

2024-09-28 21:02:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用python,pygame。你知道吗

我正试图在一个窗口中显示随机数目的图像,以下是方法:

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))

当我从主类调用流方法时:

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. 

但是,当我这么做的时候

cant seek data source error

对我如何实现这一目标有什么建议吗?你知道吗

编辑

这是程序的堆栈跟踪。你知道吗

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

Tags: inimageselfdefloadrandomscreenpygame
2条回答

获取随机数量的图像(不少于1张,不超过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))

顺便说一下:load_sprite()load_sprites()是相同的,因此可以使用load_sprite()代替load_sprites()并删除load_sprites()

我发现两个错误。第一个是在这里,您基本上告诉load\u sprites加载图像0。你把它放在200和一个随机整数之间。你知道吗

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

标签图像作为输入和输出传递。这很可能会导致一些问题进一步下去,除非这正是你的意图。你知道吗

image = pygame.image.load(image)

看看修复这两个项目,你应该能够使这项工作。这样做的方法可能是:

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

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

相关问题 更多 >