Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Pygame在MVC模式中处理动画_Python_Model View Controller_Pygame - Fatal编程技术网

Python 使用Pygame在MVC模式中处理动画

Python 使用Pygame在MVC模式中处理动画,python,model-view-controller,pygame,Python,Model View Controller,Pygame,我目前正在尝试使用Python和Pygame实现MVC模式,但我不知道如何正确处理动画。假设我们有一个可以攻击的模型对象: class ModelObject: def attack(): if not self.attacking: self.attacking = True # Then compute some stuff def isattacking(): return self.atta

我目前正在尝试使用Python和Pygame实现MVC模式,但我不知道如何正确处理动画。假设我们有一个可以攻击的模型对象:

class ModelObject:
    def attack():
        if not self.attacking:
            self.attacking = True
            # Then compute some stuff

    def isattacking():
        return self.attacking
以及通过显示攻击动画来渲染模型对象的视图对象:

class ViewObject(pygame.Sprite):

    attacking_ressource = [] # List of surfaces for animation
    default_ressource = None

    def update():
        # Detect the model object is attacking
        if self.model.isattacking():
            # Create animation if needed
            if self.attacking_animation is None:
                self.attacking_animation = iter(self.attacking_ressource)
            # Set image
            self.image = next(self.attacking_animation, self.default_ressource)
        else:
            # Reset animation
            self.attacking_animation = None
            self.image = self.default_ressource
问题是,模型如何知道它不再攻击?
该视图可以在动画结束时通知模型,但它猜测这不是MVC模式应该如何工作的。或者,可以在模型中设置动画计数器,但它似乎也不正确。

我想你搞错了

攻击的持续时间不应与动画的持续时间相同,但动画的持续时间应与攻击的持续时间相同

因此,
ViewObject
很好,因为它已经询问模型是否仍在攻击


至于
ModelObject
,攻击应该持续多长时间以及如何跟踪时间取决于您。例如,您可以调用
pygame.time.get_ticks()
以获取自您在
攻击中启动游戏一次以来的毫秒数,然后定期再次检查,如:

class ModelObject:
    def attack():
        if not self.attacking:
            self.attacking = True
            self.started = pygame.time.get_ticks()
            # Then compute some stuff

    def isattacking():
        return self.attacking

    def update():
        # attack lasts 1000ms
        self.attacking &= pygame.time.get_ticks() - self.started < 1000
类模型对象:
def攻击():
如果不是自我攻击:
自我攻击=正确
self.start=pygame.time.get_ticks()
#然后计算一些东西
def isattacking():
反击
def update():
#攻击持续1000毫秒
self.attacking&=pygame.time.get_ticks()-self.start<1000

动画完成后,是否需要告知模型?如果对视图“发射并忘记”一个
攻击
命令,视图只会忽略来自该模型的进一步命令,直到攻击完成。我想了一下,但是如果模型对象在攻击时不应该做任何其他事情,比如在动画中攻击两次,该怎么办?这是一个很好的观点。我认为这个问题可能更适合哼哼,这是有道理的!所以我想我应该在模型中为攻击定义一些记号,以便视图可以使用该值来调整动画(复制或传递一些图像),以匹配模型攻击持续时间,对吗?