pygame.error: Couldn't open images/ship.bmp
在《python编程:从入门到实践》这本书中的《外星人入侵》的项目里有如下代码:
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 读取图片文件。具体代码如下:
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的更多相关文章
- pygame.error: video system not initialized
在pygame写游戏出现pygame.error: video system not initialized 源代码 import sysimport pygamedef run_game(): py ...
- pygame加载中文名mp3文件出现error
好一阵子没有写东西了,最近几天在做一个基于Python pygame的音乐播放器,本来想做完了,再来发篇文章的,可越做越深,框架大致出来了,考虑周期比较长,也可能是我个人问题,做得比较慢,最近.下面来 ...
- pygame模块参数汇总(python游戏编程)
一.HelloWorld pygame.init() #初始函数,使用pygame的第一步: pygame.display.set_mod((600,500),0,32) #生成主屏幕screen:第 ...
- Python pygame库的应用
今天想用pygame库写一个击打外星人飞船的python程序 这个游戏的效果是操纵一个位于屏幕底端的飞船,通过上下左右控制飞船移动方向,按空格发射子弹.游戏中击杀一批飞船后进入下一关卡.每一关卡击打飞 ...
- Pygame小游戏练习二
@Python编程从入门到实践 Python项目练习 四.创建Ship类 建立ship.py,创建Ship类,管理飞船行为. # ship.py import pygame class Ship(): ...
- Python小游戏——外星人入侵(保姆级教程)第一章 03设置飞船图片 04创建Ship类
系列文章目录 第一章:武装飞船 03:设置飞船图片 04:创建Ship类--管理飞船行为的类 一.设置飞船图片 1.注意事项 A.将图片设置为位图bmp格式最简单,因为pygame默认加载位图 B.飞 ...
- 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 ...
- pygame系列_箭刺Elephant游戏
这个游戏原名为:Chimp,我们可以到: http://www.pygame.org/docs/tut/chimp/ChimpLineByLine.html 获取到源码和详细的源码讲解 下面是我对游戏 ...
- 基于pygame实现飞机大战【面向过程】
一.简介 pygame 顶级pygame包 pygame.init - 初始化所有导入的pygame模块 pygame.quit - uninitialize所有pygame模块 pygame.err ...
随机推荐
- mysql5.5编译安装过程(自己总结)
一,安装过程 //创建mysql安装目录,创建数据存放目录,创建用户和用户组与赋予数据存放目录权限 mkdir -p /usr/local/mysql/ mkdir -p /data/mysql/ g ...
- How Not to Crash #6: Properties and Accessors(属性,存储器方法使问题)
How Not to Crash #6: Properties and Accessorshtml, body {overflow-x: initial !important;}html { font ...
- Linux 下 less 命令
less 命令功能强大,上能搜索,下能动态加载查看日志,并且每一屏内容都是预加载,不会加载整个文件,所以不会由于搜索内容过多把机器卡死 语法 less [参数] 文件地址 参数说明 -N:显示行号 f ...
- win10无法访问局域网共享文件?解决如此简单。。。。。
1 笔记本系统win10 X64企业版,其中一文件夹已设置为“共享”.本地帐号登录系统. 2 平板电脑系统win8.1 X64专业版,可以顺畅的访问笔记本的共享文件.微软帐号登录系统. 3 平板电脑系 ...
- Knockout开发中文API系列4–绑定关键字
目的 Visible绑定通过绑定一个值来确定DOM元素显示或隐藏 示例 <div data-bind="visible: shouldShowMessage"> You ...
- ubuntu16.04安装workbench
sudo dpkg -i mysql-workbench-community-6.3.10-1ubuntu16.04-amd64.deb 报错: Selecting previously unsele ...
- PDF文件分割和合并
今天自己用C#实现了下PDF文件的分割和合并,大家可以试用一下. 代码和使用说明在这里:https://github.com/cserspring/pdf_split_merge 有什么意见,大家可以 ...
- Sword pcre库函数学习一
0.pcre_exec 原型: #include <pcre.h> int pcre_exec(const pcre *code, const pcre_extra *extra, con ...
- 自然语言交流系统 phxnet团队 创新实训 项目博客 (七)
在本项目中使用到的“语音转文本”的技术总结: 语音转文本部分是调用的科大讯飞的在线语音,它的激发方式是按键,通过按钮触发开启安卓设备的录音,此部分需要在源码中写入关于安卓权限的要求,来调用安卓的录音权 ...
- SAP NUMBER RANGE维护配置object FBN1 Deletion only possible if status is initial
背景: 错误日志: SAP FBN1 Deletion only possible if status is initial 场景: 如果目标机已有NUMBER RANGE 不为0,需要删除配置年为9 ...