我有一个游戏,每隔几秒就会在屏幕上弹出不同的图像,第一次加载每个图像时,游戏就会停止,就像它会暂停一样。 现在我正在加载它:
class Fruit(Widget):
def __init__(self, imagename, size, **kwargs):
super(Fruit, self).__init__(**kwargs)
with self.canvas:
self.size = size
self.x = self.center_x
self.y = self.center_y
self.pos = (self.x, self.y)
self.rect_bg = Rectangle(source=imagename,
pos=self.pos,
size=self.size)
self.bind(pos=self.update_bg_pos)
self.rect_bg.pos = self.pos
def update_bg_pos(self, instance, value):
self.rect_bg.pos = value
所以每当我需要创建一个水果时,我都会使用这个类并给它一个随机的图像和位置:
def add_fruit(self, *args):
imagename = "fruit{}.png".format(randint(1,6))
tmpFruit = Fruit(imagename=imagename, size(self.parent.height*0.067, self.parent.height*0.067))
tmpFruit.x = randint(1, self.width-40)
tmpFruit.y = randint(1, self.height-140)
self.fruit_list.append(tmpFruit)
self.add_widget(tmpFruit)
我在kivy文档中看到了加载程序和异步映像,但我们并不了解它们的工作方式以及如何在我的代码中使用它。 如果有人能告诉我这些是否会加快加载时间或如何从网址加载图片,我将不胜感激。