无法将图片上传到pygame

时间:2014-02-16 20:12:38

标签: image upload pygame

我试图理解为什么当我创建我的类的实例图片时,文件将无法加载。我已将文件cartizzle.png放在同一目录中,但我一直收到错误说:错误:无法打开cartizzle.png

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

1 个答案:

答案 0 :(得分:0)

你应该把'while true'想象成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()

这将使它工作。它使它适用于我,我在尝试运行您的代码时收到错误消息。