pygame.error: Couldn't open images\ship.bmp的解决办法
2017-11-03 16:10
483 查看
在《python编程:从入门到实践》这本书中的《外星人入侵》的项目里有如下代码:
在运行时可能会出现如下报错:
File “d:\python3wp\alien_invasion\ship.py”, line 8, in init
self.image = pygame.image.load(‘images/ship.bmp’)
pygame.error: Couldn’t open images/ship.bmp
经本人试用成功的解决办法如下:
将self.image = pygame.image.load(‘images/ship.bmp’)中的图片路径补全。(因为是Windows系统所以用反斜杠“\”)
然后在路径前加一个 r 读取图片文件。具体代码如下:
import pygame class Ship(): def __init__(self,screen): """初始化飞船并设置其初始位置""" self.screen = screen # 加载飞船图像并获取其外接矩形 self.image = pygame.image.load('images/ship.bmp') self.rect = self.image.get_rect() self.screen_rect = screen.get_rect() # 将每艘新飞船放在屏幕底部中央 self.rect.centerx = self.screen_rect.centerx self.rect.bottom = self.screen_rect.bottom def blitme(self): """在指定位置绘制飞船""" self.screen.blit(self.image,self.rect) # 根据指定位置将图像绘制到屏幕上
在运行时可能会出现如下报错:
File “d:\python3wp\alien_invasion\ship.py”, line 8, in init
self.image = pygame.image.load(‘images/ship.bmp’)
pygame.error: Couldn’t open images/ship.bmp
经本人试用成功的解决办法如下:
将self.image = pygame.image.load(‘images/ship.bmp’)中的图片路径补全。(因为是Windows系统所以用反斜杠“\”)
然后在路径前加一个 r 读取图片文件。具体代码如下:
self.image = pygame.image.load(r'D:\python3wp\alien_invasion\images\ship.bmp')
相关文章推荐
- 关于出现错误:_tkinter.TclError: couldn't open "E:\Python系列\Python程序\Python安装.gif"tkinter中不能打开GIF格式图片的解决办法
- nginx提示Error: Too many open files的解决办法
- not an error (code 0): Could not open the database in read/write mode.的解决办法
- Cognos open error 解决办法--转载
- Fatal server error: could not open 无法进图形界面的解决办法
- eclipse error:could not open jvm.cfg解决办法
- modelsim10.4仿真错误Error: (vlog-7) Failed to open design unit file "XXXXX" in read mode解决办法
- rpm: error while loading shared libraries: libgcc_s.so.1: cannot open shared object file: No such file or directory解决办法
- QT找不到文件解决办法 error: C1083: Cannot open include file: 'cstddef'
- PIL IOError: cannot identify image file 'images/1212.jpg'的解决办法
- "error while loading shared libraries:libatomic.so.1:cannot open shared objec"问题解决办法
- Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist解决办法
- 解决办法:Android Studio报错“Error:Failed to open zip file.”
- The operation couldn't be completed (LaunchServicesError error 0.)解决办法
- PyCharm ImportError: libcusolver.so.8.0: cannot open shared object file: No such file or directory 解决办法
- 真正的 ERROR: could not open required DirectShow codec drvc.bundle/contents/MacOS/drvc 的 解决 办法 Ubuntu9.10
- perl: symbol lookup error: /usr/local/lib/libtdsodbc.so: undefined symbol: libiconv_open的解决办法
- 【linux】nginx: [error] open() “/alidata/server/nginx/logs/nginx.pid” failed 解决办法
- error while loading shared libraries: libmysqlclient.so.18: cannot open shared object file: 的解决办法
- ADS编译错误Error: Fatal L6002U:Could not open file:.....解决办法