如何在kivy中重启程序?

时间:2018-02-15 16:43:14

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

我是kivy的新手。我做了一个tic tac toe游戏,但当其中一个玩家获胜时我想让游戏重新启动,玩家可以再玩一次。我怎么能在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:

1 个答案:

答案 0 :(得分:0)

您的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    

你可以研究这个,然后即兴发挥你自己的情况。这没关系吗?