2017-01-01 76 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