从python代码Kivy更改屏幕

时间:2016-12-29 23:38:09

标签: android python kivy kivy-language

所以我在游戏结束后更换屏幕时遇到问题。 我试过这样做:

Manager.current = 'endmenu'

但它不起作用。这是我的代码形式kv和py文件的片段

main.py,我使用switch()

的功能
def bounce_ball(self, ball, score):
    """Check for collision between a ball and a circle"""
    dx = ball.center_x - self.center_x
    dy = ball.center_y - self.center_y
    #print("Ball: ",ball.center)
    #print("Center: ",self.center)
    distance = Vector(ball.center).distance(self.center) - 50
    if distance >= 100 + 30 and distance <= 100 + 30 + 4:# circle radius + ball size
        vx, vy = ball.velocity
        centertoballangle = Vector(0,1).angle((dx,dy))
        if centertoballangle <= 0: #angle 0-360 not 0-180 to  -180-0
            centertoballangle += 360
        ball.angle = Vector(vx,vy).angle((dx,dy))
        if self.checkcolor(ball, centertoballangle, self.angle%360):
            bounced = Vector(vx,vy)
            if ball.angle > 90:
                ball.angle-=90
            elif ball.angle < -90:
                ball.angle+=90
            ball.angle = 180-2*ball.angle # set new angle off ball after bounce
            ball.touchspeed*=2 # make changing angle a bit more aggresive
            if ball.angle + ball.touchspeed < 350 or ball.angle + ball.touchspeed > 40:
                ball.angle += ball.touchspeed # ball.touchspeed - change the angle!
            bounced = bounced.rotate(ball.angle) # finally, rotate the ball
            if score<12:
                vel = bounced * 1.1 # make ball move faster
            else:
                vel = bounced
            ball.velocity = vel.x , vel.y
            ball.touchspeed=0
            return True # add +1 to score
        else:
            Manager.switch()
            return False

编辑后的屏幕管理员课程:

class Manager(ScreenManager):
    def __init__(self, *args, **kwargs):
        super(Manager, self).__init__(*args, **kwargs)
        self.current = 'menu'
    def switch(self, where):
        self.current = str(where)
sm = Manager(transition=WipeTransition())

class ColorPongApp(App):
    def build(self):
        return sm

和我的kv文件:

<Manager>
    Screen:
        name: 'menu'
        BoxLayout:
            Button:
                text: 'Play Color Pong'
                halign: 'center'
                valign: 'middle'
                font_size: 50
                text_size: self.size
                on_release: root.current = 'game';game.serve_ball()
            Button:
                text: 'Quit'
                halign: 'center'
                valign: 'middle'
                font_size: 50
                text_size: self.size
                on_release: game.quit()

    Screen:
        name: 'game'
        ColorPongGame:
            id: game

    Screen:
        name: 'endmenu'
        id: endmenu
        BoxLayout:
            Button:
                text: 'Replay'
                halign: 'center'
                valign: 'middle'
                font_size: 50
                text_size: self.size
                on_release: root.current = 'game';game.serve_ball()
            Button:
                text: 'Quit'
                halign: 'center'
                valign: 'middle'
                font_size: 50
                text_size: self.size
                on_release: game.quit()

我想要做的就是更改屏幕,我认为无法完成,因为名称'endmenu'在python文件中是未知的,即使有这些知识,我也不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

首先,您可以将Manager()设为全局对象 sm = Manager()
在构建方法中返回sm。然后你可以像下面这样访问它:
sm.switch()

如果您在Manager课程中,则可以使用self.current
我举了一个例子:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder


Builder.load_string("""

<Manager>:
    Screen:
        name: 'first'
        Button:
            text:'go to other'
            on_press: root.goto_other()

    Screen:
        name: 'other'
        Button:
            text:'go to first'
            on_press: root.goto_first()

""")


class Manager(ScreenManager):

    def goto_first(self):
        self.current = "first"

    def goto_other(self):
        self.current = "other"


class ScreensApp(App):

    def build(self):
        return Manager()


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