如何修复此 TypeError: add() 参数后 * 必须是可迭代的,而不是类型?

我正在编写有关蛇游戏的代码,但是我使用 python 3.7.2 和 pygame 1.9.0 给了我这个错误。

def update(self):

    if self.score ==len(self.tail):

        self.tail.append((self.rect.x,self.rect.y))

    else:

        self.tail.append((self.rect.x,self.rect.y))

        self.tail.pop(0)

    self.rect.x+=self.speedx

    self.rect.y+=self.speedy

    keys=pygame.key.get_pressed()

    if keys[pygame.K_RIGHT] and self.speedx >=0:

        self.speedx=10

        self.speedy=0

    if keys[pygame.K_LEFT] and self.speedx <=0:

        self.speedx=10

        self.speedy=0

    if keys[pygame.K_UP] and self.speedx <=0:

        self.speedx=10

        self.speedy=0

    if keys[pygame.K_DOWN] and self.speedx >=0:

        self.speedx=10

        self.speedy=0

    if self.rect.left >=width:

        self.rect.left=1

    if self.rect.left >=height:

        self.rect.top=0

    if self.rect.bottom <=0:

        self.rect.bottom=height

    if self.rect.left <=0:

        self.rect.left= width


def _exit(self):

    for i in range (1,len(self.tail)):

        if dist(self.rect.x,self.rect.y,self.tail[i][0],self.tail[i][1])<1:


收到一只叮咚
浏览 205回答 1
1回答

DIEA

创建python对象时,实例化需要加上括号,例如:class Colour:&nbsp; &nbsp; def __init__( self ):&nbsp; &nbsp; &nbsp; &nbsp; self.red&nbsp; &nbsp;= 0&nbsp; &nbsp; &nbsp; &nbsp; self.green = 0&nbsp; &nbsp; &nbsp; &nbsp; self.blue&nbsp; = 0light_red = Colour&nbsp; &nbsp; # <-- Wrongdark_red&nbsp; = Colour()&nbsp; # <-- Correct这种类型的错误发生在几个不同的实例化中。该错误是因为您的all_sprites精灵组不是精灵组的“副本”,而是对对象定义的引用。时钟对象也是如此sa3at。另外:有一个错字:fill不是dill;并且该all_sprites小组从未被吸引到屏幕上。import pygameimport randomimport syspygame.init()width=600height=400#rangakanspy=(255,255,255)swr=(100,0,0)zard=(255,255,0)shen=(0,0,255)rash=(0,0,0)rashe_tox=(50,50,50)shasha=pygame.display.set_mode((width,height))pygame.display.set_caption("Maraka")sa3at=pygame.time.Clock()runnung=Truedef dist(x1,y1,x2,y2):&nbsp; &nbsp; return ((x2-x1)**2 + (y2-y1)**2)**1/2def draw_txt(surf,text,size,x,y):&nbsp; &nbsp; font_name = pygame.font.match_font("arial")&nbsp; &nbsp; font = pygame.font.Font(font_name,size)&nbsp; &nbsp; text_surface = font.render(text,True,spy)&nbsp; &nbsp; text_rect=text_surface.get_rect()&nbsp; &nbsp; text_rect.midtop=(x,y)&nbsp; &nbsp; surf.blit(text_surface,text_rect)class Snake(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; pygame.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.image=pygame.Surface((12,12))&nbsp; &nbsp; &nbsp; &nbsp; self.image.fill(spy)&nbsp; &nbsp; &nbsp; &nbsp; self.rect=self.image.get_rect()&nbsp; &nbsp; &nbsp; &nbsp; self.rect.center=(100,100)&nbsp; &nbsp; &nbsp; &nbsp; self.speedx=0&nbsp; &nbsp; &nbsp; &nbsp; self.speedy=0&nbsp; &nbsp; &nbsp; &nbsp; self.score=0&nbsp; &nbsp; &nbsp; &nbsp; self.tail=[]&nbsp; &nbsp; def update(self):&nbsp; &nbsp; &nbsp; &nbsp; if self.score ==len(self.tail):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tail.append((self.rect.x,self.rect.y))&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tail.append((self.rect.x,self.rect.y))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.tail.pop(0)&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x+=self.speedx&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y+=self.speedy&nbsp; &nbsp; &nbsp; &nbsp; keys=pygame.key.get_pressed()&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_RIGHT] and self.speedx >=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedx=10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy=0&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_LEFT] and self.speedx <=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedx=10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy=0&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_UP] and self.speedx <=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedx=10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy=0&nbsp; &nbsp; &nbsp; &nbsp; if keys[pygame.K_DOWN] and self.speedx >=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedx=10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.speedy=0&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.left >=width:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.left=1&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.left >=height:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.top=0&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.bottom <=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.bottom=height&nbsp; &nbsp; &nbsp; &nbsp; if self.rect.left <=0:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.left= width&nbsp; &nbsp; def _exit(self):&nbsp; &nbsp; &nbsp; &nbsp; for i in range (1,len(self.tail)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if dist(self.rect.x,self.rect.y,self.tail[i][0],self.tail[i][1])<1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sys.exit()class Food(pygame.sprite.Sprite):&nbsp; &nbsp; def __init__(self,x,y):&nbsp; &nbsp; &nbsp; &nbsp; pygame.sprite.Sprite.__init__(self)&nbsp; &nbsp; &nbsp; &nbsp; self.image=pygame.Surface((12,12))&nbsp; &nbsp; &nbsp; &nbsp; self.image.fill(swr)&nbsp; &nbsp; &nbsp; &nbsp; self.rect=self.image.get_rect()&nbsp; &nbsp; &nbsp; &nbsp; self.rect.center=(x,y)all_sprites=pygame.sprite.Group()player=Snake()food=Food(random.randrange(20,width-20),random.randrange(20,height-20))all_sprites.add(player)all_sprites.add(food)while runnung:&nbsp; &nbsp; sa3at.tick(26)&nbsp; &nbsp; keys_pressed=pygame.event.get()&nbsp; &nbsp; for i in keys_pressed:&nbsp; &nbsp; &nbsp; &nbsp; if i.type==pygame.QUIT:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; runnung=False&nbsp; &nbsp; if pygame.sprite.collide_rect(player,food):&nbsp; &nbsp; &nbsp; &nbsp; food.kill()&nbsp; &nbsp; &nbsp; &nbsp; player.score+=1&nbsp; &nbsp; &nbsp; &nbsp; food=Food(random.randrange(20,width-20),random.randrange(20,height-20))&nbsp; &nbsp; &nbsp; &nbsp; all_sprites.add(food)&nbsp; &nbsp; all_sprites.update()&nbsp; &nbsp; shasha.fill(rashe_tox)&nbsp; &nbsp; all_sprites.draw( shasha )&nbsp; &nbsp; player._exit()&nbsp; &nbsp; for i in range(1,len(player.tail)):&nbsp; &nbsp; &nbsp; &nbsp; pygame.draw.rect(shasha,spy,(player.tail[i][0],player.tail[i][1],12,12))&nbsp; &nbsp; &nbsp; &nbsp; draw_txt(shasha,str(player.score),18,width/2,10)&nbsp; &nbsp; &nbsp; &nbsp; all_sprites.draw(shasha)&nbsp; &nbsp; &nbsp; &nbsp; pygame.display.flip()pygame.quit()通过这些更改,代码会生成某种游戏窗口。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python