所以我剛剛學會了如何使用類和我做了這個類,但我不能讓它跑我使用類提供一個錯誤,但沒有運行?
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)
對於簡單的問題:您可以使用'list'來創建一堆飛船並將它們全部存儲在一個變量中。 –
爲類使用'CamelCase'名稱 - '類Spacecraft(BaseClass):' - 它使代碼更具可讀性。 – furas
怎麼樣? 'epicList = []'然後呢?我需要使用類似'spacecraft1.append?'的東西嗎? – cmdtvt