我正在为我的游戏导入背景图片,但是我一直收到此错误:
"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
的文件导入到我正在使用的文件中。但是,我确定我的set_colorkey
类中有一个SpriteSheet
命令,因此有人可以解决此问题吗?
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
答案 0 :(得分:2)
您在
中以错误的方式使用set_colorkey
self.background = SpriteSheet('background_01.png')
self.background.set_colorkey(constants.WHITE)
完整的错误消息应该显示它。
self.background
是类SpriteSheet
的实例,不是具有Surface
set_colorkey
的实例
可能您必须在set_colorkey
的{{1}}上使用SpriteSheet.sprite_sheet
Surface