None 类型对象没有属性更新
None type object has no attribute update
我正在尝试制作游戏。就像在任何其他射击游戏中一样,敌人应该在射击时消失。显然,你不能杀死表面,但你可以杀死精灵。所以我试图将我的图像作为精灵加载,但我遇到了一个奇怪的错误。我创建 class,向其应用一个变量,为 pygame.sprite.Group
创建一个变量,并将 class 变量添加到组中。当我尝试更新组时,它显示 "'NoneType' object has no attribute 'update'"
。这是 class:
的代码
class Spawn(pygame.sprite.Sprite):
def __init__(self,primaryx,primaryy):
pygame.sprite.Sprite.__init__(self)
global directionM
self.directionM=directionM
x1=random.randint(100,400)
y1=random.randint(100,400)
self.x1=x1
self.y1=y1
self.primaryx=primaryx
self.primaryy=primaryy
def AIPrototype(self):
minionup=pygame.image.load("Alien.png").convert_alpha()
miniondown=pygame.image.load("Aliendown.png").convert_alpha()
minionleft=pygame.image.load("Alienleft.png").convert_alpha()
minionright=pygame.image.load("Alienright.png").convert_alpha()
global x,y,posx,posy
seperate=random.randint(1,1000)
screen.blit(self.directionM,(self.primaryx,self.primaryy))
if seperate==2:
self.primaryx=x+100
if seperate==20:
self.primaryx=x-100
if seperate==150:
self.primaryy=y+100
if seperate==200:
self.primaryy=y-100
self.x1=self.primaryx
self.y1=self.primaryy
if self.x1<x:
xspeed1=1
slopex1=x-self.x1
if self.x1>x:
xspeed1=-1
slopex1=self.x1-x
if self.y1<y:
yspeed1=1
slopey1=y-self.y1
if self.y1>y:
yspeed1=-1
slopey1=self.y1-y
#
hypo1=((slopex1**2)+(slopey1**2))**0.5
speedmark1=hypo1/1
speedy1=slopey1/speedmark1
speedx1=slopex1/speedmark1
movex1=speedx1
movey1=speedy1
if self.x1<=640 and self.x1>=0:
if self.x1>x:
self.x1+=xspeed1*movex1
if self.x1<x:
xspeed1=0
if self.y1<=480 and self.x1>=0:
if self.y1>y:
self.y1+=yspeed1*movey1
if self.y1<y:
yspeed1=0
if self.x1<=640 and self.x1>=0:
if self.x1<x:
self.x1+=xspeed1*movex1
if self.x1>x:
xspeed1=0
if self.y1<=480 and self.x1>=0:
if self.y1<y:
self.y1+=yspeed1*movey1
if self.y1>y:
yspeed1=0
#
if self.x1>640:
self.x1=640
if self.x1<0:
self.x1=0
if self.y1>480:
self.y1=480
if self.y1<0:
self.y1=0
if self.y1>=posy-20 and self.y1<=posy+20 and self.x1>=x-20 and self.x1<=x+20:
Spawn.kill()
self.primaryx=self.x1
self.primaryy=self.y1
这是游戏循环:
spritegroup=pygame.sprite.Group()
spawn=Spawn(600,200)
spritegroup=spritegroup.add(spawn)
clock = pygame.time.Clock()
keepGoing = True
try:
while keepGoing:
clock.tick(60)
screen.fill(THECOLORS['red'])
char()#start
x+1
posxlist.append(x)
posylist.append(y)
spritegroup.update()
pygame.display.flip()
我意识到我的代码真的很乱而且效率很低,对此我真的很抱歉。我对使用 classes 和精灵完全陌生。
spritegroup.add(spawn)
returnsNone
。不要将其分配回 spritegroup
,该操作 就地 更改了对象。
换句话说,改为这样做:
spritegroup = pygame.sprite.Group()
spawn = Spawn(600,200)
spritegroup.add(spawn)
来自spritegroup.Group.add()
documentation:
add Sprites to this Group
add(*sprites)
-> None
我正在尝试制作游戏。就像在任何其他射击游戏中一样,敌人应该在射击时消失。显然,你不能杀死表面,但你可以杀死精灵。所以我试图将我的图像作为精灵加载,但我遇到了一个奇怪的错误。我创建 class,向其应用一个变量,为 pygame.sprite.Group
创建一个变量,并将 class 变量添加到组中。当我尝试更新组时,它显示 "'NoneType' object has no attribute 'update'"
。这是 class:
class Spawn(pygame.sprite.Sprite):
def __init__(self,primaryx,primaryy):
pygame.sprite.Sprite.__init__(self)
global directionM
self.directionM=directionM
x1=random.randint(100,400)
y1=random.randint(100,400)
self.x1=x1
self.y1=y1
self.primaryx=primaryx
self.primaryy=primaryy
def AIPrototype(self):
minionup=pygame.image.load("Alien.png").convert_alpha()
miniondown=pygame.image.load("Aliendown.png").convert_alpha()
minionleft=pygame.image.load("Alienleft.png").convert_alpha()
minionright=pygame.image.load("Alienright.png").convert_alpha()
global x,y,posx,posy
seperate=random.randint(1,1000)
screen.blit(self.directionM,(self.primaryx,self.primaryy))
if seperate==2:
self.primaryx=x+100
if seperate==20:
self.primaryx=x-100
if seperate==150:
self.primaryy=y+100
if seperate==200:
self.primaryy=y-100
self.x1=self.primaryx
self.y1=self.primaryy
if self.x1<x:
xspeed1=1
slopex1=x-self.x1
if self.x1>x:
xspeed1=-1
slopex1=self.x1-x
if self.y1<y:
yspeed1=1
slopey1=y-self.y1
if self.y1>y:
yspeed1=-1
slopey1=self.y1-y
#
hypo1=((slopex1**2)+(slopey1**2))**0.5
speedmark1=hypo1/1
speedy1=slopey1/speedmark1
speedx1=slopex1/speedmark1
movex1=speedx1
movey1=speedy1
if self.x1<=640 and self.x1>=0:
if self.x1>x:
self.x1+=xspeed1*movex1
if self.x1<x:
xspeed1=0
if self.y1<=480 and self.x1>=0:
if self.y1>y:
self.y1+=yspeed1*movey1
if self.y1<y:
yspeed1=0
if self.x1<=640 and self.x1>=0:
if self.x1<x:
self.x1+=xspeed1*movex1
if self.x1>x:
xspeed1=0
if self.y1<=480 and self.x1>=0:
if self.y1<y:
self.y1+=yspeed1*movey1
if self.y1>y:
yspeed1=0
#
if self.x1>640:
self.x1=640
if self.x1<0:
self.x1=0
if self.y1>480:
self.y1=480
if self.y1<0:
self.y1=0
if self.y1>=posy-20 and self.y1<=posy+20 and self.x1>=x-20 and self.x1<=x+20:
Spawn.kill()
self.primaryx=self.x1
self.primaryy=self.y1
这是游戏循环:
spritegroup=pygame.sprite.Group()
spawn=Spawn(600,200)
spritegroup=spritegroup.add(spawn)
clock = pygame.time.Clock()
keepGoing = True
try:
while keepGoing:
clock.tick(60)
screen.fill(THECOLORS['red'])
char()#start
x+1
posxlist.append(x)
posylist.append(y)
spritegroup.update()
pygame.display.flip()
我意识到我的代码真的很乱而且效率很低,对此我真的很抱歉。我对使用 classes 和精灵完全陌生。
spritegroup.add(spawn)
returnsNone
。不要将其分配回 spritegroup
,该操作 就地 更改了对象。
换句话说,改为这样做:
spritegroup = pygame.sprite.Group()
spawn = Spawn(600,200)
spritegroup.add(spawn)
来自spritegroup.Group.add()
documentation:
add Sprites to this Group
add(*sprites)
->None