2014-09-12 57 views
0

切割過的所有不相關的部分,這是我是如何實現它:這是更新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() 

如果沒有這個條件語句中後臺更新這個精靈沒有被刪除,當然,操作系統我得到的屏幕上的多個副本。我有強烈的懷疑,這是迄今爲止不是處理這種情況的最佳方式,因爲每次我需要做其他事情都不會改變的圖像看起來像是浪費資源。

我將不勝感激任何建議

+0

你有一種方法可以知道精靈先前佔據了什麼?你可以用'pygame.draw.rect'而不是整個背景 – 2014-09-12 16:01:53

+0

或者根據你的背景'screen.blit(background,[sprite_old_x,sprite_old_y])' – 2014-09-12 16:04:57

+0

來繪製那部分背景,這當然是一個選項。我懷疑這雖然不是最好的方法。我想,應該是涉及精靈陣列的東西。 – Alex 2014-09-12 16:05:07

回答

2

這是我會建議,基於個人經驗。我製作了一個基於拼圖的遊戲並過度設計了一下。我的最終解決方法看起來有點像這樣:

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