我正在Kivy中构建一个基本的猜谜游戏应用程序,以学习它的基础知识,但还没有完全了解它。当我进入下一个屏幕(TimeToGuess)时,我需要访问guesss_left,以便在编写逻辑来删除猜测时(如果猜错了)并最终耗尽它们(如果用完了)。

我已经尝试过将TimeToGuess类属性设置为Guesses以便通过剧院方式访问它,但是在按钮释放按钮上使用canvas.clear()并切换到TimeToGuess不适用于我。

这是python文件代码:

from kivy import *
import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

guesses_left = int()
computer_num = random.randint(1, 10)
user_guess = int()

class WannaPlay(Screen):
    pass


class Guesses(Screen):
    guesses_left = int()


class TimeToGuess(Screen):
        def user_is_guessing(self):
        if user_guess != computer_num:
            guesses_left -= 1


class Winner(Screen):
    pass


class Loser(Screen):
    pass


class ScreenManage(ScreenManager):
    pass


kv = Builder.load_file('guessinggame.kv')


class GuessingGameApp(App):
    def build(self):
        return kv


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



和.kv:

ScreenManage:
    WannaPlay:
    Guesses:
    TimeToGuess:
    Winner:
    Loser:


<WannaPlay>:
    name: 'start'
    GridLayout:
        cols: 1

        Label:
            text: 'Want to Play the Guessing Game?'

        GridLayout:
            cols: 2

            Button:
                name: 'yes'
                text: 'Yes'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'guess_choice'

            Button:
                name: 'no'
                text: 'No'
                on_release:
                    app.stop()


<Guesses>
    name: 'guess_choice'
    id: guesses_choice
    GridLayout:
        cols: 1

        Label:
            text: 'How Many Guesses Would You Like?'

        GridLayout:
            cols: 4

            Button:
                name: 'guess_amount_one'
                text: '1'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 1
                    print(guesses_left)



            Button:
                name: 'guess_amount_two'
                text: '2'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 2
                    print(guesses_left)

            Button:
                name: 'guess_amount_three'
                text: '3'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 3
                    print(guesses_left)

            Button:
                name: 'guess_amount_four'
                text: '4'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 4
                    print(guesses_left)


<TimeToGuess>
    name: 'time_to_guess'
    GridLayout:
        cols: 1

        Label:
            text: 'Guess a number between 1 and 10'

        GridLayout:
            cols: 5

            Button:
                name: 'guess_1'
                text: '1'
            Button:
                name: 'guess_2'
                text: '2'
            Button:
                name: 'guess_3'
                text: '3'
            Button:
                name: 'guess_4'
                text: '4'
            Button:
                name: 'guess_5'
                text: '5'
            Button:
                name: 'guess_6'
                text: '6'
            Button:
                name: 'guess_7'
                text: '7'
            Button:
                name: 'guess_8'
                text: '8'
            Button:
                name: 'guess_9'
                text: '9'
            Button:
                name: 'guess_10'
                text: '10'


先谢谢您的帮助

这是更新后的代码,以显示检查后的答案实际上是如何帮助解决至少该问题的(我相信我还会遇到更多问题。大声笑)

Python代码:

from kivy import *
import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

guesses_left = int()
user_guess = int()
computer_num = random.randint(1, 10)


class WannaPlay(Screen):
    pass


class Guesses(Screen):
    pass


def user_is_guessing(guesses_amount=guesses_left, computer_num=computer_num):
    if user_guess != computer_num and guesses_amount == 0:
        #subtract from  guesses_left go to lose page
        pass
    elif guesses_amount == computer_num:
        #go to win page
        pass
    elif guesses_amount != computer_num:
        #subtract from guesses_left and have the user guess again
        pass


class TimeToGuess(Screen):
    pass


class Winner(Screen):
    pass


class Loser(Screen):
    pass


class ScreenManage(ScreenManager):
    pass


kv = Builder.load_file('guessinggame.kv')


class GuessingGameApp(App):
    def build(self):
        return kv


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


.kv:

ScreenManage:
    WannaPlay:
    Guesses:
    TimeToGuess:
    Winner:
    Loser:


<WannaPlay>:
    name: 'start'
    GridLayout:
        cols: 1

        Label:
            text: 'Want to Play the Guessing Game?'

        GridLayout:
            cols: 2

            Button:
                name: 'yes'
                text: 'Yes'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'guess_choice'

            Button:
                name: 'no'
                text: 'No'
                on_release:
                    app.stop()


<Guesses>
    name: 'guess_choice'
    id: guesses_choice
    GridLayout:
        cols: 1

        Label:
            text: 'How Many Guesses Would You Like?'

        GridLayout:
            cols: 4

            Button:
                name: 'guess_amount_one'
                text: '1'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 1
                    print(guesses_left)



            Button:
                name: 'guess_amount_two'
                text: '2'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 2
                    print(guesses_left)

            Button:
                name: 'guess_amount_three'
                text: '3'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 3
                    print(guesses_left)

            Button:
                name: 'guess_amount_four'
                text: '4'
                on_release:
                    root.manager.transition.duration = 0
                    root.manager.current = 'time_to_guess'
                    guesses_left = 4
                    print(guesses_left)


<TimeToGuess>
    name: 'time_to_guess'
    GridLayout:
        cols: 1

        Label:
            text: 'Guess a number between 1 and 10'

        GridLayout:
            cols: 5

            Button:
                name: 'guess_1'
                text: '1'
            Button:
                name: 'guess_2'
                text: '2'
            Button:
                name: 'guess_3'
                text: '3'
            Button:
                name: 'guess_4'
                text: '4'
            Button:
                name: 'guess_5'
                text: '5'
            Button:
                name: 'guess_6'
                text: '6'
            Button:
                name: 'guess_7'
                text: '7'
            Button:
                name: 'guess_8'
                text: '8'
            Button:
                name: 'guess_9'
                text: '9'
            Button:
                name: 'guess_10'
                text: '10'

最佳答案

我已经有一段时间没有接触过Kivy了,所以我不确定这是否行得通,但是如果遵循基本的OOP,则应该行得通。

您可以只在特定类之外声明guesss_left变量,从而使所有类都可以访问它。即:

from kivy import *
import random
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen

# Initilize here
guesses_left = int()

class WannaPlay(Screen):
    pass


class Guesses(Screen):
    # instead of here
    pass


class TimeToGuess(Screen):
    pass


class Winner(Screen):
    pass


class Loser(Screen):
    pass


class ScreenManage(ScreenManager):
    pass


kv = Builder.load_file('guessinggame.kv')


class GuessingGameApp(App):
    def build(self):
        return kv


if __name__ == '__main__':
    # Or initilize here
    # guesses_left = int()
    GuessingGameApp().run()


如果仍然无法运行,请发表评论,我将在可能的情况下进一步调查。

07-24 20:54