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

时间:2019-03-26 00:08:31

标签: python python-3.x pygame

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

Full code here

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:

给出错误消息(来自image的OCR):

pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
Traceback (most recent call last):
File "C:\Users\ACER\Desktop\untitled1\venv\lLib\site-packages\pygame\sprite.py", Line 365, in add
self.add(*sprite)
TypeError: add() argument after * must be an iterable, not type

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\_pydev_bundle\pydev_umd.py", Line 197,
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2018.2.4\helpers\pydev\_pydev_imps\ pydev_execfile.py", line 18
exec(compile(contents+"\n", file, ‘exec'), glob, loc)
File "C:/Users/ACER/Desktop/untitledi1/snake.py", Line 90, in <module>
all_sprites.add(player)
File "C:\Users\ACER\Desktop\untitled1\venv\lib\site-packages\pygame\sprite.py", Line 378, in add
sprite.add_internal(self)
TypeError: add_internal() missing 1 required positional argument: ‘group'

Process finished with exit code 0

1 个答案:

答案 0 :(得分:1)

创建python对象时,需要给实例化加上括号,例如:

class Colour:
    def __init__( self ):
        self.red   = 0
        self.green = 0
        self.blue  = 0

light_red = Colour    # <-- Wrong
dark_red  = Colour()  # <-- Correct

这种类型的错误发生在几个不同的实例中。引起该错误的原因是您的all_sprites子画面组不是子画面组的“副本”,而是对对象定义的引用。对于时钟对象sa3at同样。

也:有一个错字:fill不是dill;并且all_sprites组从未被绘制到屏幕上。

import pygame
import random
import sys
pygame.init()

width=600
height=400

#rangakan
spy=(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=True

def dist(x1,y1,x2,y2):
    return ((x2-x1)**2 + (y2-y1)**2)**1/2

def draw_txt(surf,text,size,x,y):
    font_name = pygame.font.match_font("arial")
    font = pygame.font.Font(font_name,size)
    text_surface = font.render(text,True,spy)
    text_rect=text_surface.get_rect()
    text_rect.midtop=(x,y)
    surf.blit(text_surface,text_rect)

class Snake(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((12,12))
        self.image.fill(spy)
        self.rect=self.image.get_rect()
        self.rect.center=(100,100)
        self.speedx=0
        self.speedy=0
        self.score=0
        self.tail=[]

    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:
                sys.exit()
class Food(pygame.sprite.Sprite):
    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)
        self.image=pygame.Surface((12,12))
        self.image.fill(swr)
        self.rect=self.image.get_rect()
        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:
    sa3at.tick(26)
    keys_pressed=pygame.event.get()
    for i in keys_pressed:
        if i.type==pygame.QUIT:
            runnung=False
    if pygame.sprite.collide_rect(player,food):
        food.kill()
        player.score+=1
        food=Food(random.randrange(20,width-20),random.randrange(20,height-20))
        all_sprites.add(food)
    all_sprites.update()
    shasha.fill(rashe_tox)
    all_sprites.draw( shasha )
    player._exit()
    for i in range(1,len(player.tail)):
        pygame.draw.rect(shasha,spy,(player.tail[i][0],player.tail[i][1],12,12))
        draw_txt(shasha,str(player.score),18,width/2,10)
        all_sprites.draw(shasha)
        pygame.display.flip()
pygame.quit()

通过这些更改,代码将产生某种游戏窗口。