# Day 17
#     ___                        _                 ___       _ _
#    / __\ ___  _   _ _ __   ___(_)_ __   __ _    / __\ __ _| | |
#   /__\/// _ \| | | | '_ \ / __| | '_ \ / _` |  /__\/// _` | | |
#  / \/  \ (_) | |_| | | | | (__| | | | | (_| | / \/  \ (_| | | |
#  \_____/\___/ \__,_|_| |_|\___|_|_| |_|\__, | \_____/\__,_|_|_|
#                                        |___/
from tkinter import *
import time

# main
root=Tk()
root.title("Ball Men")
root.resizable(True, True)
root.geometry("700x500+0+0")

# canvas
c=Canvas(root, width=700, height=500, bg="black")
c.pack()

# background
bg = PhotoImage(file="phodo/cosmic.png").subsample(4,4)
c.create_image( 0, 0, image = bg, anchor = "nw")


# Variables
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# variables for blue ball
blux = 200
bluy = 200
bluxdirection = 1
bluydirection = 1
bluspeed = 12
blue_ball = c.create_oval(blux, bluy, blux+25, bluy+25, fill="blue")

# variables for tennis ball
tenx = 600
teny = 400
tenxdirection = -1
tenydirection = -1
tenspeed = 13
tennis = PhotoImage(file="phodo/ball.png").subsample(64, 64)
tennis_ball = c.create_image(tenx, teny, image=tennis)

# variables for dragon ball
drax = 350
dray = 50
draxdirection = -1
draydirection = 1
draspeed = 9
dragon = PhotoImage(file="phodo/dragonball.png").subsample(16,16)
dragonball = c.create_image(drax, dray, image=dragon)


# Extra functions
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

# pause function
def toggle_pause():
    global paused
    if paused == True:
        # print("0")
        paused = False
    else:
        paused = True
        # print("1")


def speed_up():
    global tenspeed
    global draspeed
    global bluspeed
    tenspeed += 1
    draspeed += 1
    bluspeed += 1

def speed_down():
    global tenspeed
    global draspeed
    global bluspeed
    tenspeed -= 1
    draspeed -= 1
    bluspeed -= 1

# pause button
pause = Button(root, text="PAUSE", background="blue", foreground="white", font=20, command = toggle_pause)
pause.place(x=0, y=10)

# SPEED speed_u button
pause = Button(root, text="SPEED UP!", background="blue", foreground="white", font=20, command = speed_up)
pause.place(x=90, y=10)

# SPEED speed_u button
pause = Button(root, text="SPEED DOWN!", background="blue", foreground="white", font=20, command = speed_down)
pause.place(x=215, y=10)

# main function
# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_

paused = False
while True:
    if not paused:

        # move blue ball
        blux += bluxdirection * bluspeed # move blue oval in x direction
        bluy += bluydirection * bluspeed # move blue oval in y direction

        # move tennis ball
        tenx += tenxdirection * tenspeed
        teny += tenydirection * tenspeed

        # move dragonball
        drax += draxdirection * draspeed
        dray += draydirection * draspeed

        # boundaries for blue ball
        # check for right and left side boundary
        if blux < 0:
            bluxdirection = 1
            draxdirection = -1
        if blux > 675: # adjust for the size of the ball
            bluxdirection = -1
            draxdirection = 1
        # check for top and bottom boundary
        if bluy < 0:
            bluydirection = 1
            draydirection = -1
        if bluy > 475 : # adjust for the size of the ball
            bluydirection = -1
            draydirection = 1

        # boundaries for tennis ball
        # check for right and left side boundary
        if tenx < 0:
            tenxdirection = 1
            draxdirection = -1
        if tenx > 675: # adjust for the size of the ball
            tenxdirection = -1
            draxdirection = 1
        # check for top and bottom boundary
        if teny < 0:
            tenydirection = 1
            draydirection = -1
        if teny > 475 : # adjust for the size of the ball
            tenydirection = -1
            draydirection = 1

    # boundaries for dragon ball
    # check for right and left side boundary
    if drax < 0:
        draxdirection = 1
    if drax > 675: # adjust for the size of the ball
        draxdirection = -1
    # check for top and bottom boundary
    if dray < 0:
        draydirection = 1
    if dray > 475 : # adjust for the size of the ball
        draydirection = -1


    # lets do interaction why not
    # blue and tennis
    if tenx in range(blux, blux+25) and teny in range(bluy, bluy+25):
        tenxdirection = tenxdirection*-1
        tenydirection = tenydirection*-1
        bluxdirection = bluxdirection*-1
        bluydirection = bluydirection*-1
    # dragonball and tennis
    if tenx in range(drax, drax+25) and teny in range(dray, dray+25):
        tenxdirection = tenxdirection*-1
        tenydirection = tenydirection*-1
        draxdirection = draxdirection*-1
        draydirection = draydirection*-1
    # blue and dragonball
    if drax in range(blux, blux+25) and dray in range(bluy, bluy+25):
        draxdirection = draxdirection*-1
        draydirection = draydirection*-1
        bluxdirection = bluxdirection*-1
        bluydirection = bluydirection*-1

    c.moveto(blue_ball, blux, bluy)   # move ball to new location
    c.moveto(tennis_ball, tenx, teny)
    c.moveto(dragonball, drax, dray)
    time.sleep(.01)                   # delay
    root.update()                     # redraw canvas


root.mainloop()