首页 > 解决方案 > 向kivy屏幕添加功能

问题描述

当我第一次离开主屏幕时,屏幕被复制了。当我第一次返回主屏幕时,屏幕全黑。但它工作正常。

我第一次滑动时第二个屏幕的图像在此处输入图像描述。我第一次回来时的主屏幕图像:在此处输入图像描述。但它适用于第二次和其他滑动。

Builder.load_string("""
<MenuScreen>:
    BoxLayout:
        Button:
            text: 'Go to main screen'
            on_press: root.manager.current = 'main'
            size_hint: None, None
            size: self.texture_size
        Button:
            text: 'Go to second screen'
            on_press: root.manager.current = 'second'
            size_hint: None, None
            size: self.texture_size
<MainScreen>:
    # add Game widget
    Game:
        power: powerbar
        lifes: lifesbar
        id: game
    BoxLayout:
        Button:
            text: 'Go to the menu screen'
            on_press: root.manager.current = 'menu'
            size_hint: None, None
            size: self.texture_size
    FloatLayout:
        ShootProgressBar:
            id: powerbar
            pos_hint: {'x': 0.035, 'y': 0.9}
            size_hint: (0.25, 0.1)
            max:10
        LifesProgressBar:
            canvas:
                Color:
                    rgba: 1, 0, 0, 1
            id: lifesbar
            pos_hint: {'x': 0.7, 'y': 0.92}
            size_hint: (0.25, 0.1)
            max:3
            value:3
        Image:
            id: ethereum
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.002, 'y': 0.93}
            allow_stretch: True
            source: "ethereum.png"
        Image:
            id: lifes_img
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.952, 'y': 0.93}
            allow_stretch: True
            source: "lifes.png"

<SecondScreen>:
    # add Game widget
    Game:
        power: powerbar
        lifes: lifesbar
        id: game
    BoxLayout:
        Button:
            text: 'Back to menu'
            on_press: root.manager.current = 'menu'
            size_hint: None, None
            size: self.texture_size
    FloatLayout:
        ShootProgressBar:
            id: powerbar
            pos_hint: {'x': 0.035, 'y': 0.9}
            size_hint: (0.25, 0.1)
            max:10
        LifesProgressBar:
            canvas:
                Color:
                    rgba: 1, 0, 0, 1
            id: lifesbar
            pos_hint: {'x': 0.7, 'y': 0.92}
            size_hint: (0.25, 0.1)
            max:3
            value:3
        Image:
            id: ethereum
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.002, 'y': 0.93}
            allow_stretch: True
            source: "ethereum.png"
        Image:
            id: lifes_img
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.952, 'y': 0.93}
            allow_stretch: True
            source: "lifes.png"
            
<-ShootProgressBar@ProgressBar>:
    canvas:
        Color:
            rgb: 1, 1, 1
        BorderImage:
            border: (12, 12, 12, 12)
            pos: self.x, self.center_y - 3
            size: self.width, 24
            source : 'black_bg.png'
        BorderImage:
            border: [int(min(self.width * (self.value / float(self.max)) if self.max else 0, 12))] * 4
            pos: self.x, self.center_y - 12
            size: self.width * (self.value / float(self.max)) if self.max else 0, 24
            source : 'blue_fg.png'
            
<-LifesProgressBar@ProgressBar>:
    canvas:
        Color:
            rgb: 1, 1, 1
        BorderImage:
            border: (12, 12, 12, 12)
            pos: self.x, self.center_y - 14
            size: self.width, 24
            source: 'black_bg.png'
        BorderImage:
            border: [int(min(self.width * (self.value / float(self.max)) if self.max else 0, 12))] * 4
            pos: self.x, self.center_y - 12
            size: self.width * (self.value / float(self.max)) if self.max else 0, 24
            source: 'green2_fg.png'

<ColourScreen>:
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'colour {:.2},{:.2},{:.2} screen'.format(*root.colour[:3])
            font_size: 30
        Widget:
            canvas:
                Color:
                    rgba: root.colour
                Ellipse:
                    pos: self.pos
                    size: self.size
        BoxLayout:
            Button:
                text: 'goto first screen'
                font_size: 30
                on_release: app.root.current = 'first'
            Button:
                text: 'get random colour screen'
                font_size: 30
                on_release: app.root.new_colour_screen()
""")


# Declare both screens

class MenuScreen(Screen):
    pass


class MainScreen(Screen):
    def on_enter(self, *args):
        self.ids.game.initialize()
        self.ids.game.power.value = 0
        self.ids.game.lifes.value = 3
        self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)

    def on_leave(self, *args):
        self.update_event.cancel()
        self.ids.game.indices = []
        self.ids.game.vertices = []
        self.ids.game.particles = []
        self.ids.game.canvas.clear()
        self.update_event = None
            


class SecondScreen(Screen):

    def on_enter(self, *args):
        self.ids.game.initialize()
        self.ids.game.power.value = 0
        self.ids.game.lifes.value = 3
        self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)

    def on_leave(self, *args):
        self.update_event.cancel()
        self.ids.game.indices = []
        self.ids.game.vertices = []
        self.ids.game.particles = []
        self.ids.game.canvas.clear()
        self.update_event = None

我想要的是使用条件来改变屏幕。例如,if self.parent.power.value == 10: sm.current = 'second'

标签: pythonkivy

解决方案


如果我了解您要做什么,您可以将您的Game小部件添加到MainScreenkv(您还必须调整 的大小Button,使其不覆盖整个MainScreen):

<MainScreen>:
    # add Game widget
    Game:
        id: game
    BoxLayout:
        Button:
            text: 'Go to the second screen'
            on_press: root.manager.current = 'second'
            size_hint: None, None
            size: self.texture_size
    FloatLayout:
        ShootProgressBar:
            id: powerbar
            pos_hint: {'x': 0.035, 'y': 0.9}
            size_hint: (0.25, 0.1)
            max:10
        LifesProgressBar:
            canvas:
                Color:
                    rgba: 1, 0, 0, 1
            id: lifesbar
            pos_hint: {'x': 0.7, 'y': 0.92}
            size_hint: (0.25, 0.1)
            max:3
            value:3
        Image:
            id: ethereum
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.002, 'y': 0.93}
            allow_stretch: True
            source: "ethereum.png"
        Image:
            id: lifes_img
            size_hint: (0.04, 0.04)
            pos_hint: {'x': 0.952, 'y': 0.93}
            allow_stretch: True
            source: "lifes.png"

然后在MainScreenSecondSceen类中,您可以进行初始化并开始更新:

class MainScreen(Screen):
    def on_enter(self, *args):
        self.ids.game.initialize()
        self.ids.game.power.value = 0
        self.ids.game.lifes.value = 0
        self.update_event = Clock.schedule_interval(self.ids.game.update_glsl, 1.0 / 60.0)

    def on_leave(self, *args):
        self.update_event.cancel()
        self.ids.game.indices = []
        self.ids.game.vertices = []
        self.ids.game.particles = []
        self.ids.game.canvas.clear()
        self.update_event = None


class SecondScreen(Screen):

    def on_enter(self, *args):
        self.ids.game1.initialize()
        self.ids.game1.power.value = 0
        self.ids.game1.lifes.value = 0
        self.update_event = Clock.schedule_interval(self.ids.game1.update_glsl, 1.0 / 60.0)

    def on_leave(self, *args):
        self.update_event.cancel()
        self.ids.game1.indices = []
        self.ids.game1.vertices = []
        self.ids.game1.particles = []
        self.ids.game1.canvas.clear()
        self.update_event = None

并在Game.update_glsl()方法中,用于super访问PSWidget更新方法,如下所示:

def update_glsl(self, nap):
    if self.use_mouse:
        self.player_x, self.player_y = Window.mouse_pos

    if self.firing:
        self.fire_delay -= nap

    self.spawn_delay -= nap

    super(Game, self).update_glsl(nap)

还。在您的PSWidget班级中,更改 , 的定义indicesvertices并使particles它们成为Properties,如下所示:

class PSWidget(Widget):
    indices = ListProperty([])
    vertices = ListProperty([])
    particles = ListProperty([])

推荐阅读