所以我刚刚学会了如何使用类,我创建了这个类,但是我不能让它运行我使用
spacecraft1 = spacecraft(500,500,100,34)
上课
然后在循环中我使用spacecraft1.motion()
来更新/移动。从这一切我得到错误:
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
并且可以同时创建40个这样的宇宙飞船,而不是像
那样定义它们的每个人spacecraft1 = spacecraft(500,500,100,34)
spacecraft2 = spacecraft(500,500,100,34)
spacecraft3 = spacecraft(500,500,100,34)
spacecr...
这是我的课程,如果它有所帮助。此类和主游戏文件位于单独的文件中,但位于相同的文件夹中。
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)
答案 0 :(得分:1)
鉴于错误显示“表面对象不可调用”,这意味着在您的函数中某个位置,您认为您正在实例化航天器的实例,您之前已经创建了一个包含覆盖该类的pygame.Surface对象的局部变量来自全球范围的“航天器”。你的类不是问题所在,它是你试图实例化它的函数中的前一个代码。
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(...)
...