首页 文章

AttributeError:object没有属性rect

提问于
浏览
0

我从我的代码中得到一个错误,我不知道为什么,但从语法上讲,它是正确的,我想

import pygame

class BaseClass(pygame.sprite.Sprite):
    allsprites = pygame.sprite.Group()  
    def __init__(self, x, y, width, height, image_string):
        pygame.sprite.Sprite.__init__(self)
        BaseClass.allsprites.add(self)

        self.image = pygame.image.load("Images\lebron.png")
        self.rectangle = self.image.get_rect()
        self.rectangle.x = x
        self.rectangle.y = y
        self.width = width
        self.height = height

class Lebron(BaseClass):
    List = pygame.sprite.Group()
    def __init__(self, x, y, width, height, image_string):
        BaseClass.__init__(self, x, y, width, height, image_string)
        Lebron.List.add(self)
        self.velx = 0

    def movement(self):
        self.rectangle.x += self.velx

所以,如果我将其编译到我的主文件,代码如下

import pygame, sys
from classes import *
from process import process

pygame.init()
WIDTH, HEIGHT = 640, 360
screen = pygame.display.set_mode((WIDTH, HEIGHT))

clock = pygame.time.Clock()
FPS = 24

lebron = Lebron(0, 100, 104, 120, "Images\lebron.png")

while True:
    process()
    #LOGIC
    lebron.movement()
    #LOGIC

    #DRAW
    BaseClass.allsprites.draw(screen)
    screen.fill((0,0,0))
    pygame.display.flip()
    #DRAW

    clock.tick(FPS)

我收到一个属性错误的错误:Lebron对象没有属性rect . 怎么了?追溯:

Traceback (most recent call last):
 File "MAIN.py", line 24, in <module>
   BaseClass.allsprites.draw(screen)
 File "C:\Python27\lib\site-packages\pygame\sprite.py", line 409, in draw
   self.spritedict[spr] = surface_blit(spr.image, spr.rect)
 AttributeError: 'Lebron' object has no attribute 'rect'

1 回答

  • 0

    从发布的回溯看起来(当你将它们缩进4个空格时它们看起来好得多,当你编辑问题以包含它们时它们会更好),PyGame draw()方法希望你的属性提供 rect 属性 . 猜测一下,如果用 rect 替换 rectangle 的每一次出现,你至少会取得一些进展 .

    基本上,PyGame想要绘制你的对象,但它无法找出方法 .

相关问题