2014-02-16 61 views
0

我想了解爲什麼當我創建我的類的實例圖片該文件將不會加載。我已經把文件cartizzle.png在同一個目錄,但我不斷收到一條錯誤消息:錯誤:無法打開cartizzle.png麻煩上傳圖像到pygame

class Picture():  

    def __init__(self, location, filename): 
     self.x = location[0] 
     self.y = location[1] 
     self.filename = filename 

    def draw(self): 
     pygame.init() 
     surface_sz = 500 
     main_surface = pygame.display.set_mode((surface_sz, surface_sz)) 

    while True: 
     ev = pygame.event.poll() 
     if ev.type == pygame.QUIT: 
      break 
     elif ev.type == pygame.KEYDOWN and ev.key == pygame.K_q: 
      break   
     main_surface.fill((0,200,255)) 
     main_surface.blit(self.filename, (self.x, self.y))    
     pygame.display.flip() 
    pygame.quit() 

pic_inst = Picture((150,200), pygame.image.load("cartizzle.png")) 
pic_inst.draw() 

回答

0

你應該準備的「而真正的」入draw()函數。

class Picture(): 

def __init__(self, location, filename): 
    self.x = location[0] 
    self.y = location[1] 
    self.filename = filename 

def draw(self): 
    pygame.init() 
    surface_sz = 500 
    main_surface = pygame.display.set_mode((surface_sz, surface_sz)) 

    while True: 
     ev = pygame.event.poll() 
     if ev.type == pygame.QUIT: 
      break 
     elif ev.type == pygame.KEYDOWN and ev.key == pygame.K_q: 
      break 
     main_surface.fill((0,200,255)) 
     main_surface.blit(self.filename, (self.x, self.y)) 
     pygame.display.flip() 
    pygame.quit() 

pic_inst = Picture((150,200), pygame.image.load("cartizzle.png")) 
pic_inst.draw() 

這將使它工作。它使它適用於我,當試圖運行你的代碼時,我收到了錯誤信息。