游戏发布时的黑屏

时间:2018-04-04 16:48:10

标签: python kivy

当我点击"开始游戏"时,我试图开始游戏。菜单屏幕被移除并且游戏启动(我可以听到声音然后被敌人杀死)但是看不到任何东西。我得到的只是黑屏。这就像游戏在后台启动一样。

我做错了什么?

更新了kv文件:

<Game_Screen>:
    name: 'game_screen'
    on_enter: app.launch_game()

<Game_Menu>:
    FloatLayout:
        pos_hint: {'center_x': 0.5, 'center_y': 0.5}
        size_hint: None, None
        canvas.before:
            Color:
                rgba: 1, 0, 0, 1
            Rectangle:
                pos: self.pos
                size: self.size
        Label:
            text: 'SPACE SHOOTER'
            font_size: 40
            font_name: "fonts/Alexis Italic.ttf"
            color: 0, 1, 0, 1
            pos_hint: {'center_x': 0.5, 'center_y': 1.7}


        BoxLayout:
            canvas.before:
                Rectangle:
                    pos: self.pos
                    size: self.size

            size: self.parent.size
            pos: self.parent.pos
            orientation: 'vertical'

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

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

更新了Python代码:

class Game(Widget):
    #  works fine on its own

class Game_Menu(Screen):
    pass

class Game_Screen(Screen):
    pass

class Menu_UI(Widget):
    pass

class Credits_Screen(Screen):
    pass

class GameApp(App):


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

    def launch_game(self):
        game = Game()
        Window.size = game.size
        engines1.loop = True
        engines1.play()
        return game


    def build(self):
        return self.sm


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

1 个答案:

答案 0 :(得分:0)

问题

Programming Guide » Events and Properties » Main loop

  

在Kivy应用程序中,避免长/无限循环或睡眠。该   程序将永远不会退出你的循环,阻止Kivy完成所有操作   需要做的其他事情。结果,你所看到的只是一个   您将无法与之互动的 黑色窗口

在您的kv文件中,您有 FloatLayout: ,Kivy会将其视为根小部件。

Rule context

  

通过声明根小部件的类来声明根规则,   没有任何缩进,后跟:并将被设置为根   App实例的属性。

解决方案

使用时钟计划功能,例如 schedule_interval() schedule_once() create_trigger()代替长/无限循环或睡眠。

  1. GameApp 之后立即将 sm 的代码移至 build 方法。
  2. &lt; Game_Menu&gt;
  3. 之后缩进所有代码

    片段 - main.py

    类GameApp(App):

    def launch_game(self):
        game = Game()
        Window.size = game.size
        engines1.loop = True
        engines1.play()
        return game
    
    def build(self):
        self.sm = ScreenManager()
        self.sm.add_widget(Game_Menu(name='menu'))
        self.sm.add_widget(Credits_Screen(name='credits'))
        self.sm.add_widget(Game_Screen(name='game_screen'))
        return self.sm
    

    片段 - game.kv

    #:kivy 1.10.0
    
    <Game_Screen>:
        name: 'game_screen'
        on_enter: app.launch_game()
    
    <Game_Menu>:
        FloatLayout:
            pos_hint: {'center_x': 0.5, 'center_y': 0.5}
            size_hint: None, None
            canvas.before:
                Color:
                    rgba: 1, 0, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
            Label:
                text: 'SPACE SHOOTER'
                font_size: 40
                font_name: "fonts/alexisv3ital.ttf"     # "fonts/Alexis Italic.ttf"
                color: 0, 1, 0, 1
                pos_hint: {'center_x': 0.5, 'center_y': 1.7}
    

    输出

    Img01 - AppStartup