"""
# Step 1 - Create the App
# Step 2 - Create the Game
# Step 3 - Build the Game
# Step 4 - Run the App
"""

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty
from kivy.vector import Vector
from kivy.clock import Clock
from random import randint


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

    def bounce_ball(self, ball):
        if self.collide_widget(ball):
            ball.velocity_x *= -1
            print('hello world')


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

    # Latest Position of the Ball = Current Velocity + Current Position
    def move(self):
        self.pos = Vector(*self.velocity) + self.pos


# Update - moving the ball by calling the move function and other stuff
# on touch_down() = When our fingers/mouse touches he screen
# on touch_up() - when we lift our finger off the screen after touching it
# on_touch_move() - when we drag our finger on the screen

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

    def serve_ball(self):
        self.ball.velocity = Vector(4, 0).rotate(randint(0, 360))

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

        # Bounce off top and bottom Y
        if (self.ball.y < 0) or (self.ball.y > self.height - 50):
            self.ball.velocity_y *= -1.1

        # Bounce off left and increase th score
        if self.ball.x < 0:
            self.ball.velocity_x *= -1
            self.player1.score += 1

        # Bounce off right and increase the score
        if self.ball.x > self.width - 50:
            self.ball.velocity_x *= -1
            self.player2.score += 1

        self.player1.bounce_ball(self.ball)
        self.player2.bounce_ball(self.ball)

    def on_touch_move(self, touch):
        if touch.x < self.width / 1 / 4:
            self.player1.center_y = touch.y

        if touch.x > self.width * 3 / 4:
            self.player2.center_y = touch.y


class PongApp(App):
    def build(self):
        game = PongGame()
        game.serve_ball()
        Clock.schedule_interval(game.update, 1.0 / 60.0)
        return game


PongApp().run()

[((17, 12, 17, 30), 'kivy.properties.NumericProperty', 'NumericProperty', ({(17, 28, 17, 29): '0'}, {}), '(0)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((26, 17, 26, 35), 'kivy.properties.NumericProperty', 'NumericProperty', ({(26, 33, 26, 34): '0'}, {}), '(0)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((27, 17, 27, 35), 'kivy.properties.NumericProperty', 'NumericProperty', ({(27, 33, 27, 34): '0'}, {}), '(0)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((28, 15, 28, 60), 'kivy.properties.ReferenceListProperty', 'ReferenceListProperty', ({(28, 37, 28, 47): 'velocity_x', (28, 49, 28, 59): 'velocity_y'}, {}), '(velocity_x, velocity_y)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((41, 11, 41, 31), 'kivy.properties.ObjectProperty', 'ObjectProperty', ({(41, 26, 41, 30): 'None'}, {}), '(None)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((42, 14, 42, 34), 'kivy.properties.ObjectProperty', 'ObjectProperty', ({(42, 29, 42, 33): 'None'}, {}), '(None)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((43, 14, 43, 34), 'kivy.properties.ObjectProperty', 'ObjectProperty', ({(43, 29, 43, 33): 'None'}, {}), '(None)', False, 'from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty\n'), ((80, 8, 80, 56), 'kivy.clock.Clock.schedule_interval', 'Clock.schedule_interval', ({(80, 32, 80, 43): 'game.update', (80, 45, 80, 55): '(1.0 / 60.0)'}, {}), '(game.update, 1.0 / 60.0)', False, 'from kivy.clock import Clock\n'), ((32, 19, 32, 41), 'kivy.vector.Vector', 'Vector', ({(32, 26, 32, 40): '*self.velocity'}, {}), '(*self.velocity)', False, 'from kivy.vector import Vector\n'), ((46, 49, 46, 64), 'random.randint', 'randint', ({(46, 57, 46, 58): '0', (46, 60, 46, 63): '360'}, {}), '(0, 360)', False, 'from random import randint\n'), ((46, 29, 46, 41), 'kivy.vector.Vector', 'Vector', ({(46, 36, 46, 37): '4', (46, 39, 46, 40): '0'}, {}), '(4, 0)', False, 'from kivy.vector import Vector\n')]