在《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('images\ship.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)

pygame.error: Couldn't open images/ship.bmp的更多相关文章

  1. pygame.error: video system not initialized

    在pygame写游戏出现pygame.error: video system not initialized 源代码 import sysimport pygamedef run_game(): py ...

  2. pygame加载中文名mp3文件出现error

    好一阵子没有写东西了,最近几天在做一个基于Python pygame的音乐播放器,本来想做完了,再来发篇文章的,可越做越深,框架大致出来了,考虑周期比较长,也可能是我个人问题,做得比较慢,最近.下面来 ...

  3. pygame模块参数汇总(python游戏编程)

    一.HelloWorld pygame.init() #初始函数,使用pygame的第一步: pygame.display.set_mod((600,500),0,32) #生成主屏幕screen:第 ...

  4. Python pygame库的应用

    今天想用pygame库写一个击打外星人飞船的python程序 这个游戏的效果是操纵一个位于屏幕底端的飞船,通过上下左右控制飞船移动方向,按空格发射子弹.游戏中击杀一批飞船后进入下一关卡.每一关卡击打飞 ...

  5. Pygame小游戏练习二

    @Python编程从入门到实践 Python项目练习 四.创建Ship类 建立ship.py,创建Ship类,管理飞船行为. # ship.py import pygame class Ship(): ...

  6. Python小游戏——外星人入侵(保姆级教程)第一章 03设置飞船图片 04创建Ship类

    系列文章目录 第一章:武装飞船 03:设置飞船图片 04:创建Ship类--管理飞船行为的类 一.设置飞船图片 1.注意事项 A.将图片设置为位图bmp格式最简单,因为pygame默认加载位图 B.飞 ...

  7. Some User Can Not Execute "Ship Confirm"(Doc ID 473312.1)

    APPLIES TO: Oracle Shipping Execution - Version 11.5.10.2 and later Information in this document app ...

  8. pygame系列_箭刺Elephant游戏

    这个游戏原名为:Chimp,我们可以到: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html 获取到源码和详细的源码讲解 下面是我对游戏 ...

  9. 基于pygame实现飞机大战【面向过程】

    一.简介 pygame 顶级pygame包 pygame.init - 初始化所有导入的pygame模块 pygame.quit - uninitialize所有pygame模块 pygame.err ...

随机推荐

  1. 【微信小程序】tabBar的显示问题

    tabBar不显示 在app.json中配置了4个页面,在tabBar的list中随意写了两个页面,编译后发现不能显示tabBar. { "pages": [ "page ...

  2. 键盘 Input子系统

    应用层测试代码 #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <li ...

  3. hbase源码系列(一)Balancer 负载均衡

    看源码很久了,终于开始动手写博客了,为什么是先写负载均衡呢,因为一个室友入职新公司了,然后他们遇到这方面的问题,某些机器的硬盘使用明显比别的机器要多,每次用hadoop做完负载均衡,很快又变回来了. ...

  4. PCL采样一致性算法

    在计算机视觉领域广泛的使用各种不同的采样一致性参数估计算法用于排除错误的样本,样本不同对应的应用不同,例如剔除错误的配准点对,分割出处在模型上的点集,PCL中以随机采样一致性算法(RANSAC)为核心 ...

  5. on SDN

    sdn (software defined network ) emulex 网络 新型网络创新架构 网络虚拟化的一种实现方式 核心技术:OpenFlow 分离网络设备的控制层面和数据层面 目的:实现 ...

  6. 【转】Android配置文件权限一览表

    转自:http://blog.csdn.net/kbest/article/details/7495154访问登记属性 android.permission.ACCESS_CHECKIN_PROPER ...

  7. thinkphp5的Redis缓存配置

    thinkphp采用cache类提供缓存功能支持,采用驱动方式,在使用缓存之前需要进行初始化操作.支持的缓存类型包括file.memcache.wincache.sqlite.redis和xcache ...

  8. K-SVD算法

    它与K-mean算法原理上是类似的: K-mean 算法: (之前写过:http://www.cnblogs.com/yinheyi/p/6132362.html) 对于初始化的类别中心,可以看作初化 ...

  9. 第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码

    第三百八十三节,Django+Xadmin打造上线标准的在线教育平台—第三方模块django-simple-captcha验证码 下载地址:https://github.com/mbi/django- ...

  10. rqalpha-自动量化交易系统(一)

    因为最近做的东西牵涉到自动计算这一块,在网上搜了一下,基本上python做自动量化交易成了一个趋势,于是花了两天学习一下. 目标很简单,学习,使用. rqalpha看起来是比较成熟的,这儿看重的是自带 ...