【发布时间】:2019-08-06 23:25:18
【问题描述】:
我正在为我的游戏导入背景图片,但我不断收到此错误:
"File "/Users//PycharmProjects/Platformer/levels.py", line 75, in __init__
self.background.set_colorkey(constants.WHITE)
AttributeError: 'SpriteSheet' object has no attribute 'set_colorkey'".
我已将一个名为 spritesheet_functions.py 的文件导入到我正在使用的文件中。但是,我确定我的SpriteSheet 类中有一个set_colorkey 命令,那么任何人都可以解决这个问题吗?
import pygame
from os import path
import constants
img_dir = path.join(path.dirname(__file__), 'IMG')
class SpriteSheet(object):
""" Class used to grab images out of a sprite sheet. """
def __init__(self, file_name):
""" Constructor. Pass in the file name of the sprite sheet. """
# Load the sprite sheet.
self.sprite_sheet = pygame.image.load(path.join(img_dir,
file_name)).convert()
def get_image(self, x, y, width, height):
""" Grab a single image out of a larger spritesheet
Pass in the x, y location of the sprite
and the width and height of the sprite. """
# Create a new blank image
image = pygame.Surface([width, height]).convert()
# Copy the sprite from the large sheet onto the smaller image
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
# Assuming black works as the transparent color
image.set_colorkey(constants.BLACK)
# Return the image
return image
class Level01(Level):
""" Definition for level 1. """
def __init__(self, player):
""" Create level 1. """
# Call the parent constructor
Level.__init__(self, player)
self.background = SpriteSheet('background_01.png')
self.background.set_colorkey(constants.WHITE)
self.level_limit = -2500
【问题讨论】:
-
文件
p1_walk.png是否位于您正在运行的代码可以访问的位置?它是有效的 PNG 图像文件(未损坏、为空等)吗?我的猜测是文件丢失了。 -
我写了完整的消息。
-
你可以使用Surface.subsurface而不是在
get_image中处理图像 -
坦率地说,这不是完整的消息。完整的消息从单词“Traceback”开始。但是看看哪行代码出了问题就足够了。
标签: python pygame pycharm python-3.7