-2

I'm doing a game on pygame and i'm facing a problem and i'm trying to figure out the problem i'm facing. and i dont know the solution. I'm getting following error msg:

I'm getting following error msg:

shark = enemy(-389,410,170,71,-389, 1360) TypeError: enemy() takes no arguments does anyone know where is the error? .

class enemy (object):
    walkRight= [pygame.image.load('shark\R\R1.png').convert()
    pygame.image.load('shark\R\R2.png').convert()
    pygame.image.load('shark\R\R3.png').convert()
    pygame.image.load('shark\R\R4.png').convert()
    pygame.image.load('shark\R\R5.png').convert()
    pygame.image.load('shark\R\R6.png').convert()
    pygame.image.load('shark\R\R7.png').convert()
    pygame.image.load('shark\R\R8.png').convert()
    pygame.image.load('shark\R\R9.png').convert()
    pygame.image.load('shark\R\R10.png').convert()
    pygame.image.load('shark\R\R11.png').convert()
    pygame.image.load('shark\R\R12.png').convert()
    pygame.image.load('shark\R\R13.png').convert()
    pygame.image.load('shark\R\R14.png').convert()
    pygame.image.load('shark\R\R15.png').convert()
    pygame.image.load('shark\R\R16.png').convert()
    pygame.image.load('shark\R\R17.png').convert()
    pygame.image.load('shark\R\R18.png').convert()
    pygame.image.load('shark\R\R19.png').convert()]

walkLeft = [pygame.image.load('shark\L\L1.png').convert()
pygame.image.load('shark\L\L2.png').convert()
pygame.image.load('shark\L\L3.png').convert()
pygame.image.load('shark\L\L4.png').convert()
pygame.image.load('shark\L\L5.png').convert()
pygame.image.load('shark\L\L6.png').convert()
pygame.image.load('shark\L\L7.png').convert()
pygame.image.load('shark\L\L8.png').convert()
pygame.image.load('shark\L\L9.png').convert()
pygame.image.load('shark\L\L10.png').convert()
pygame.image.load('shark\L\L11.png').convert()
pygame.image.load('shark\L\L12.png').convert()
pygame.image.load('shark\L\L13.png').convert()
pygame.image.load('shark\L\L14.png').convert()
pygame.image.load('shark\L\L15.png').convert()
pygame.image.load('shark\L\L16.png').convert()
pygame.image.load('shark\L\L17.png').convert()
pygame.image.load('shark\L\L18.png').convert()
pygame.image.load('shark\L\L19.png').convert()]

def __init__(self, x, y, width, hight, 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):
    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


def redrawwindow():
    global fish
    surface.blit (poisson, (0,0))
    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()

 #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)`

I'm getting another error:

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

Does anyone knows where is the error?

Pedram Parsian
  • 3,750
  • 3
  • 19
  • 34
  • 1
    Please reformat your question to write the code as actual code. Use the brackets at the top of the interface. – InstaK0 Jun 01 '20 at 22:45
  • The error says that `shark` is not defined. In the code you have included, I do not see anywhere that you do that. I see `fish = player(...)` but I do not see a `shark = ...` anywhere. Nor a shark1, shark2 or shark3, though you use them in the `redrawwindow()`. – Glenn Mackintosh Jun 01 '20 at 22:55
  • i have puted all the code of the enemy so you can see it all – Yara Beheiry Jun 01 '20 at 23:11

1 Answers1

0

These lines:

 #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)

where you assign/define these various objects, are at the end of your program. That is AFTER you have tried to use them in the lines above.

You need these lines earlier, probably just before def redrawwindow(): would be fine.

Two other things though. I am not sure if it is a copy paste issue, but your indenting in the enemy class is wrong. Also I am pretty sure that you intend all those loads to be part of a list assignment. Like this:

walkRight= [pygame.image.load('shark\R\R1.png').convert(),
    pygame.image.load('shark\R\R2.png').convert(),
    pygame.image.load('shark\R\R3.png').convert(),
    pygame.image.load('shark\R\R4.png').convert(),
    pygame.image.load('shark\R\R5.png').convert(),
    pygame.image.load('shark\R\R6.png').convert(),
    pygame.image.load('shark\R\R7.png').convert(),
    pygame.image.load('shark\R\R8.png').convert(),
    pygame.image.load('shark\R\R9.png').convert(),
    pygame.image.load('shark\R\R10.png').convert(),
    pygame.image.load('shark\R\R11.png').convert(),
    pygame.image.load('shark\R\R12.png').convert(),
    pygame.image.load('shark\R\R13.png').convert(),
    pygame.image.load('shark\R\R14.png').convert(),
    pygame.image.load('shark\R\R15.png').convert(),
    pygame.image.load('shark\R\R16.png').convert(),
    pygame.image.load('shark\R\R17.png').convert(),
    pygame.image.load('shark\R\R18.png').convert(),
    pygame.image.load('shark\R\R19.png').convert()]

walkLeft =  pygame.image.load('shark\L\L1.png').convert(),
    pygame.image.load('shark\L\L2.png').convert(),
    pygame.image.load('shark\L\L3.png').convert(),
    pygame.image.load('shark\L\L4.png').convert(),
    pygame.image.load('shark\L\L5.png').convert(),
    pygame.image.load('shark\L\L6.png').convert(),
    pygame.image.load('shark\L\L7.png').convert(),
    pygame.image.load('shark\L\L8.png').convert(),
    pygame.image.load('shark\L\L9.png').convert(),
    pygame.image.load('shark\L\L10.png').convert(),
    pygame.image.load('shark\L\L11.png').convert(),
    pygame.image.load('shark\L\L12.png').convert(),
    pygame.image.load('shark\L\L13.png').convert(),
    pygame.image.load('shark\L\L14.png').convert(),
    pygame.image.load('shark\L\L15.png').convert(),
    pygame.image.load('shark\L\L16.png').convert(),
    pygame.image.load('shark\L\L17.png').convert(),
    pygame.image.load('shark\L\L18.png').convert(),
    pygame.image.load('shark\L\L19.png').convert()]

Not just loaded and then having the result discarded.

Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
  • oh yes thank you so much i didn't notice it.but now when i change it, there is another error. why should i remove the argument or what should i remove exactly . Traceback (most recent call last): shark= enemy(-389,410,170,71,-389, 1360) TypeError: enemy() takes no arguments – Yara Beheiry Jun 01 '20 at 23:35
  • @YaraBeheiry That could be a result of your indenting not being correct. As I said it appears your indenting in the enemy class is wrong. It appears that the __init__(), draw() and move() are out-dented and therefor not part of the enemy class. If that __init__() is not part of the class it will use the default __init__() from the `object` base class. The __init__() from the object class takes no arguments. That is likely your issue. Fix the indenting and it will likely work. – Glenn Mackintosh Jun 02 '20 at 01:03
  • i change it and it gave me the same error. i change it like this: def __init__(): def draw(): def move(): i cant understand why – Yara Beheiry Jun 02 '20 at 08:45
  • @YaraBeheiry Can you edit your question to include you new code? Do not replace the old code just at the end add a comment saying something like **EDIT** including updated code and then paste in the new code? To make sure it displays correctly make sure that after you paste the code, you highlight it all and them mark it as a code block by clicking the `{}` button at the top of the edit window. Then double check that the formatting and indenting look the way you expect. Also copy and paste the full error message that it gives. – Glenn Mackintosh Jun 02 '20 at 15:47