在游戏启动时删除菜单屏幕

时间:2018-04-04 11:47:29

标签: python kivy

我正在与Kivy开发一款简单的射击游戏。我正处于这样的地步 我正在从游戏菜单中启动新游戏。然而程序行为奇怪,我不知道为什么。当我点击“开始”时游戏启动但菜单屏幕不会消失。它向上移动。

我试图摆弄self.remove_widget(sm),但这没有效果。

我在此处记录了行为https://drive.google.com/open?id=1R-JV74w4VDThOvjf9Xhm7w6WV8rWtEgZ供您查看。

这是我的代码:

kv文件:

        Label:
            text: 'menu'
            font_name: "fonts/Alexis Bold.ttf"
            font_size: 30
            color: 0, 1, 0, 1

        Button:
            text: 'start game'
            on_press: app.launch_game()
        Button:
            text: 'highest score'
            on_release: 
        Button:
            text: 'credits'
            on_release: app.root.current = 'credits'

Python代码:

class GameApp(App):

    sm = ScreenManager()
    sm.add_widget(Game_Menu(name='menu'))
    sm.add_widget(Credits_Screen(name='credits'))

    def launch_game(self):
        self.sm.remove_widget(Game_Menu(name='menu'))
        game = Game()
        game_menu = Menu_UI()
        Window.size = game.size
        engines1.loop = True
        engines1.play()
        return game

    def build(self):
        return sm


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

1 个答案:

答案 0 :(得分:0)

在以下说明中def store_data(AccountNo): db = MySQLdb.connect(host=HOST, user=USER, passwd=PASSWD, db=DATABASE, charset="utf8") cursor = db.cursor() insert_query = "INSERT INTO cstore (AccountNo,ZIP,ReportDate) VALUES (:AccountNo,:ZIP,:ReportDate)" cursor.executemany(insert_query, AccountNo) db.commit() cursor.close() db.close() return def on_data(file_path): # This is the meat of the script...it connects to your mongoDB and stores the tweet try: #declare an empty list for the all accountno's accountno_list = list() # Decode the JSON from Twitter testFile = open(file_path) datajson = json.load(testFile) # print (len(datajson)) # grab the wanted data from the Tweet for row in datajson[0]['Transactions']: values = dict() values['AccountNo'] = row['CSPAccountNo'] values['ZIP'] = row['ZIP'] values['ReportDate'] = row['ReportDate'] #from here on you can populate the attributes you need in a similar way.. accountno_list.append(values) except: pass store_data(accountno_list) 您正在创建一个尚未添加到ScreenManager的新Menu对象,因为尚未添加它无法删除。你应该做的是获得相同的菜单,为此我们将使用ScreenManager的get_screen()方法通过其名称获取相同的屏幕,然后将其删除。

您必须使用以下行替换上一行:

self.sm.remove_widget(Game_Menu(name='menu'))
相关问题