我需要安装一个图片,但是它报错
I need to install a picture, but it gives an error
我正在尝试根据 Eric Matiz 的书“学习 python”创建一个游戏并创建了 Ship 模块,该模块中存在一个我无法解决的错误
这是代码本身:
import pygame
class Ship():
def __init__(self, screen):
"""Init ship and sets its starting position"""
self.screen = screen
# load image ship and gets rectangle.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# every new ship is starting in bottom edge screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def bliteme(self):
"""Draws the ship at the current position."""
self.screen.blit(self.image, self.rect)
我尝试通过“os”模块来完成,但没有任何效果
错误:
self.image = pygame.image.loading('images/ship.bmp')
FileNotFoundError error: There is no such file or directory.
文件位于名为“ship.bmp”的“images”目录下,路径指定正确,但仍然报错
我试图在互联网上找到答案,但是 mos只有这样的错误仍然没有有效的答案
将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
图像文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))
我正在尝试根据 Eric Matiz 的书“学习 python”创建一个游戏并创建了 Ship 模块,该模块中存在一个我无法解决的错误
这是代码本身:
import pygame
class Ship():
def __init__(self, screen):
"""Init ship and sets its starting position"""
self.screen = screen
# load image ship and gets rectangle.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
self.screen_rect = screen.get_rect()
# every new ship is starting in bottom edge screen.
self.rect.centerx = self.screen_rect.centerx
self.rect.bottom = self.screen_rect.bottom
def bliteme(self):
"""Draws the ship at the current position."""
self.screen.blit(self.image, self.rect)
我尝试通过“os”模块来完成,但没有任何效果 错误:
self.image = pygame.image.loading('images/ship.bmp')
FileNotFoundError error: There is no such file or directory.
文件位于名为“ship.bmp”的“images”目录下,路径指定正确,但仍然报错
我试图在互联网上找到答案,但是 mos只有这样的错误仍然没有有效的答案
将文件放在同一目录或子目录中是不够的。您还需要设置工作目录。
图像文件路径必须相对于当前工作目录。工作目录可能与 python 脚本的目录不同。
将以下内容放在代码的开头,以将工作目录设置为与脚本目录相同:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))