Python Pygame | AttributeError:';背景';对象没有属性';图像';
在小组()中拖拉精灵时,我遇到了一个问题。我计划稍后在解决此问题时绘制多个精灵 我尝试了几件事情来解决,比如重命名类名,将代码简化为基本原理(将图像作为sprite添加到Group()中)。我甚至检查了我的pygame练习计划,确认我确实把这个项目搞砸了。我认为重新安装Pygame库也不能解决这个问题 在所有情况下,以下是我的代码: main.pyPython Pygame | AttributeError:';背景';对象没有属性';图像';,python,pygame,Python,Pygame,在小组()中拖拉精灵时,我遇到了一个问题。我计划稍后在解决此问题时绘制多个精灵 我尝试了几件事情来解决,比如重命名类名,将代码简化为基本原理(将图像作为sprite添加到Group()中)。我甚至检查了我的pygame练习计划,确认我确实把这个项目搞砸了。我认为重新安装Pygame库也不能解决这个问题 在所有情况下,以下是我的代码: main.py import sys from pygamerectangle import Background from settings import set
import sys
from pygamerectangle import Background
from settings import settings
import GFunctions as gf
from pygame.sprite import Group
if __name__ == "__main__":
pygame.init()
setting = settings()
screen = pygame.display.set_mode((setting.width, setting.height))
pygame.display.set_caption("Pygame Theory")
background = 60,60,60
BackgroundGroup = Background()
BackgroundGroup = Group()
gf.BackgroundRepeat(BackgroundGroup)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(background)
BackgroundGroup.draw(screen)
pygame.display.flip()
import pygame
from pygame.sprite import Sprite
class Background(Sprite):
def __init__(self):
super().__init__()
self.img = pygame.image.load("background.png")
self.rect = self.img.get_rect()
import pygame
from pygamerectangle import Background
def BackgroundRepeat(BackgroundGroup):
BackgroundSample = Background()
BackgroundGroup.add(BackgroundSample) # Used as part of the fundamentals to add a single sprite.
# The code after this contains my plan to repeat the image;
# similar to old-school websites that had repeating
# backgrounds.
# BackgroundSampleWidth = BackgroundSample.rect.width
# ScreenAvailableSpace = setting.width - 2 * BackgroundSampleWidth
# NumberOfBackgrounds = int(ScreenAvailableSpace / (2 * BackgroundSampleWidth))
# for BackgroundRepeating in range(NumberOfBackgrounds):
# Backgrounds = Background(setting, screen)
# Backgrounds.x = BackgroundSampleWidth + 2 * BackgroundSampleWidth * BackgroundRepeating
# Backgrounds.rect.x = Backgrounds.x
# BackgroundGroup.add(Backgrounds)
pygamerectangle.py
import sys
from pygamerectangle import Background
from settings import settings
import GFunctions as gf
from pygame.sprite import Group
if __name__ == "__main__":
pygame.init()
setting = settings()
screen = pygame.display.set_mode((setting.width, setting.height))
pygame.display.set_caption("Pygame Theory")
background = 60,60,60
BackgroundGroup = Background()
BackgroundGroup = Group()
gf.BackgroundRepeat(BackgroundGroup)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(background)
BackgroundGroup.draw(screen)
pygame.display.flip()
import pygame
from pygame.sprite import Sprite
class Background(Sprite):
def __init__(self):
super().__init__()
self.img = pygame.image.load("background.png")
self.rect = self.img.get_rect()
import pygame
from pygamerectangle import Background
def BackgroundRepeat(BackgroundGroup):
BackgroundSample = Background()
BackgroundGroup.add(BackgroundSample) # Used as part of the fundamentals to add a single sprite.
# The code after this contains my plan to repeat the image;
# similar to old-school websites that had repeating
# backgrounds.
# BackgroundSampleWidth = BackgroundSample.rect.width
# ScreenAvailableSpace = setting.width - 2 * BackgroundSampleWidth
# NumberOfBackgrounds = int(ScreenAvailableSpace / (2 * BackgroundSampleWidth))
# for BackgroundRepeating in range(NumberOfBackgrounds):
# Backgrounds = Background(setting, screen)
# Backgrounds.x = BackgroundSampleWidth + 2 * BackgroundSampleWidth * BackgroundRepeating
# Backgrounds.rect.x = Backgrounds.x
# BackgroundGroup.add(Backgrounds)
GFunctions.py
import sys
from pygamerectangle import Background
from settings import settings
import GFunctions as gf
from pygame.sprite import Group
if __name__ == "__main__":
pygame.init()
setting = settings()
screen = pygame.display.set_mode((setting.width, setting.height))
pygame.display.set_caption("Pygame Theory")
background = 60,60,60
BackgroundGroup = Background()
BackgroundGroup = Group()
gf.BackgroundRepeat(BackgroundGroup)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
screen.fill(background)
BackgroundGroup.draw(screen)
pygame.display.flip()
import pygame
from pygame.sprite import Sprite
class Background(Sprite):
def __init__(self):
super().__init__()
self.img = pygame.image.load("background.png")
self.rect = self.img.get_rect()
import pygame
from pygamerectangle import Background
def BackgroundRepeat(BackgroundGroup):
BackgroundSample = Background()
BackgroundGroup.add(BackgroundSample) # Used as part of the fundamentals to add a single sprite.
# The code after this contains my plan to repeat the image;
# similar to old-school websites that had repeating
# backgrounds.
# BackgroundSampleWidth = BackgroundSample.rect.width
# ScreenAvailableSpace = setting.width - 2 * BackgroundSampleWidth
# NumberOfBackgrounds = int(ScreenAvailableSpace / (2 * BackgroundSampleWidth))
# for BackgroundRepeating in range(NumberOfBackgrounds):
# Backgrounds = Background(setting, screen)
# Backgrounds.x = BackgroundSampleWidth + 2 * BackgroundSampleWidth * BackgroundRepeating
# Backgrounds.rect.x = Backgrounds.x
# BackgroundGroup.add(Backgrounds)
注意:在GFunctions.py下,我知道一旦我取消对代码的注释,一些事情将无法工作,因为它们缺少参数,例如设置。我这样做是暂时的,因为我知道这不是问题的一部分
下面是我得到的错误:
AttributeError:“背景”对象没有属性“图像”
对于那些想知道我的背景图像是什么的人:
提前谢谢 程序正在“image”上引发AttributeError,这意味着您的背景对象没有“image”属性。通过查看您的代码,我可以看到背景对象确实具有“img”属性:
self.img=pygame.image.load(“background.png”)
因此,在代码中的某个地方,您一定是无意中键入了self.image而不是self.img。确保这些匹配,错误就会消失。程序正在“图像”上引发AttributeError,这意味着您的背景对象没有“图像”属性。通过查看您的代码,我可以看到背景对象确实具有“img”属性:
self.img=pygame.image.load(“background.png”)
因此,在代码中的某个地方,您一定是无意中键入了self.image而不是self.img。确保这些匹配,错误就会消失。虽然类名通常应使用大写字母约定,但变量名称应为小写。看 第四期是台词 在这几行之后,
BackgroundGroup
是的一个实例,没有属性.image
更改变量的名称。使用不同的和小写的名称。e、 g:
backgroundSprite=Background()
背景组=组()
gf.BackgroundRepeat(backgroundSprite)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
屏幕填充(背景)
背景组。绘图(屏幕)
pygame.display.flip()
def BackgroundRepeat(背景组):
backgroundSample=Background()
backgroundGroup.add(backgroundSample)
虽然类名通常应使用大写字母约定,但变量名称应为小写。看
第四期是台词
在这几行之后,BackgroundGroup
是的一个实例,没有属性.image
更改变量的名称。使用不同的和小写的名称。e、 g:
backgroundSprite=Background()
背景组=组()
gf.BackgroundRepeat(backgroundSprite)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
sys.exit()
屏幕填充(背景)
背景组。绘图(屏幕)
pygame.display.flip()
def BackgroundRepeat(背景组):
backgroundSample=Background()
backgroundGroup.add(backgroundSample)
在后台类中,图像存储在self.img中,而不是self.image中。确保在您的应用程序中使用self.imgcode@schwartz721令我惊讶的是,这实际上纠正了错误。我所要做的就是将self.img重命名为self.image(以及与之相关的一些其他小调整)。要么我假设Pygame让代码使用了一个名为image的变量,要么这只是我刚才做的一个错误的命名。不管怎样,非常感谢您的解决方案!有什么方法可以让我或其他人将你的评论标记为我对这个问题的回答吗?我写了一个答案,这样你就可以接受它。在你的后台类中,图像存储在self.img中,而不是self.image中。确保在您的应用程序中使用self.imgcode@schwartz721令我惊讶的是,这实际上纠正了错误。我所要做的就是将self.img重命名为self.image(以及与之相关的一些其他小调整)。要么我假设Pygame让代码使用了一个名为image的变量,要么这只是我刚才做的一个错误的命名。不管怎样,非常感谢您的解决方案!有没有什么方法可以让我或其他人将你的评论标记为对我提出的这个问题的回答?我写了一个答案,这样你就可以接受了。谢谢你给了我一个命名惯例的提示。我不太确定Pycal(CAMEL)情况是否在Python中使用变量,因为我在C++中学习了它,并且假设它只适用于C++本身。我承认我对政治公众人物没有太多的了解。@ATOPL问题是,你已经尝试使用BackgroundGroup
这个名称两次了!在这种情况下,C++中的命名约定是相同的。谢谢您给我命名惯例的提示。我不太确定Pycal(CAMEL)情况是否在Python中使用变量,因为我在C++中学习了它,并且假设它只适用于C++本身。我承认我对政治公众人物没有太多的了解。@ATOPL问题是,你已经尝试使用BackgroundGroup
这个名称两次了!在这种情况下,C++中的命名约定是相同的。