【问题标题】:Having trouble uploading image into pygame无法将图像上传到 pygame
【发布时间】:2014-02-16 20:12:38
【问题描述】:

我试图理解为什么当我创建我的图片类的实例时文件不会加载。我已将文件 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()

【问题讨论】:

    标签: image upload pygame


    【解决方案1】:

    你应该打算在 draw() 函数中加入 'while true'。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 2017-01-29
      • 2015-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多