基维文字冒险,Python

Text-Based adventure in Kivy, Python

我对编程很陌生,刚刚在大学参加了一个入门研讨会,我应该在 Python 中编写一个小应用程序。为此,我想使用 Kivy,但我被卡住了。 我有一个文本文件,其中应该包括问题、可能的答案以及考虑用户选择的答案应该去哪里:

0|"Will you rather do A or B?"|"A"|"B"|1|2
1|"Congrats, you chose A. Now go to B."|"Go to B"|"Go to B"|2|2
2|"That's B. Incredible. Want to discover C?"|"Yes."|"Stay here."|3|6
3|Wow, C is so cool, isn't it? There's also a D.|D? Crazy!|Boring. Go back.|4|0
4|Welcome to the depths of D. You are curious, aren't you?|Yep.|Nope.|5|0
5|Cool. There's nothing else here.|There must be.|Knew it.|4|0
6|Surprise! You should really discover C.|Alright.|Is there a D?|3|4

现在我想让游戏转到相应的行,替换显示的文本然后继续。从理论上讲,这有点像我的代码(如果它搞砸了,我很抱歉,正如我所说,我是这个主题的新手):

import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

with open('try.txt') as t:
    Lines = t.readlines()
first_line = Lines[0].strip()
x = first_line.split("|")
answer_b = int(x[5])

class MyGrid(GridLayout): 
    def __init__(self, **kwargs):
        super(MyGrid, self).__init__(**kwargs)
        self.cols = 1

        self.inside = GridLayout()
        self.inside.cols = 2

        self.btna = Button(text=x[2])
        self.btna.bind(on_press=self.a)
        self.inside.add_widget(self.btna)

        self.btnb = Button(text=x[3])
        self.btnb.bind(on_press=self.b)
        self.inside.add_widget(self.btnb)

        self.main_text = Label(text=x[1])
        self.add_widget(self.main_text)

        self.add_widget(self.inside)

    def a(self, instance):
        answer_a = int(x[4])
        next_line_a = Lines[answer_a].strip()
        print(next_line_a)
        print(answer_a)
        x = next_line_a.split("|")
        self.main_text.text = x[1]
        self.btna.text = x[2]
        self.btnb.text = x[3]
        self.btna.bind(on_press=self.a)

    def b(self, instance):
        next_line_b = Lines[answer_b].strip()
        print(next_line_b)
        print(answer_b)
        x = next_line_b.split("|")
        self.main_text.text = x[1]
        self.btna.text = x[2]
        self.btnb.text = x[3]

class Game(App):
    def build(self):
        return MyGrid()

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

问题是它停留在我定义的第一行,我真的不知道如何解决这个问题。我想我首先用第一行定义 x,然后用相应的新行重新定义 x。但是 next_line 和 x 变量都相互依赖 - 我尝试了两种不同的方法来回答 a 和 b,但两者都不起作用。 B会一直取first_line-x,A告诉我x在赋值前被引用了。 如果有人可以帮助我摆脱困惑,那就太好了,因为我尝试的一切都没有成功...... 谢谢!

我对其进行了更改,以便您将项目传递到您创建的对象中。获得正确的继承是一项挑战。

我还向 Games 对象添加了一个初始值设定项。我认为这是可行的,但老实说,我不是 Kivy 工作方面的专家,并且已经使这种模式发挥作用,但我不确定这是否是最佳实践。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button

with open('try.txt') as t:
    Lines = t.readlines()


class MyGrid(GridLayout):
    def __init__(self, Lines: list):
        super(MyGrid, self).__init__()
        self.first_line = Lines[0].strip()
        self.xx = self.first_line.split("|")
        self.answer_b = int(self.xx[5])
        self.cols = 1

        self.inside = GridLayout()
        self.inside.cols = 2

        self.btna = Button(text=self.xx[2])
        self.btna.bind(on_press=self.a)
        self.inside.add_widget(self.btna)

        self.btnb = Button(text=self.xx[3])
        self.btnb.bind(on_press=self.b)
        self.inside.add_widget(self.btnb)

        self.main_text = Label(text=self.xx[1])
        self.add_widget(self.main_text)

        self.add_widget(self.inside)

    def a(self, instance):
        answer_a = int(self.xx[4])
        next_line_a = Lines[answer_a].strip()
        print(next_line_a)
        print(answer_a)
        self.xx = next_line_a.split("|")
        self.main_text.text = self.xx[1]
        self.btna.text = self.xx[2]
        self.btnb.text = self.xx[3]
        self.btna.bind(on_press=self.a)

    def b(self, instance):
        next_line_b = Lines[self.answer_b].strip()
        print(next_line_b)
        print(self.answer_b)
        self.xx = next_line_b.split("|")
        self.main_text.text = self.xx[1]
        self.btna.text = self.xx[2]
        self.btnb.text = self.xx[3]


class Game(App):
    def __init__(self, **kwargs):
        self._arguments_to_pass_through = kwargs
        super().__init__()

    def build(self):
        return MyGrid(**self._arguments_to_pass_through)


if __name__ == '__main__':
    Game(Lines=Lines).run()