【问题标题】:when I use pygame to load an picture, but an error has occured当我使用pygame加载图片时,出现了错误
【发布时间】:2017-03-12 10:52:22
【问题描述】:

这是我的代码:

import pygame
class Ship():
    def __init__(self):
        self.image = pygame.image.load('images/ship.bmp')

my_ship = Ship
print(my_ship.image)
AttributeError: type object 'Ship' has no attribute 'image'

我使用 pycharm6 和 python3.6。我是python的初学者,希望有人能帮助我。

【问题讨论】:

  • 我们需要minimal, complete and verifiable example。并发布完整的回溯(错误消息)。
  • 对不起,我重新修改了格式。
  • 你应该多学习Python,然后你就会明白你需要实例化Ship的实例。

标签: python python-3.x pygame


【解决方案1】:

my_ship = Ship

就其本身而言,这不会加载变量,只是将其设置为实际类 Ship 的副本。将其更改为my_ship = Ship(),它实际上会使其成为 Ship 类的实例,因此self.image 变量也会被加载。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-10
    • 2018-05-03
    • 1970-01-01
    • 2018-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    相关资源
    最近更新 更多