2017-01-01 39 views
0

我是新來的python/pygame類,我正在做一個遊戲。 所以我的問題是,我有一個班,繪製太空船屏幕,但我需要以某種方式添加spacemove_x,spacemove_y到他們的X和Y.我怎樣才能不斷更新變量從外面的類?

這些spacemove_x,spacemove_y當用戶移動他的自己的飛船被定義

(我用spacemove_x,spacemove_y用於更新其他的東西的地方太像星星,因爲這個遊戲是從上往下看,所以當我提出我的飛船其他的事情應該取決於我的角色正在朝哪個方向前進)

所以spacemove_x,spacemove_y總是更新它自我,但我怎麼能給這個信息類?

CODE:

import pygame 
import random 

class BaseClass(pygame.sprite.Sprite): 

allsprites = pygame.sprite.Group() 
def __init__(self, x, y, width, height,spacemove_x,spacemove_y): 
    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.spacemove_x = spacemove_x 
    self.spacemove_y = spacemove_y 

    #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 spacecraftBOT(BaseClass): 
    ships = pygame.sprite.Group() 

    def __init__(self, x, y, width, height,spacemove_x,spacemove_y): 
     BaseClass.__init__(self, x, y, width, height,spacemove_x,spacemove_y) 
     spacecraftBOT.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 
      self.rect.y -=self.spacemove_x 

      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 
      self.rect.x +=self.spacemove_x 
      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 
      self.rect.y +=self.spacemove_y 

      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 
      self.rect.x -=self.spacemove_x 

      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: 
      print("loitter") 

     if self.timer < 0: 
      self.dirrection = random.randrange(1,6) 
      self.timer = random.randrange(10,50) 

這是我如何創建宇宙飛船到我的遊戲:

spacecraft1 = spacecraftBOT(500,500,100,34,spacemove_x,spacemove_y) 

那麼,如何給這個類更新spacemove_x,spacemove_y

+0

我對Python不太熟悉,但是可以在'class spacecraftBOT'中添加另一個方法,就像'def update_spacemove(self,x,y)'等等,並且調用它沿着'spacecraft1.update_spacemove(1,2)'的方向行事? –

+0

但是,這會有x和y的問題,因爲如果我需要說 '宇宙飛船1。update_spacemove(1,2)'wouldnt我需要redifine x,y然後船會去那些x和y? – cmdtvt

+0

您可能必須更改變量名稱以符合代碼。另外,參數名不必與類中的變量相同/不同,但選擇合適的名稱是個好主意。 –

回答

2

那麼我該如何給這個班級更新spacemove_x,spacemove_y

只是賦予它們應該工作:

spacecraft1.spacemove_x = 100 
spacecraft1.spacemove_y = 200 

BTW,spacecraft1spacecraftBOT類的實例,而不是一個類。

+0

這符合分離關注原則嗎? –

+0

這似乎工作正常,它也很簡單allsoso – cmdtvt

0

您可以使用點運算符訪問實例成員。事實上,你已經多次用self來做這件事。名稱self並不特別;它只是一個引用類實例的變量。同樣,spacecraft1是指一個類實例的變量。這意味着您可以使用完全相同的符號:你想

spacecraft1.spacemove_x = 100 

spacecraft1.spacemove_x += 50 

或者基本上什麼。

或者,您也可以定義move()方法:

class spacecraftBOT(BaseClass): 
    # ... all of the code you already have ... 

    def move(self, x, y): 
     self.spacemove_x = x 
     self.spacemove_y = y 

現在你可以調用該方法類似

spaceship1.move(100, 50) 

注意,這直接跳轉到指定的位置。如果你想增加x和y座標,你只需要實現正確的邏輯。 (提示:使用+=而不是=。)

+0

我認爲我需要使用這個'def move()',當我想要更多的航天器。它使更新更容易。 – cmdtvt