【发布时间】: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