0

使用 pygame '1.9.2pre' 运行 Python 3.3.0,遵循教程,python 新手,老实说看不到我哪里出错了,看起来和教程上的一样,但是它已经 4 岁了。感谢帮助!

我收到错误 - 两者都不支持的图像格式。我试过 jpg 和 png,版本规范说它都支持它们。

bif ="bg.jpg"
mif ="man.jpg"
import pygame, sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((1100,750),0,32)
background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()


Running = True

while Running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            Running = False
            sys.exit()
            break

    screen.blit(background,(0,0))

    x,y = pygame.mouse.get_pos()
    x -= mouse_r.get_width()/2
    y -= mouse_r.get_height()/2

    screen.blit(mouse_r,(x,y))

    pygame.display.update()
4

2 回答 2

0

也许您在这里使用了错误的变量名:

x,y = pygame.mouse.get_pos()
x -= mouse_r.get_width()/2
y -= mouse_r.get_height()/2

screen.blit(mouse_r,(x,y))

我以前看过相同的教程,似乎mouse_r应该是mouse_c

编辑:

在加载图像时尝试使用完整的目录路径名:

import os
bif = os.getcwd() + "\\bg.jpg"
mif = os.getcwd() + "\\man.jpg"
于 2013-08-19T17:58:02.470 回答
0

我会假设 pygame 窗口由于您的代码中的错误而没有关闭。您可以退出 python shell 以退出 pygame 窗口,但错误是这里的主要问题。

如果您要像这样导入图像,请确保图像与您的 .py 文件位于同一文件夹或位置,我不明白您从哪里得到 mouse_r。

尝试这个:

import pygame, sys
from pygame.locals import *



pygame.init()



screen = pygame.display.set_mode((1100,750),0,32)

background = pygame.image.load("bg.jpg").convert()
mouse_c = pygame.image.load("man.jpg").convert_alpha()

Running = True

while Running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            Running = False
            sys.exit()
            break

    screen.blit(background,(0,0))

    x,y = pygame.mouse.get_pos()
    x -= screen.get_width()/2
    y -= screen.get_height()/2

    screen.blit(mouse_c,(x,y))

    pygame.display.update()
于 2013-08-19T17:04:27.157 回答