在Kivy中启动屏幕时执行功能

问题描述 投票:0回答:1

当屏幕从屏幕1(ComputerScreen)更改为屏幕2(PlayerScreen)时,我试图自动执行功能(self.bomb),如下所示。屏幕以代码self.manager.current = "player"更改。

class ComputerScreen(Screen):
    def __init__(self, **kwargs):
        super(ComputerScreen, self).__init__(**kwargs)
        self.comgrid = comgrid()
        self.add_widget(self.comgrid)


    def shoot(self):
        shootsub_x = self.shootxinput.text,
        shootsub_y = self.shootyinput.text,
        shootsub_z = self.shootdepthinput.text,
        while True:
            try:
                shootsub_x = int(shootsub_x[0])
                try:
                    shootsub_y = str(shootsub_y[0]).upper()
                    try:
                        shootsub_z = int(shootsub_z[0])
                        break
                    except:
                        showpopup("z error")
                        print("try")
                except:
                    showpopup("y error")
            except:
                showpopup("x error")
        if shootsub_x not in range(1, 11):
            showpopup("x error")
        elif shootsub_y not in sub_ydict:
            showpopup("y error")
        elif shootsub_z not in sub_zdict:
            showpopup("z error")
            print("z not in dict")
        else:
            target = sub_ydict[shootsub_y] + shootsub_x + sub_zdict[shootsub_z]
            shots = explosion(target)
            for i in shots:
                if i in computerlist:
                    computerlist.remove(i)
                if comgrid_dict[i] == "clear.png":
                    comgrid_dict[i] = "cross.png"
                elif comgrid_dict[i] == "clear_ship.png":
                    comgrid_dict[i] = "explosion.png"
                else:
                    pass
            self.comgrid.update()
            Clock.schedule_once(self.check_game, 1)

    def check_game(self,instance):
        if updategame():
            game_status = True
            self.show = popup_game()
            self.show.label.text = "Computer's turn to make a move..."
            self.popupWindow = Popup(title="Computer's Turn", content=self.show, size_hint=(0.5, 0.4),
                                pos_hint={'x': 0.25, 'y': 0.3})
            self.popupWindow.open()
            Clock.schedule_once(self.computerturn, 2)
        else:
            self.show = popup_game()
            self.show.label.text = "You have eliminated your opponent!"
            popupWindow = Popup(title="You win!", content=self.show, size_hint=(0.5, 0.4),
                                pos_hint={'x': 0.25, 'y': 0.3})
            popupWindow.open()
            Clock.schedule_once(self.end_game, 2)

    def computerturn(self,instance):
        self.manager.current = "player"
        self.popupWindow.dismiss()
        game_status = True

    def end_game(self,instance):
        self.manager.current = "main"





class PlayerScreen(Screen):
    def __init__(self, **kwargs):
        super(PlayerScreen, self).__init__(**kwargs)
        self.gamegrid = gamegrid()
        self.add_widget(self.gamegrid)
        if game_status:
            self.bomb()

    def bomb(self):
        target = random.choice(range(1, 200))
        shots = explosion(target)
        for i in shots:
            if i in playerlist:
                playerlist.remove(i)
                game_dict[i] = "explosion.png"
                print(playerlist)
            if game_dict[i] == "clear.png":
                game_dict[i] = "cross.png"
        self.gamegrid.update()
        status = False
        Clock.schedule_once(self.check_game, 3)

    def check_game(self, instance):
        if updategame():
            self.manager.current = "computer"
        else:
            self.show = popup_game()
            self.show.label.text = "Your opponent exploded your ships!"
            popupWindow = Popup(title="You lose!", content=self.show, size_hint=(0.5, 0.4),
                                pos_hint={'x': 0.25, 'y': 0.3})
            popupWindow.open()
            Clock.schedule_once(self.end_game, 2)

    def end_game(self,instance):
        self.manager.current = "main"

我添加了if game_status:行,因为没有它,当应用程序启动时,屏幕立即变为PlayerScreen,跳过前面的几个屏幕。

python kivy kivy-language
1个回答
0
投票

要在进入新屏幕时执行功能,您可以尝试向该屏幕添加以下方法:

def on_enter():

显示屏幕时将触发此方法。

因此,在您的情况下,请尝试将def bomb(self)替换为def on_enter(self):。并且也将self.bomb()替换为self.on_enter()。让我知道它是否有效。

© www.soinside.com 2019 - 2024. All rights reserved.