Reputation: 71
So i have just learned how to use classes and i made this class but i can't make it to run i use
spacecraft1 = spacecraft(500,500,100,34)
to make the class
then on loop i use spacecraft1.motion()
to update / move. From all of this i get error:
Traceback (most recent call last):
File "C:\Users\Redstone3\Desktop\Desctop\Centers\game5\gameBuild 0.04.py", line 107, in <module>
spacecraft1 = spacecraft(500,500,100,34)
TypeError: 'pygame.Surface' object is not callable
and is it possible to create like 40 of these spaceships at same time without defining everyone of them like
spacecraft1 = spacecraft(500,500,100,34)
spacecraft2 = spacecraft(500,500,100,34)
spacecraft3 = spacecraft(500,500,100,34)
spacecr...
Here is my class if it helps something. And this class and the main gamefile are in seperate files but in same folders.
import pygame
import random
class BaseClass(pygame.sprite.Sprite):
allsprites = pygame.sprite.Group()
def __init__(self, x, y, width, height,):
pygame.sprite.Sprite.__init__(self)
BaseClass.allsprites.add(self)
self.shipDefaultUp = pygame.image.load("textures/ships/shipDefault/shipUP.png")
self.shipDefaultRight = pygame.image.load("textures/ships/shipDefault/shipRIGHT.png")
self.shipDefaultDown = pygame.image.load("textures/ships/shipDefault/shipDOWN.png")
self.shipDefaultLeft = pygame.image.load("textures/ships/shipDefault/shipLEFT.png")
self.shipCargo1Up = pygame.image.load("textures/ships/shipCargo1/shipCargo1UP.png")
self.shipCargo1Right = pygame.image.load("textures/ships/shipCargo1/shipCargo1RIGHT.png")
self.shipCargo1Down = pygame.image.load("textures/ships/shipCargo1/shipCargo1DOWN.png")
self.shipCargo1Left = pygame.image.load("textures/ships/shipCargo1/shipCargo1LEFT.png")
self.shipCargo2Up = pygame.image.load("textures/ships/shipCargo2/shipCargo2UP.png")
self.shipCargo2Right = pygame.image.load("textures/ships/shipCargo2/shipCargo2RIGHT.png")
self.shipCargo2Down = pygame.image.load("textures/ships/shipCargo2/shipCargo2DOWN.png")
self.shipCargo2Left = pygame.image.load("textures/ships/shipCargo2/shipCargo2LEFT.png")
self.image = pygame.image.load("textures/ships/shipDefault/shipUP.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.width = width
self.height = height
#self.dirrection = random.randrange(1,5)
self.dirrection = 1
self.timer = random.randrange(10,50)
self.speed = random.randrange(2,10)
self.shiptype = 3#random.randrange(1,3)
#shiptypes#
#1 = shipDefault
#2 = shipCargo1
#3 = shipCargo2
self.move1 = ((1),(2),(4))
self.move2 = ((1),(2),(3))
self.move3 = ((2),(3),(4))
self.move4 = ((1),(3),(4))
class spacecraft(BaseClass):
ships = pygame.sprite.Group()
def __init__(self, x, y, width, height,):
BaseClass.__init__(self, x, y, width, height,)
spacecraft.ships.add(self)
def motion(self):
#1 = UP
#2 = RIGHT
#3 = DOWN
#4 = LEFT
self.timer -=1
if self.dirrection == 1: ##############SHIP UP
self.rect.y -=self.speed
if self.shiptype == 1:
self.image = self.shipDefaultUp
if self.shiptype == 2:
self.image = self.shipCargo1Up
if self.dirrection == 2:################SHIP RIGHT
self.rect.x +=self.speed
if self.shiptype == 1:
self.image = self.shipDefaultRight
if self.shiptype == 2:
self.image = self.shipCargo1Right
if self.shiptype == 3:
self.image = self.shipCargo2Right
if self.dirrection == 3: ##############SHIP DOWN
self.rect.y +=self.speed
if self.shiptype == 1:
self.image = self.shipDefaultDown
if self.shiptype == 2:
self.image = self.shipCargo1Down
if self.shiptype == 3:
self.image = self.shipCargo2Down
if self.dirrection == 4: ################SHIP LEFT
self.rect.x -=self.speed
if self.shiptype == 1:
self.image = self.shipDefaultLeft
if self.shiptype == 2:
self.image = self.shipCargo1Left
if self.shiptype == 3:
self.image = self.shipCargo2Left
if self.dirrection == 5: ####IF SHIP IS NOT MOVING####
print("loitter")
if self.timer < 0:####GET NEW DIRECTION FOR SHIP####
self.dirrection = random.randrange(1,6)
self.timer = random.randrange(10,50)
Upvotes: 0
Views: 43
Reputation: 1880
Given that the error says "surface object not callable" that means at some point in your function where you think you are instantiating an instance of spacecraft, you have previously created a local variable containing a pygame.Surface object that overrides the class "spacecraft" from the global scope. Your class is not where the problem is, it's the previous code in the function where you're trying to instantiate it.
e.g.
class spacecraft:
...
def someOtherFunction:
...
# this valid line of code is actually where the problem
# is as it hides your spacecraft class with a local variable instead.
spacecraft = some pygame.Surface instance
...
spacecraft1 = spacecraft(...)
...
Upvotes: 1