TypeError:敌人()不带参数

问题描述 投票:-2回答:1

我正在pygame上做游戏,我遇到了一个问题,我试图找出我面临的问题。而且我不知道解决方案。我收到以下错误消息:

shark = enemy(-389,410,170,71,-389, 1360)
TypeError: enemy() takes no arguments

有人知道错误在哪里吗? 。

这里是代码

def __init__(self, x, y, width, height, start ,end):
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.start = start
    self.end = end
    self.walkCount = 0
    self.vel = 7

def draw(self, surface):
    self.move()
    if self.walkCount +1 >=57:
        self.walkCount = 0

        if self.vel > 0:
            surface.blit(pygame.transform.scale(self.walkRight[self.walkCount//3,], (self.width, self.height)),(self.x, self.y))
            self.walkCount +=1

    else:
        surface.blit(pygame.transform.scale(self.walkLeft[self.walkCount//3,], (self.width, self.height)),(self.x, self.y))
        self.walkCount += 1

def move(self): #self
    if self.vel > 0:
        if self.x < self.end + self.vel:
            self.x += self.vel
        else:
            self.vel = self.vel * -1
            self.x += self.vel
            self.walkCount = 0

 #mainloop
fish = player(300,410,64,64)
shark = enemy(-389,410,170,71,-389, 1360)
shark1= enemy(900,180, 170,71 ,-120, 1360)
shark2 = enemy(600,300, 170,71 ,-300, 1360)
shark3= enemy(300,550, 170,71 ,-500, 1360)

def redrawwindow():
            global fish
            surface.blit (poisson, (0,0))#dessiner le charactere sur la fenetre
            fish.draw(surface)
            shark.draw(surface)
            shark1.draw(surface)
            shark2.draw(surface)
            shark3.draw(surface)
            global vie


if(shark.vel > 0 and fish.x < shark.x + shark.width and fish.x>=shark.x and fish.y <= shark.y + shark.height and fish.y >= shark.y):
   vie= vie-1

   fish = player(0, 0, 64, 64)

elif(shark.vel < 0 and  fish.x+fish.width > shark.x and fish.x<=shark.x and fish.y<= shark.y + shark.height and fish.y>= shark.y):
    vie= vie-1
    fish = player(0, 0, 64, 64)

if vie ==0:
    print ("Game Over")

pygame.display.update()`

python error-handling pygame pycharm computer-science
1个回答
0
投票

敌人的等级没有定义。您必须将所有代码放入类定义中,如下所示:

class enemy:
   #all your code

然后调用此

fish = player(300,410,64,64)
shark = enemy(-389,410,170,71,-389, 1360)
shark1= enemy(900,180, 170,71 ,-120, 1360)
shark2 = enemy(600,300, 170,71 ,-300, 1360)
shark3= enemy(300,550, 170,71 ,-500, 1360)

类定义之外。

© www.soinside.com 2019 - 2024. All rights reserved.