我无法在我的pygame中导入图片,任何回复都将不胜感激!
我使用python3.2 32位
当我运行这个程序时,我可以获得音乐,按钮(按钮按钮很好),窗口,但没有图片!而且,它没有任何理由终止!它只说:
a = pygame.image.load('1,png')
pygame.error: Couldn't open 1,png
这是我的代码:
import sys
import Person
import Buttons
import pygame
from pygame.locals import *
pygame.init()
displayWidth = 800 # The width of the game window
displayHeight = 450 # The height of the game window
NAVYBLUE = (60, 60, 100) # The navyblue color
RED = (255, 0, 0) # The red color
WHITE = (255, 255, 255) # The white color
class Guys(Person.person):
'''a male class that extends person class from Person module'''
def __init__(self, name, picture):
self.name = name
self.picture = picture
def setPic(self, picture):
self.picture = pygame.image.load(picture)
def setName(self, name):
self.name = name
class Ladies(Person.person):
'''a female class that extends person class from Person module'''
def __init__(self, name, picture):
self.name = name
self.picture = picture
def setPic(self, picture):
self.picture = pygame.image.load(picture)
def setName(self, name):
self.name = name
class Actions(Buttons.Button):
'''a class contains button actions'''
def __init__(self):
pass
def display(self):
self.screen = pygame.display.set_mode((displayWidth, displayHeight), 0, 32)
pygame.display.set_caption('Match Game')
def update_display(self):
self.screen.fill(NAVYBLUE)
#Parameters: surface, color, x, y, length, height, width, text, text_color
self.Button1.create_button(self.screen, (RED), 170, 370, 100, 60, 0, "MATCHED", (WHITE))
self.Button1.create_button(self.screen, (RED), 570, 370, 100, 60, 0, "UNMATCHED", (WHITE))
pygame.display.flip()
def main():
# The main method
action = Actions()
action.Button1 = Buttons.Button()
action.Button2 = Buttons.Button()
action.display()
soundObj = pygame.mixer.Sound('pla.wav')
soundObj.play()
malePic = ["'a.png'", "'b.png'", "'c.png'"]
femalePic = ["'1.png'", "'2.png'", "'3.png'"]
maleName = ['a', 'b', 'c']
femaleName = ['1', '2', '3']
while True: # The main game loop
action.update_display()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == MOUSEBUTTONDOWN:
for index1 in range(len(femalePic)):
for index2 in range(len(malePic)):
p1 = pygame.image.load(femalePic[index1])
p2 = pygame.image.load(malePic[index2])
DISPLAYSURF.blit(p1, (120, 60))
DISPLAYSURF.blit(p2, (320, 60))
pygame.display.flip()
if action.Button1.pressed(pygame.mouse.get_pos()):
print ('MATCHED')
elif action.Button2.pressed(pygame.mouse.get_pos()):
print ('UNMATCHED')
if __name__ == '__main__':
main()
我有一个问题报告txt文件:
> <?xml version="1.0" encoding="UTF-16"?> <DATABASE> <EXE
> NAME="pythonw.exe" FILTER="CMI_FILTER_PRIVACY">
> <MATCHING_FILE NAME="python.exe" SIZE="26624" CHECKSUM="0x9945D8B" MODULE_TYPE="WIN32" PE_CHECKSUM="0xB8E1" LINKER_VERSION="0x0"
> LINK_DATE="04/06/2013 18:07:46" UPTO_LINK_DATE="04/06/2013 18:07:46"
> EXE_WRAPPER="0x0"
> FILE_ID="00005fcb54d6a678978d7fad6b688933330ae2c47a2a"
> PROGRAM_ID="0000da39a3ee5e6b4b0d3255bfef95601890afd80709" />
> <MATCHING_FILE NAME="pythonw.exe" SIZE="27136" CHECKSUM="0xBF101C9B" MODULE_TYPE="WIN32" PE_CHECKSUM="0x12472"
> LINKER_VERSION="0x0" LINK_DATE="04/06/2013 18:07:55"
> UPTO_LINK_DATE="04/06/2013 18:07:55" EXE_WRAPPER="0x0"
> FILE_ID="0000313a13dfada2dabc1f4ca178db40afd49f0a5ae8"
> PROGRAM_ID="0000da39a3ee5e6b4b0d3255bfef95601890afd80709" />
> <MATCHING_FILE NAME="w9xpopen.exe" SIZE="49664" CHECKSUM="0x14EB69E" MODULE_TYPE="WIN32" PE_CHECKSUM="0x150D9"
> LINKER_VERSION="0x0" LINK_DATE="04/06/2013 18:06:48"
> UPTO_LINK_DATE="04/06/2013 18:06:48" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="DLLs\python3.dll" SIZE="80896" CHECKSUM="0x27E3CE9" MODULE_TYPE="WIN32" PE_CHECKSUM="0x0"
> LINKER_VERSION="0x0" LINK_DATE="04/06/2013 18:06:43"
> UPTO_LINK_DATE="04/06/2013 18:06:43" EXPORT_NAME="python3.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="DLLs\sqlite3.dll" SIZE="453632" CHECKSUM="0xC0357E30" MODULE_TYPE="WIN32" PE_CHECKSUM="0x7BC36"
> LINKER_VERSION="0x0" LINK_DATE="04/06/2013 18:07:00"
> UPTO_LINK_DATE="04/06/2013 18:07:00" EXPORT_NAME="sqlite3.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="DLLs\tcl85.dll" SIZE="880128" CHECKSUM="0x12DE6A39" BIN_FILE_VERSION="8.5.2.9"
> BIN_PRODUCT_VERSION="8.5.2.9" PRODUCT_VERSION="8.5.9"
> FILE_DESCRIPTION="Tcl DLL" COMPANY_NAME="ActiveState Corporation"
> PRODUCT_NAME="Tcl 8.5 for Windows" FILE_VERSION="8.5.9"
> ORIGINAL_FILENAME="tcl85.dll" LEGAL_COPYRIGHT="Copyright © 2001 by
> ActiveState Corporation, et al" VERDATEHI="0x0" VERDATELO="0x0"
> VERFILEOS="0x4" VERFILETYPE="0x2" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0xD9E87" LINKER_VERSION="0x0"
> UPTO_BIN_FILE_VERSION="8.5.2.9" UPTO_BIN_PRODUCT_VERSION="8.5.2.9"
> LINK_DATE="12/17/2010 20:37:56" UPTO_LINK_DATE="12/17/2010 20:37:56"
> EXPORT_NAME="tcl85.dll" VER_LANGUAGE="English (United States) [0x409]"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="DLLs\tclpip85.dll" SIZE="7168" CHECKSUM="0x67151215" MODULE_TYPE="WIN32" PE_CHECKSUM="0x89B3"
> LINKER_VERSION="0x0" LINK_DATE="12/17/2010 20:37:57"
> UPTO_LINK_DATE="12/17/2010 20:37:57" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="DLLs\tk85.dll" SIZE="1338880" CHECKSUM="0x4A2EC8A9" BIN_FILE_VERSION="8.5.2.9"
> BIN_PRODUCT_VERSION="8.5.2.9" PRODUCT_VERSION="8.5.9"
> FILE_DESCRIPTION="Tk DLL" COMPANY_NAME="ActiveState Corporation"
> PRODUCT_NAME="Tk 8.5 for Windows" FILE_VERSION="8.5.9"
> ORIGINAL_FILENAME="tk85.dll" LEGAL_COPYRIGHT="Copyright © 2001 by
> ActiveState Corporation, et al" VERDATEHI="0x0" VERDATELO="0x0"
> VERFILEOS="0x4" VERFILETYPE="0x2" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x14E696" LINKER_VERSION="0x0"
> UPTO_BIN_FILE_VERSION="8.5.2.9" UPTO_BIN_PRODUCT_VERSION="8.5.2.9"
> LINK_DATE="12/17/2010 20:38:19" UPTO_LINK_DATE="12/17/2010 20:38:19"
> EXPORT_NAME="tk85.dll" VER_LANGUAGE="English (United States) [0x409]"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\distutils\command\wininst-6.0.exe" SIZE="61440" CHECKSUM="0xE589B8AD" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x0" LINKER_VERSION="0x0" LINK_DATE="05/31/2008 04:52:45"
> UPTO_LINK_DATE="05/31/2008 04:52:45" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\distutils\command\wininst-7.1.exe" SIZE="65536" CHECKSUM="0xA2833DFD" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x0" LINKER_VERSION="0x0" LINK_DATE="05/31/2008 04:53:42"
> UPTO_LINK_DATE="05/31/2008 04:53:42" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\distutils\command\wininst-8.0.exe" SIZE="61440" CHECKSUM="0x8527B654" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x1701B" LINKER_VERSION="0x0" LINK_DATE="10/04/2006
> 15:16:27" UPTO_LINK_DATE="10/04/2006 15:16:27" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\distutils\command\wininst-9.0-amd64.exe" SIZE="224256" CHECKSUM="0x2C21FF43" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x3C5B1" LINKER_VERSION="0x0" LINK_DATE="01/29/2009
> 13:03:06" UPTO_LINK_DATE="01/29/2009 13:03:06" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\distutils\command\wininst-9.0.exe" SIZE="196096" CHECKSUM="0x4C294F6E" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x3C5CF" LINKER_VERSION="0x0" LINK_DATE="01/29/2009
> 13:02:55" UPTO_LINK_DATE="01/29/2009 13:02:55" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\avcodec-52.dll" SIZE="4899328" CHECKSUM="0xBF3D51B0" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x4B4EEF" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:10:02" UPTO_LINK_DATE="07/28/2010 06:10:02"
> EXPORT_NAME="avcodec-52.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\avformat-52.dll" SIZE="814592" CHECKSUM="0xD1E0D6CC" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0xD3DE8" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:10:04" UPTO_LINK_DATE="07/28/2010 06:10:04"
> EXPORT_NAME="avformat-52.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\avutil-50.dll" SIZE="109568" CHECKSUM="0x80797396" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x1DEED" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:10:00" UPTO_LINK_DATE="07/28/2010 06:10:00"
> EXPORT_NAME="avutil-50.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libfreetype-6.dll" SIZE="447488" CHECKSUM="0x296AA74B" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x77900" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:22" UPTO_LINK_DATE="07/28/2010 06:09:22"
> EXPORT_NAME="libfreetype-6.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libjpeg-8.dll" SIZE="216064" CHECKSUM="0x63F87CEF" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x3769A" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:33" UPTO_LINK_DATE="07/28/2010 06:09:33"
> EXPORT_NAME="libjpeg-8.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libogg-0.dll" SIZE="19968" CHECKSUM="0xA05C0F23" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0xF551" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:47" UPTO_LINK_DATE="07/28/2010 06:09:47"
> EXPORT_NAME="libogg-0.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libpng14.dll" SIZE="269824" CHECKSUM="0xE94B5FE7" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x4DAF5" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:30" UPTO_LINK_DATE="07/28/2010 06:09:30"
> EXPORT_NAME="libpng14.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libtiff-3.dll" SIZE="343040" CHECKSUM="0x6C14CED" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x5631D" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:39" UPTO_LINK_DATE="07/28/2010 06:09:39"
> EXPORT_NAME="libtiff-3.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libvorbis-0.dll" SIZE="156160" CHECKSUM="0x15B14624" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x354F4" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:50" UPTO_LINK_DATE="07/28/2010 06:09:50"
> EXPORT_NAME="libvorbis-0.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\libvorbisfile-3.dll" SIZE="33280" CHECKSUM="0x85544035" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0xF978" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:51" UPTO_LINK_DATE="07/28/2010 06:09:51"
> EXPORT_NAME="libvorbisfile-3.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\portmidi.dll" SIZE="26112" CHECKSUM="0x7DE9FEF5" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x16349" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:57" UPTO_LINK_DATE="07/28/2010 06:09:57"
> EXPORT_NAME="portmidi.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\SDL.dll" SIZE="319488" CHECKSUM="0x13D6A99" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x55926" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:15" UPTO_LINK_DATE="07/28/2010 06:09:15" EXPORT_NAME="SDL.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\SDL_image.dll" SIZE="65536" CHECKSUM="0x861F1E67" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x1EF46" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:41" UPTO_LINK_DATE="07/28/2010 06:09:41"
> EXPORT_NAME="SDL_image.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\SDL_mixer.dll" SIZE="492544" CHECKSUM="0x52F25032" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x79FD7" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:54" UPTO_LINK_DATE="07/28/2010 06:09:54"
> EXPORT_NAME="SDL_mixer.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\SDL_ttf.dll" SIZE="20992" CHECKSUM="0xF55F822C" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x678C" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:27" UPTO_LINK_DATE="07/28/2010 06:09:27"
> EXPORT_NAME="SDL_ttf.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\smpeg.dll" SIZE="346112" CHECKSUM="0xF627FBA9" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x5A413" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:44" UPTO_LINK_DATE="07/28/2010 06:09:44" EXPORT_NAME="smpeg.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\swscale-0.dll" SIZE="331264" CHECKSUM="0xEC66C2CB" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x5C632" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:10:05" UPTO_LINK_DATE="07/28/2010 06:10:05"
> EXPORT_NAME="swscale-0.dll" EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="Lib\site-packages\pygame\zlib1.dll" SIZE="114688" CHECKSUM="0x85A47953" MODULE_TYPE="WIN32"
> PE_CHECKSUM="0x238A8" LINKER_VERSION="0x10000" LINK_DATE="07/28/2010
> 06:09:19" UPTO_LINK_DATE="07/28/2010 06:09:19" EXPORT_NAME="zlib1.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="tcl\dde1.3\tcldde13.dll" SIZE="18432" CHECKSUM="0xCA5FF567" MODULE_TYPE="WIN32" PE_CHECKSUM="0x10EB2"
> LINKER_VERSION="0x0" LINK_DATE="12/17/2010 20:37:58"
> UPTO_LINK_DATE="12/17/2010 20:37:58" EXPORT_NAME="tcldde13.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="tcl\reg1.2\tclreg12.dll" SIZE="17408" CHECKSUM="0x88668FF5" MODULE_TYPE="WIN32" PE_CHECKSUM="0xFD5B"
> LINKER_VERSION="0x0" LINK_DATE="12/17/2010 20:37:57"
> UPTO_LINK_DATE="12/17/2010 20:37:57" EXPORT_NAME="tclreg12.dll"
> EXE_WRAPPER="0x0" />
> <MATCHING_FILE NAME="tcl\tix8.4.3\tix84.dll" SIZE="262656" CHECKSUM="0x3E0EAE66" MODULE_TYPE="WIN32" PE_CHECKSUM="0x4423E"
> LINKER_VERSION="0x0" LINK_DATE="08/28/2010 12:59:46"
> UPTO_LINK_DATE="08/28/2010 12:59:46" EXPORT_NAME="tix84.dll"
> EXE_WRAPPER="0x0" /> </EXE> </DATABASE> <EXE NAME="kernel32.dll"
> FILTER="CMI_FILTER_THISFILEONLY">
> <MATCHING_FILE NAME="kernel32.dll" SIZE="1114112" CHECKSUM="0x9725986B" BIN_FILE_VERSION="6.1.7601.17651"
> BIN_PRODUCT_VERSION="6.1.7601.17651" PRODUCT_VERSION="6.1.7601.17686"
> FILE_DESCRIPTION="Windows NT BASE API Client DLL"
> COMPANY_NAME="Microsoft Corporation" PRODUCT_NAME="Microsoft® Windows®
> Operating System" FILE_VERSION="6.1.7601.17686
> (win7sp1_gdr.110906-1506)" ORIGINAL_FILENAME="kernel32"
> INTERNAL_NAME="kernel32" LEGAL_COPYRIGHT="© Microsoft Corporation. All
> rights reserved." VERDATEHI="0x0" VERDATELO="0x0" VERFILEOS="0x40004"
> VERFILETYPE="0x2" MODULE_TYPE="WIN32" PE_CHECKSUM="0x1164FD"
> LINKER_VERSION="0x60001" UPTO_BIN_FILE_VERSION="6.1.7601.17651"
> UPTO_BIN_PRODUCT_VERSION="6.1.7601.17651" LINK_DATE="07/16/2011
> 04:27:04" UPTO_LINK_DATE="07/16/2011 04:27:04"
> EXPORT_NAME="KERNEL32.dll" VER_LANGUAGE="English (United States)
> [0x409]" EXE_WRAPPER="0x0"
> FILE_ID="0000754e133c87fd29a1432a7af8ab8c69f244eb7d99"
> PROGRAM_ID="0000f519feec486de87ed73cb92d3cac802400000000" /> </EXE>
> </DATABASE>
答案 0 :(得分:1)
这里有一些潜在的问题:
Pygame.image.load需要文件名(或路径)或Python文件对象。我不确定你的PNG文件是否真的命名为1,png
,但我首先要遵循一个更标准的filename.ext方案,如1.png
。此外,Pygame文档建议使用类似os.path.join(filename)
的内容来解决兼容性问题;这可能有助于解决您的问题。
您有Pic
列表引用图片名称的原因是否有原因? E.g。
malePic = ["'a.png'", "'b.png'", "'c.png'"]
pygame.image.load
只需要一个文件名的字符串("a.png"
),通过双引号("'a.png'"
)你正在使image.load查找一个名为'a.png'
的文件,我猜测它可能不是你的png文件的名称。