экран переключения не работает в .py файле - PullRequest
1 голос
/ 09 января 2020

Я новичок в киви. Я пытаюсь создать игру в понг, и все работает правильно, но я не могу переключиться на новый экран после победы игрока. Я хочу переключиться на новый экран после того, как игрок выиграл, и хочу отобразить сообщение на этом экране (что-то вроде - Вы выиграли !!), а также отобразить две кнопки: Воспроизвести снова / Go назад в Меню. Я пытаюсь переключиться на экран в файле .py, но он не работает. Ниже приведен код .py и .kv.

pong.py:

import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import (
    NumericProperty, ReferenceListProperty, ObjectProperty)
from kivy.uix.screenmanager import ScreenManager,WipeTransition
from kivy.vector import Vector
from kivy.clock import Clock

kivy.require('1.11.1')

class PongPaddle(Widget):
    score = NumericProperty(0)

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            vx, vy = ball.velocity
            offset = (ball.center_y - self.center_y) / (self.height / 2)
            bounced = Vector(-1 * vx, vy)
            vel = bounced * 1.3
            ball.velocity = vel.x, vel.y + offset


class PongBall(Widget):
    velocity_x = NumericProperty(0)
    velocity_y = NumericProperty(0)
    velocity = ReferenceListProperty(velocity_x, velocity_y)

    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


class PongGame(Widget):
    ball = ObjectProperty(None)
    player1 = ObjectProperty(None)
    player2 = ObjectProperty(None)

    def __init__(self, *args, **kwargs):
        super(PongGame,self).__init__(*args,**kwargs)
        Clock.schedule_interval(self.update, 1.0 / 60.0)

    def serve_ball(self, vel=(4, 0)):
        self.ball.center = self.center
        self.ball.velocity = vel

    def update(self, dt):
        self.ball.move()

        #bounce of paddles
        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

    #bounce ball off bottom or top
        if (self.ball.y < self.y) or (self.ball.top > self.top):
            self.ball.velocity_y *= -1

    #went of to a side to score point?
        if self.ball.x < self.x:
            self.player2.score += 1
            self.serve_ball(vel=(4, 0))
        if self.ball.x > self.width:
            self.player1.score += 1
            self.serve_ball(vel=(-4, 0))

        if self.player1.score==5:
                self.ball.center = self.center
                self.ball.velocity = self.vel=(0, 0)
                mainapp.sm.current='winner1'

        if self.player2.score==5:
                self.ball.center = self.center
                self.ball.velocity = self.vel=(0, 0)
                mainapp.sm.current='winner2'

    def on_touch_move(self, touch):
        if touch.x < self.width / 3:
            self.player1.center_y = touch.y
        if touch.x > self.width - self.width / 3:
            self.player2.center_y = touch.y

class Manager(ScreenManager):
    pass

class MainApp(App):

    def build(self):
        self.load_kv('pong.kv')
        self.sm=Manager()
        return Manager(transition=WipeTransition())

if __name__ == '__main__':
    mainapp = MainApp()
    mainapp.run()


pong.kv:

<PongBall>:
    size: 50, 50
    canvas:
        Ellipse:
            pos: self.pos
            size: self.size

<PongPaddle>:
    size: 25, 200
    canvas:
        Rectangle:
            pos:self.pos
            size:self.size

<PongGame>:
    ball: pong_ball
    player1: player_left
    player2: player_right

    canvas:
        Rectangle:
            pos: self.center_x-5, 0
            size: 10, self.height

    Label:
        font_size: 70
        center_x: root.width / 4
        top: root.top - 50
        text: str(root.player1.score)

    Label:
        font_size: 70
        center_x: root.width * 3 / 4
        top: root.top - 50
        text: str(root.player2.score)

    PongBall:
        id: pong_ball
        center: self.parent.center

    PongPaddle:
        id: player_left
        x: root.x
        center_y: root.center_y

    PongPaddle:
        id: player_right
        x: root.width-self.width
        center_y: root.center_y


<Manager>:
    id: screen_manager

    Screen:
        name: 'home'
        BoxLayout:
            orientation: 'vertical'

            Label:
                text: 'WELCOME TO PING PONG GAME'
                font_size: 45
                text_size: self.size
                halign: 'center'
                valign: 'middle'

            Button:
                text: 'Start New Game'
                halign: 'center'
                valign: 'middle'
                font_size: 30
                text_size: self.size
                on_release: root.current = 'game';game.serve_ball()

            Button:
                text: 'Rules'
                halign: 'center'
                valign: 'middle'
                font_size: 30
                text_size: self.size
                pos: self.pos
                on_release: root.current= 'rule'

    Screen:
        name: 'game'
        PongGame:
            id: game

    Screen:
        name: 'rule'
        BoxLayout:
            orientation: 'vertical'
            Label:
                text: 'Please read the rules carefully.'
                text_size: self.size
                font_size: 30
                halign: 'center'
                valign: 'middle'

            Label:
                text: 'Players need to move their pong paddles and save the balls from dashing into their sides of the wall.'
                text_size: self.size
                font_size: 25

            Label:
                text: 'The player who pushes the ball into their opponents wall gets a score.'
                text_size: self.size
                font_size: 25

            Label:
                text: 'The first player to reach score 5 wins the game.'
                text_size: self.size
                font_size: 25

            Label:
                text: 'All The Best!!!!'
                text_size: self.size
                font_size: 30
                halign: 'center'
                valign: 'middle'

            Button:
                text: 'Home'
                text_size: self.size
                font_size: 25
                halign: 'center'
                valign: 'middle'
                on_release: root.current = 'home'

    Screen:
        name: 'winner1'
        GridLayout:
            cols: 1
            Label:
                text: 'Congratulations Player1 !!! You won against Player2!!!!'
                text_size: self.size
                font_size: 45
                halign: 'center'
                valign: 'middle'
            GridLayout:
                cols: 2
                Button:
                    text: 'Play Again'
                    text_size: self.size
                    font_size: 45
                    halign: 'center'
                    valign: 'middle'
                    on_release: root.current = 'game'; game.serve_ball()

                Button:
                    text: 'Home'
                    text_size: self.size
                    font_size: 45
                    halign: 'center'
                    valign: 'middle'
                    on_release: root.current = 'home'
    Screen:
        name: 'winner2'

        GridLayout:
            cols: 1
            Label:
                text: 'Congratulations Player2 !!! You won against player 1!!!!'
                text_size: self.size
                font_size: 45
                halign: 'center'
                valign: 'middle'

            GridLayout:
                cols: 2
                Button:
                    text: 'Play Again'
                    text_size: self.size
                    font_size: 45
                    halign: 'center'
                    valign: 'middle'
                    on_release: root.current = 'game'; game.serve_ball()

                Button:
                    text: 'Home'
                    text_size: self.size
                    font_size: 45
                    halign: 'center'
                    valign: 'middle'
                    on_release: root.current = 'home'```
...