[英]How can I make the program restart in kivy?
I am a newbie in kivy. 我是猕猴桃的新手。 I made a tic tac toe game, but when one of the player wins I want the game to restart so, the players can play it again.
我做了一个井字游戏,但是当其中一位玩家获胜时,我希望游戏重新开始,以便玩家可以再次玩。 How can I make this in kivy or should I reset the buttons and lists that the game based on?
我该如何在游戏中表现出色?还是应该重设游戏所基于的按钮和列表? I have tried many things like
我尝试过很多类似的事情
self.clear_widgets()
but it didn't work 但这没用
this is the main.py
这是
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()
and this is the tictactoe.kv 这是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:
Your TicTacToeApp
should have the build
method that returns a widget. 您的
TicTacToeApp
应该具有返回小部件的build
方法。
I will give you an example, where it is a quiz app, when the correct button is pressed (or released, in kivy lang), the app will update its quiz. 我将举一个例子,它是一个测验应用程序,当按下正确的按钮(或以奇异果语发布)时,该应用程序将更新其测验。
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
You may study this, and then improvise for your own case. 您可以研究一下,然后根据自己的情况即兴创作。 Is this okay?
这个可以吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.