Python 2.7 在更新方法中使用逻辑

Python 2.7 在更新方法中使用逻辑,python-2.7,pygame,Python 2.7,Pygame,我正在和一些同学做一个小游戏,我想要一些关于如何处理这个问题的指导 在我的代码中,归根结底,我想调用Boss_Shoot类,其中Boss类update()开始打印“应该射击”。。打印语句只不过是“某种占位符…” 一如既往,非常感谢 class Boss(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load("sp

我正在和一些同学做一个小游戏,我想要一些关于如何处理这个问题的指导

在我的代码中,归根结底,我想调用Boss_Shoot类,其中Boss类update()开始打印“应该射击”。。打印语句只不过是“某种占位符…”

一如既往,非常感谢

class Boss(pygame.sprite.Sprite):
  def __init__(self):
    pygame.sprite.Sprite.__init__(self)
    self.image = pygame.image.load("sprites/boss.png")
    self.rect = self.image.get_rect()
    self.dx = 1
    self.dy = 1
    self.shoot= True

 def update(self):

    if self.rect.centerx >= 600:
        self.rect.centerx -= self.dx
    elif self.rect.centerx <= 600:

        print "should be shooting"
    self.rect.centery -= self.dy
    self.checkBounds()

def checkBounds(self):
    if self.rect.top <= 0:
        self.dy *= -1
        print 
    if self.rect.bottom >= 500:
        self.dy *= -1

class Boss_Shoot(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface((20, 20))
        self.image.fill((255, 0, 0))
        self.rect = self.image.get_rect()
        self.rect.center = (320, 240)
        self.dy =2

    def update(self):
        if self.shoot == True:
            print "shooting"
        for i in range(100):   
            print "wtf man"
            self.rect.x = random.randrange(1000, 3000)
            self.rect.y = random. randrange(10, 490)
            self.rect.x -= 5
            self.rect.y +=self.dy
class Boss(pygame.sprite.sprite):
定义初始化(自):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
self.image=pygame.image.load(“sprites/boss.png”)
self.rect=self.image.get_rect()
self.dx=1
self.dy=1
self.shot=True
def更新(自我):
如果self.rect.centerx>=600:
self.rect.centerx-=self.dx

elif self.rect.centerx您需要一种方式,让您的演员(或精灵,或任何您称之为“实体”)能够与游戏的全局状态进行沟通

您没有显示所有代码,因此我假设所有内容都在一个文件中

在代码的某个地方,您可能有一个所有参与者的列表(如果没有,您应该创建一个)

actors = []
也许您使用一个列表,也许您想使用pygame的

我进一步假设您在主循环中的所有参与者上调用
update()
方法,如:

for a in actors:
    a.update()
(或者,如果您使用pygame的
Group
类,类似于
mygroup.update()

现在,在
Boss
update()

def update(self):
    if self.rect.centerx >= 600:
        self.rect.centerx -= self.dx
    elif self.rect.centerx <= 600:
        actors.append(Boss_Shoot())

你会明白的。

我看到奇怪的事情发生了
请具体一点。如果我们必须在你已经知道我在泛泛而谈的情况下尝试找出“可能”出了什么问题,那就是浪费时间。对不起……我想用我的特定代码将ShootBoss类称为我的Boss类中的构造函数语句,并回复在该构造函数中使用print语句“应该射击”。我想使用boss\u shoot=bosssshoot.update(),但它不起作用。因此我尝试创建构造函数bosssshot=boss\u shoot()但这不起作用。最后,我没有在Boss_shot.update或Boss_shot中出错。但是它不起作用。所以我想我有更大的问题
class State(object):
    def __init__(self):
        self._actors = []

    def register(self, actor):
        self._actors.append(actor)

    def unregister(self, actor):
        self._actors.remove(actor)

    def update(self):
        for a in self._actors:
            a.update()
    ...

class Boss(pygame.sprite.Sprite):
    def __init__(self, state):
        self.state = state
        self.state.register(self)
        ...

    def update(self):
        if self.rect.centerx >= 600:
            self.rect.centerx -= self.dx
        elif self.rect.centerx <= 600:
            self.state.register(Boss_Shoot(self.state))

class Boss_Shoot(pygame.sprite.Sprite):
    def __init__(self, state):
        self.state = state
        self.state.register(self)
        ...

while True:
    state.update()
    state.draw(screen)
    ...