这是更新pygame中的精灵的最佳方式吗?
问题描述:
切割过的所有不相关的部分,这是我是如何实现它:这是更新pygame中的精灵的最佳方式吗?
done=False
while(done==False)
#here goes some code for listening to the events
if (some condition fulfilled) then
screen.blit(background, [0,0])
screen.blit(some_sprite,[x,y])
elif (more conditions)
do something else
else
do even more stuff
pygame.display.flip()
如果没有这个条件语句中后台更新这个精灵没有被删除,当然,操作系统我得到的屏幕上的多个副本。我有强烈的怀疑,这是迄今为止不是处理这种情况的最佳方式,因为每次我需要做其他事情都不会改变的图像看起来像是浪费资源。
我将不胜感激任何建议
答
这是我会建议,基于个人经验。我制作了一个基于拼图的游戏并过度设计了一下。我的最终解决方法看起来有点像这样:
class Graphic(object):
def __init__(*some_args):
self.owner = which_object_owns_this_graphic
self.priority = some_int
# if more than one graphic are stacked on each other, which one to display?
self.surface = surface_to_draw_on
self.graphic = sprite_to_draw
@property
def x(self): return self.owner.x
@property
def y(self): return self.owner.y
def draw(self):
self.surface.blit(self.graphic, (self.x, self.y))
class Tile(object):
def __init__(*some_args):
self.x = x
self.y = y
self.graphic = some_default_Graphic_with_priority_minusone
self.contains = [self]
# list of objects that live here right now
@property
def topmost(self):
"""returns the graphic of the object with highest priority that is contained here"""
global_update_list = []
# anytime a tile is moved into or out of, place it here
然后在我的事件循环:
for tile in global_update_list:
tile.topmost.draw()
global_update_list = []
这让我无法重新绘制每次的东西搬到屏幕上,我可以只重绘瓦它移出并移入其中。
+0
确定有一个Surface.get_rect()命令,它可以做到这一点 – Alex 2014-09-13 10:27:31
你有一种方法可以知道精灵先前占据了什么?你可以用'pygame.draw.rect'而不是整个背景 – 2014-09-12 16:01:53
或者根据你的背景'screen.blit(background,[sprite_old_x,sprite_old_y])' – 2014-09-12 16:04:57
来绘制那部分背景,这当然是一个选项。我怀疑这虽然不是最好的方法。我想,应该是涉及精灵阵列的东西。 – Alex 2014-09-12 16:05:07