CCXX2017 2017-10-18 07:52 采纳率: 0%
浏览 5722

pygame error: couldn't open the ship.bmp

在pygame里练习一个游戏程序,遇到“pygame error: couldn't open the ship.bmp”在网上查了下错误的原因,主要是说要补全路径或把bmp图片放在.py文件相同的目录下,我各种方式都试了,但还是报错...
以下是代码:

import sys

import pygame

from settings import Settings
from ship import Ship

def run_game():
#initialize game and create a window obj
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")

#creat a ship
ship = Ship(screen)


#begin main cycle of game

while True:

    #monitor key and shubiao
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        #draw a new screen while a new cycle begin
        screen.fill(ai_settings.bg_color)
        ship.blitme()

        #show the near screen
        pygame.display.flip()

run_game()

模块ship.py

import pygame

class Ship():

def __init__(self,screen):
    """initialize ship and set location of ship"""
    self.screen = screen

    #load image of ship and get the bounding box of the ship
    self.image = pygame.image.load(r'images\ship.bmp')
    self.rect = self.image.get_rect()
    self.screen_rect = screen.get_rect()

    #place each ship into center of bottom of screen
    self.rect.centerx = self.screen_rect.centerx
    self.rect.bottom = self.screen_rect.bottom
def blitme(self):
    """drawing the ship in the specific place"""
    self.screen.blit(self.image,self.rect)
  • 写回答

3条回答 默认 最新

  • LT688_58B 2017-10-23 07:30
    关注

    你要把图片放到 py源码目录下的 images 文件夹内,
    并且保证 ship.bmp 的一个有效的,能浏览的图片文件。

    评论

报告相同问题?

悬赏问题

  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺