我的代码出错了,我不知道为什么,但从语法上来说,这是正确的,我想
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'
答案 0 :(得分:0)
从发布的回溯看起来(当你将它们缩进4个空格时看起来好多了,编辑问题以包含它们时更好),PyGame draw()方法希望你的属性提供{{1属性。猜测一下,如果您用rect
替换每一个rectangle
,您将至少取得一些进展。
基本上,PyGame想要绘制你的对象,但它无法找到方法。