Python:pygame - 不导入图像

时间:2015-02-15 06:10:09

标签: python image pygame pycharm

我在Pycharm上使用Python 3.4.2。我想要显示的图像与代码文件位于同一文件夹中。一切似乎运行正常,直到我尝试导入图片。这是我的代码:

import pygame
import sys
from pygame.locals import *
pygame.init()


white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
purple = (255,0,255)

setDisplay = pygame.display.set_mode((400,300))
pygame.display.set_caption("this is a game")
singlePixel = pygame.PixelArray(setDisplay)
setDisplay.fill(green)
image = pygame.image.load("Jeffery.png")
while True:
    pygame.display.update()
    for event in pygame.event.get():
        setDisplay.blit(image,0,0)

        if event.type == QUIT:
            pygame.quit()
            sys.exit()

每当我尝试运行程序时,都会出现以下错误:

"TypeError: invalid destination position for blit" 

请帮忙!我研究了一个多小时,我仍然找不到解决方案!非常感谢所有帮助!

1 个答案:

答案 0 :(得分:0)

原因是你的blit语句在for循环中。对于Pyhton 2.x或3.x,错误是相同的。通过“blit的无效目标位置”,这意味着您无法将该语句放在该位置。我看到的大多数人,使用任何一个版本的Python,都会在for循环中将blit语句 放在 之后,但不在其中。确保它仍在你的while循环中,错误应该消失了。而且,而不是:

(image, 0, 0)

执行:

(image, (0, 0))

防止()中的无效内容出现其他错误。我希望这可以帮助你!