我從我的代碼中的錯誤沒有屬性,我不知道爲什麼,但在語法上,它是正確的,我想AttributeError的:對象具有矩形
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)
我得到一個錯誤,這是屬性錯誤:勒布朗對象沒有屬性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'
哪條線產生的錯誤?你能否提供完整的錯誤追溯? –
pygame的可能試圖打電話給你的對象的'矩形()'或以其他方式ACCES坐在'rect'屬性方法時,它沒有一個 - 看http://www.pygame.org/docs/ref/rect.html(和魯本的有關回溯右) – holdenweb
了一個與代碼BaseClass.allsprites.draw(屏幕) – TheChosenOne