【问题标题】:How can I make the program restart in kivy?如何让程序在 kivy 中重新启动?
【发布时间】:2018-02-15 16:43:13
【问题描述】:

我是 kivy 的新手。我做了一个井字游戏,但是当其中一个玩家获胜时,我希望游戏重新开始,这样玩家就可以再玩一次。我怎样才能在 kivy 中做到这一点,或者我应该重置游戏所基于的按钮和列表?我已经尝试了很多事情,比如

self.clear_widgets()

但是没用

这是main.py

from kivy.app import App
from kivy.properties import OptionProperty, ObjectProperty
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout

class Option():
    p1 = []
    p2 = []
    activeplayer = 1

class TicTable(BoxLayout):
    pass


class EntryButton(Button):
    opt = Option()
    obj = ObjectProperty()
    a = ObjectProperty()

    def setButton(self, p):
        self.obj.text = p
        self.obj.disabled = True


    def show_winner(self, win_player):
        if win_player:
            popup = Popup(title="There is a Winner", content=Label(text=win_player), size_hint=(None, None), size=(200, 200))
            popup.open()

    def check_winner(self):
        p1_list = set(self.opt.p1)
        p2_list = set(self.opt.p2)
        winner = None
        winning = [{1, 2, 3}, {4, 5, 6}, {7, 8, 9},
                    {1, 4, 7}, {2, 5, 8}, {3, 6, 9}]
        for i in winning:
            if p1_list.intersection(i) == i:
                winner = "Player X is the Winner"
                self.show_winner(winner)
                break
            elif p2_list.intersection(i) == i:
                winner = "Player O is the Winner"
                self.show_winner(winner)
                break



    def play(self):
        if self.opt.activeplayer == 1:
            self.setButton("X")
            self.opt.p1.append(self.obj.n)
            self.check_winner()
            self.opt.activeplayer =2
        elif self.opt.activeplayer ==2:
            self.setButton("O")
            self.opt.p2.append(self.obj.n)
            self.check_winner()
            self.opt.activeplayer = 1



class TicTacToeApp(App):
    pass



if __name__ == '__main__':
    TicTacToeApp().run()

这是tictactoe.kv

<EntryButton>:
    obj: obj
    id: obj
    on_press: root.play()



<TicTable>:
    orientation: "vertical"
    BoxLayout:
        EntryButton:
            n:1
            text: ""
        EntryButton:
            n:2
            text: ""
        EntryButton:
            n:3
            text:""
    BoxLayout:
        EntryButton:
            n:4
            text: ""
        EntryButton:
            n:5
            text: ""
        EntryButton:
            n:6
            text: ""
    BoxLayout:
        EntryButton:
            n:7
            text: ""
        EntryButton:
            n:8
            text: ""
        EntryButton:
            n:9
            text: ""




TicTable:

【问题讨论】:

    标签: python python-3.x kivy kivy-language


    【解决方案1】:

    您的TicTacToeApp 应该具有返回小部件的build 方法。

    我会给你一个例子,它是一个测验应用程序,当按下正确的按钮(或释放,在 kivy lang 中)时,应用程序将更新它的测验。

        import random
        from kivy.app import App
        from kivy.uix.button import Button
        from kivy.uix.gridlayout import GridLayout
        from kivy.uix.label import Label
    
        Q_sets = ["1+1=...","1+6=...","77-43=..."];
        Opt_sets = [["2", "23"], ["4","7"], ["34","66"]];
        Ans_sets = ["2","7", "34"];
    
        class Option(Button):
            def __init__(self, label):
               super().__init__(self);
               self.text = label;
            def on_release(self):
               super().on_release(self);
               if self.text == self.parent.answer:
                   self.parent.parent.clear_widgets();
                   index = int(random.uniform(0, 3));
                   New = Quiz(Q_sets[index], Opt_sets[index][0], Opt_sets[index][1], Ans_sets[index]);      
                   self.parent.parent.add_widget(New);
    
    
        class Quiz(GridLayout):
            def __init__(self, question, opt1, opt2, correct):
               super().__init__(self, rows=3, cols=1);
               self.question_label = Label(text=question);
               self.opt1_button = Option(label=opt1);
               self.opt2_button = Option(label=opt2);
               self.answer=correct;
               self.add_widget(self.question_label);
               self.add_widget(self.opt1_button);
               self.add_widget(self.opt2_button);
    
        class QuizzesApp(App):
            def build(self):
                Container = GridLayout(); 
                Container.add_widget(Quiz(Q_sets[0], Opt_sets[0][0], Opt_sets[0][1], Ans_sets[0]));  
                return Container    
    

    你可以研究这个,然后为你自己的情况即兴创作。这样可以吗?

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多