简体   繁体   English

Kivy、Python 中基于文本的冒险

[英]Text-Based adventure in Kivy, Python

I'm very new to programming, just have an introductory seminar in university, and I'm supposed to program a little app in Python.我对编程很陌生,刚在大学里有一个介绍性研讨会,我应该在 Python 中编写一个小应用程序。 For that, I want to use Kivy, but I got stuck.为此,我想使用 Kivy,但我被卡住了。 I have a text file which should include the question, the possible answers and where it's supposed to go considering which answer the user chose:我有一个文本文件,其中应该包含问题、可能的答案以及它应该在哪里 go 考虑到用户选择的答案:

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

Now I want the game to go to the according line, replace the displayed text and go on.现在我要游戏到go到相应的行,替换显示的文字和go上。 In theory, this is kind of working with my Code (I'm sorry if it's messed up, as I said, I'm new to this topic):从理论上讲,这有点像我的代码(如果它搞砸了,我很抱歉,正如我所说,我是这个主题的新手):

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()

The problem is that it stays with the first line I defined and I don't really know how to go around that problem.问题是它停留在我定义的第一行,我真的不知道如何解决这个问题。 I would imagine that I first define x with the first line, and after that x gets redefined with the according new line.我想我首先用第一行定义 x ,然后用相应的新行重新定义 x 。 But the next_line and x variable are both dependent on each other - I tried two different ways with answer a and b, but both don't really work.但是 next_line 和 x 变量都是相互依赖的——我尝试了两种不同的方法来回答 a 和 b,但两者都不起作用。 B will just continuously take the first_line-x, A tells me that x is referenced before assignment. B 会不断地取 first_line-x,A 告诉我在赋值之前引用了 x。 It would be great if someone could help me out of my confusion, because everything I tried just didn't work out... Thanks!如果有人能帮助我摆脱困惑,那就太好了,因为我尝试的一切都没有成功……谢谢!

I changed it so you pass items into the object that you create.我对其进行了更改,因此您将项目传递到您创建的 object 中。 It's challenging to get the inheritance correct.正确获取 inheritance 具有挑战性。

I also added an initializer to the Games object.我还在游戏 object 中添加了一个初始化程序。 I think this works but to be honest I am not expert in the workings of Kivy and have gotten this pattern to work but I don't know for sure if it is best practice.我认为这可行,但老实说,我不是 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM