【发布时间】:2019-08-20 10:34:37
【问题描述】:
我正在尝试使用 Kivy 语言编写类似于谁想成为百万富翁的游戏。我对小部件的动态功能有疑问。
我想要做的是当用户单击选项按钮以选择答案时,该按钮会保持向下状态一段时间,然后根据正确答案切换颜色。然后同一个屏幕应该更新并给我新的问题,所有颜色都已重置。这是我正在处理的代码的相关部分。忽略我有两个问题生成器这一事实,一旦我确定这个问题生成器正常工作,我将切换到随机问题生成器。
class PlayScreen(Screen):
question = StringProperty()
question_id = StringProperty()
option_a = StringProperty()
option_b = StringProperty()
option_c = StringProperty()
option_d = StringProperty()
def __init__(self, **kwargs):
super(PlayScreen, self).__init__(**kwargs)
self.Questions = dict([('id12345', "What is the capital city of Azerbaijan"), ('idd5', "What is the capital city of Turkey")])
self.Answer_Set = dict([('id12345', ["Ganja", "Baku", "Lankaran", "Ghakh"]), ('idd5', ["Istanbul", "Tiflis", "Ankara", "Izmir"])])
self.Correct_Answers = dict([('id12345', "Baku"), ('idd5', "Ankara")])
self.Question_IDs = list(self.Questions.keys())
self.question_builder()
def question_builder(self):
self.question_id =self.Question_IDs[0] #randint(0, len(self.Questions))
self.question = self.Questions[self.question_id]
self.option_a = self.Answer_Set[self.question_id][0]
self.option_b = self.Answer_Set[self.question_id][1]
self.option_c = self.Answer_Set[self.question_id][2]
self.option_d = self.Answer_Set[self.question_id][3]
return self.question
def question_builder_2(self):
self.question_id =self.Question_IDs[1] #randint(0, len(self.Questions))
self.question = self.Questions[self.question_id]
self.option_a = self.Answer_Set[self.question_id][0]
self.option_b = self.Answer_Set[self.question_id][1]
self.option_c = self.Answer_Set[self.question_id][2]
self.option_d = self.Answer_Set[self.question_id][3]
return self.question
def checker(self, instance, question_id):
if instance.text == self.Correct_Answers[question_id]:
return True
else:
return False
Kivy_language_body = """
<PlayScreen>:
name: "play"
GridLayout:
question: root.question
question_id: root.question_id
option_a: root.option_a
option_b: root.option_b
option_c: root.option_c
option_d: root.option_d
default_color: root.default_color
id: PlayScreenGrid
cols:1
rows: 2
GridLayout:
id: q
cols: 1
rows: 1
size: root.width, root.height/2
pos_hint: {"center_x":0.5, "top":1}
canvas.before:
Color:
rgb: 1,0,0
Rectangle:
pos: self.pos
size: self.size
Label:
text: PlayScreenGrid.question
font_size: 20
GridLayout:
cols: 2
rows: 2
size: root.width, root.height/2
pos_hint: {"y":0}
Button:
id: a
text: root.option_a
font_size: 20
min_state_time: 4
on_release:
a.background_color = 1, 1, 1, 1
self.trigger_action(duration=5)
self.state = "down"
print (self.state)
self.state = "normal"
print(self.state)
if root.checker(a, root.question_id): a.background_color = 1,1,0,1
else: a.background_color = 100,0,0,0.5
self.parent._trigger_layout()
root.question_builder_2()
Button:
id: b
text: root.option_b
font_size: 20
on_release:
self.state = "down"
self.state = app.timer()
if root.checker(b, root.question_id): b.background_color = 1,1,0,1
else: b.background_color = 100,0,0,0.5
Button:
id: c
text: root.option_c
font_size: 20
on_release:
self.state = "down"
app.timer()
self.state = "normal"
if root.checker(c, root.question_id): c.background_color = 1,1,0,1
else: c.background_color = 100,0,0,0.5
app.timer()
c.background_color = 1,1,1,1
app.timer()
if root.checker(c, root.question_id): root.question_builder_2()
Button:
id: d
text: root.option_d
font_size: 20
on_release:
self.state = "down"
app.timer()
self.state = "normal"
if root.checker(d, root.question_id): d.background_color = 1,1,0,1
else: d.background_color = 100,0,0,0.5
app.timer()
d.background_color = 1,1,1,1
app.timer()
if root.checker(d, root.question_id):
root.question_builder_2()
"""
【问题讨论】:
标签: python-3.x kivy kivy-language