在《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. [转]Nginx的负载均衡方式

    如果Nginx没有仅仅只能代理一台服务器的话,那它也不可能像今天这么火,Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用.具体配置过程如下: 1. 在http节点下,添加ups ...

  2. springmvc搭建环境时报No mapping found for HTTP request with URI [/exam3/welcome] in DispatcherServlet with name 'spring2'

    项目是使用spring MVC (1)在浏览器中访问,后台总报错: No mapping found for HTTP request with URI [/exam3/welcome] in Dis ...

  3. 自定义垂直拖动的seekbar进度条

    系统自定义的seekbar为横向拖动的样式,需要纵向的时则需要自己定义,网上很多说了重写系统SeekBar中onDraw()的方法,但是我使用的时候不知道为什么拖动条和点偏离了,不在一条直线上,好气. ...

  4. jQuery 时间控件推荐

    My97DatePicker  My97DatePicker是一个更全面,更人性化,并且速度一流的日期选择控件.具有强大的日期范围限制功能:自定义事件和丰富的API库:多语言支持和自定义皮肤支持:跨无 ...

  5. 总结这两天连续干掉的bug In 创新实训 智能自然语言交流系

    临近项目合并,在pre合并中出现相当多的hug,遂记之 ps:这只是总结一下提纲,具体的方法在前文的博文中都详细记录.总结了. 平台的移植兼容性,虽然是跨平台的java,但是依旧有很多的意外: 1.从 ...

  6. 关于Unity中自带摇杆与车轮碰撞器的使用

    准备 在创建好项目目录的基础上 导入一个第三方的资源包,在Project面板里面 右键---->Import Package---->Custom Package---->easy_ ...

  7. Quick solution to java.lang.NoClassDefFoundError: org/openqa/selenium/HasInputDevices error

    In case if you face this problem, one of the possible solutions that will work for you is to make su ...

  8. mysql 查看某个库下面某个表的所有列字段, columnName

    mysql 查看某个库下面某个表的所有列字段 select COLUMN_NAME as columnName from information_schema.COLUMNS where table_ ...

  9. 【Centos】【Python】【Flask】阿里云上部署一个 flask 项目

    1. 安装 python3 和 pip3 参考:http://www.cnblogs.com/mqxs/p/8692870.html 2.安装 lnmpa 集成开发环境 参考:http://www.c ...

  10. Spring和mybatis的整合

    一.搭建项目开发环境 1. 新建一个maven项目SpringMybatis,项目结构如下: 说明: src/main/java 存放java代码和映射文件: com.study.springmyba ...