【问题标题】:Kivy: How to automate clock processes one after the otherKivy:如何一个接一个地自动化时钟处理
【发布时间】:2018-09-02 20:17:37
【问题描述】:

正在编写俄罗斯方块游戏,并遇到了一些似乎无法克服的挑战。我希望积木系统地一个接一个地倒下,但似乎无法得到它,因为我一个接一个地遇到错误。这是我的错误服务代码

#All modules have been imported
class block1(Widget):

    def __init__(self, **kwargs):
        super(block1, self).__init__(**kwargs)
        xpositions = (0, 50, 100, 150, 200, 250, 300, 350, 400)
        self.bind(pos= self.fall)
        self.pos_x = random.choice(xpositions)
        self.pos_y = Window.height
        self.pos = (self.pos_x,self.pos_y)
        self.vel_x = 0
        self.vel_y = -5
        velocity = vel_x,vel_y

    def fall(self, **kwargs):
        self.pos =  Vector(*self.position) + self.pos
        if self.pos[1]==0:
            self.position[1] = 0
            return self.pos

    @classmethod
    def new_widget(cls):
        return cls
        #This widget is intended to help me create the new instance of a the 
same class i.e to multiply this block within my app
class block2(Widget):

    def __init__(self, **kwargs):
        super(block1, self).__init__(**kwargs)
        xpositions = (0, 50, 100, 150, 200, 250, 300, 350, 400)
        self.bind(pos= self.fall)
        self.pos_x = random.choice(xpositions)
        self.pos_y = Window.height
        self.pos = (self.pos_x,self.pos_y)
        self.vel_x = 0
        self.vel_y = -5
        velocity = vel_x,vel_y

    def fall(self, **kwargs):
        self.pos =  Vector(*self.position) + self.pos
        if self.pos[1]==0:
            self.position[1] = 0
            return self.pos
    @classmethod
    def new_widget(cls):
        return cls
    #This widget is intended to help me create the new instance of a the 
same class i.e to multiply this block within my app

我面临三个问题。第一个来自我的块类调用fall 下的函数,当它明显在我的__init__ 函数中时,我不断收到一个错误,上面写着block1 has no attribute called velocity

我定义了两个新的块类,为每个自定义了.kv 文件,为每个定义了不同的颜色和大小。每当创建类时,我都会编写代码来定义新的启动位置和固定速度。在此之后,我创建了 fall 类以使我的应用程序下降。 在构建我的应用程序类时,我尝试通过检测一个块何时掉落并开始下一个块来使块一个接一个地掉落。

def build(self):
    game= board()
    first = block1()
    second = block2()
    game.add_widget(first)
    Clock.schedule_interval(first.fall, 1/60)
    if first.pos[1] == 0:
        game.add_widget(second)
        Clock.schedule_interval(second.fall, 1/60)

其次,在我的 init 函数中,我尝试将 fall 函数绑定到类的 pos 属性,以便在块落下时,类的 pos 属性随之改变.不管绑定如何,程序似乎都没有检测到pos 的变化。

最后我尝试创建一个新的@classmethod,这将帮助我为俄罗斯方块应用程序反复无限地创建新块,但不知道我哪里弄错了。我创建了一个返回类的新实例的类方法,并计划创建一个循环,以这种方式不断创建一个类的新实例:

game = tetrisgame()#This is the main layout for the game
while game:#To create a loop to keep adding new blocks
        blockchoice = randint(1,6)
        if blockchoice == 1:
            game.add_widget(block1.new_widget)
            Clock.schedule_interval(block1.fall,1/60)
            for i in allblocks:
                if block1.collide_widget(i):
                    block1.position[1] = 0

这给了我一个绑定错误,说 widget.bind 错误,并且无法为我的类创建一个新实例。 有人可以帮我澄清一下吗? 注意:我试图挑选出导致错误的代码部分,以防止发布包含大量代码的帖子,因此请注意所有模块都已导入,.kv 文件包含所有设计被省略了。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    这里有很多问题。您的 block1 确实没有 velocity 属性。您的__init__() 中提到的velocity 只是一个局部变量。如果您希望它成为一个属性,则必须将其分配为self.velocity=。此外,对velx,vely 的引用并未引用属性self.velxself.vely

    您的 fall() 方法引用了我在任何地方都没有定义的 self.position

    我看不到您的小部件位置是如何更改的。也许您的fall() 方法中的那一行应该是:self.pos = Vector(*self.velocity) + self.pos

    Widgetpos 属性告诉Kivy 在哪里绘制它,所以我认为您不必担心将pos 绑定到任何东西,它将是Widget 的位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多