首先复制我的代码:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
Builder.load_string("""
<StartScreen>:
BoxLayout:
orientation: 'vertical'
Label:
size: self.texture_size
text: root.instructions
TextInput:
id: save_code
font_size: 28
Button:
text: 'Press me to go to the Game Screen'
on_press: root.load_or_start_new(save_code.text)
<CreateNewCharacterScreen>:
BoxLayout:
orientation: 'vertical'
Label:
id: instr
size: self.texture_size
text: root.instructions
TextInput:
id: name
font_size: 28
Button:
text: 'Enter a name for your character'
on_press: root.create_character(name.text)
<MainGameScreen>:
BoxLayout:
orientation: 'vertical'
Label:
id: player_data
size: self.texture_size
text: root.statistics
Button:
text: 'Workout'
on_press: root.workout("STR",1)
<MenuScreen>:
BoxLayout:
Button:
text: 'Goto settings'
on_press: root.manager.current = 'settings'
Button:
text: 'Quit'
on_press: quit()
<SettingsScreen>:
BoxLayout:
Button:
text: 'My settings button'
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
""")
class PlayerStatistics(Widget):
def __init__(self, username='dummy', **kwargs):
super().__init__(**kwargs)
self.name: str = username
self.strength: int = 0
self.attributeDict = {"STR": self.strength}
def increment(self, parameter, amount=1):
if self.attributeDict.__contains__(parameter):
self.attributeDict[parameter] = self.attributeDict[parameter] + amount
else:
print("That Parameter does not exist")
def __str__(self):
return str("Strength: " + str(self.strength))
# Create the screen manager = sm
class MyScreenManager(ScreenManager):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.data = PlayerStatistics
def setPlayer(self, player: PlayerStatistics):
self.data = player
print(self.data.name) # Debugging Check
def getPlayer(self):
return self.data
class StartScreen(Screen):
instructions = StringProperty(str('''
Welcome to this fun game!
If you're new to the game or you want to start from the beginning just press the button!
Otherwise, first paste in your save code, and then press the button to load the game.
'''))
def load_or_start_new(self, savedata=''):
# For now we always start a new game
if savedata != '':
self.load_game(savedata)
else:
self.start_new_game()
pass
# Right now load and new do the same thing, but that might change in the future
def load_game(self, data):
self.manager.current = 'character'
pass
def start_new_game(self):
self.manager.current = 'character'
pass
pass
class CreateNewCharacterScreen(Screen):
instructions = StringProperty(str('''
This world is unlike any world you've known before. Great Things await. But first, you'll need a name...
'''))
fail_instructions = StringProperty(str('''
This world is unlike any world you've known before. Great Things await. But first, you'll need a name...
FOOLISH MORTAL!!! ENTER A NAME FIRST BEFORE YOU CLICK THE BUTTON.
'''))
def create_character(self, username):
if username != '':
data = PlayerStatistics(username)
self.manager.setPlayer(data)
self.manager.current = 'menu'
else:
self.instructions = self.fail_instructions
pass
pass
class MainGameScreen(Screen):
playerdata =
statistics =str(playerdata)
def workout(self):
pass
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
sm = MyScreenManager()
sm.add_widget(StartScreen(name='start'))
sm.add_widget(CreateNewCharacterScreen(name='character'))
sm.add_widget(MainGameScreen(name='game'))
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
class GUIApp(App):
def build(self):
return sm
# Entry point into the game
if __name__ == '__main__':
GUIApp().run()
在上面的示例中,我使用MyScreenManager扩展了ScreenManager类,以使屏幕知道有关修改与播放器关联的值的PlayerStatistics类。我希望能够在MainGameScreen类中调用getPlayer方法,以便可以在标签ID:player_data中显示数据。或者,可以使用一个按钮在PlayerStatistics中调用递增方法:on_press ...
不幸的是,这似乎不起作用。让我的屏幕(以及其中的窗口小部件)知道此称为PlayerStatistics的外部类的最佳方法是什么?