簡體   English   中英

Kivy、Python 中基於文本的冒險

[英]Text-Based adventure in Kivy, Python

我對編程很陌生,剛在大學里有一個介紹性研討會,我應該在 Python 中編寫一個小應用程序。 為此,我想使用 Kivy,但我被卡住了。 我有一個文本文件,其中應該包含問題、可能的答案以及它應該在哪里 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

現在我要游戲到go到相應的行,替換顯示的文字和go上。 從理論上講,這有點像我的代碼(如果它搞砸了,我很抱歉,正如我所說,我是這個主題的新手):

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。 如果有人能幫助我擺脫困惑,那就太好了,因為我嘗試的一切都沒有成功……謝謝!

我對其進行了更改,因此您將項目傳遞到您創建的 object 中。 正確獲取 inheritance 具有挑戰性。

我還在游戲 object 中添加了一個初始化程序。 我認為這可行,但老實說,我不是 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