zoukankan      html  css  js  c++  java
  • pygame.error: Couldn't open images/ship.bmp

      在《python编程:从入门到实践》这本书中的《外星人入侵》的项目里有如下代码:

     Python Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
     
    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)

      运行时可能会出现错误:pygame.error: Couldn’t open images/ship.bmp

      将self.image = pygame.image.load(‘images/ship.bmp’)中的图片路径补全。(因为是Windows系统所以用反斜杠“”) 
           然后在路径前加一个 r 读取图片文件。具体代码如下:

     Python Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
     
    import pygame
    class Ship():
        
    def __init__(self, screen):
            
    """初始化飞船并设置其初始位置"""
            self.screen = screen
            
    # 加载飞船图像并获取其外接矩形
            # self.image = pygame.image.load('imagesship.bmp')
            self.image = pygame.image.load(r'D: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)

  • 相关阅读:
    委托,匿名方法,Lambda,泛型委托,表达式树
    Winform 异步调用一个方法
    计算两个经纬度的直线距离
    多线程中线程同步的几种方式
    音频文件相关
    c# 语音(二)文字生成WAV文件
    c# 语音
    三种创建委托的方式
    RunLoop 再次 探索与源码简析
    SDWebImage 实现原理与源码简析
  • 原文地址:https://www.cnblogs.com/MakeView660/p/9552286.html
Copyright © 2011-2022 走看看