# Day 21
#                   _                         _                                              _
#    /\ /\___ _   _| |__   ___   __ _ _ __ __| |   /\/\   _____   _____ _ __ ___   ___ _ __ | |_
#   / //_/ _ \ | | | '_ \ / _ \ / _` | '__/ _` |  /    \ / _ \ \ / / _ \ '_ ` _ \ / _ \ '_ \| __|
#  / __ \  __/ |_| | |_) | (_) | (_| | | | (_| | / /\/\ \ (_) \ V /  __/ | | | | |  __/ | | | |_
#  \/  \/\___|\__, |_.__/ \___/ \__,_|_|  \__,_| \/    \/\___/ \_/ \___|_| |_| |_|\___|_| |_|\__|
#             |___/

from tkinter import *
import time
import random
# import os

# tried to play music
# os.system("mpg123 /home/jasonjas/Documents/Python/Work/phodo/what.mp3")

# window
root = Tk()
root.title("astroids")
root.geometry("900x700+0+0")
root.resizable(True, True)

# Canvas
c = Canvas(root, width=900, height=600, bg="black")
c.place(x=0, y=0)

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


def checkwrap():
    # check right wall for wrap
    global x, y

    if x > 900:
        x = -180

    if y > 600:
        y = -90

    if x < -180:
        x = 900

    if y < -90:
        y = 600


def Shootstar():
    global starx, stary
    if starx < 0 and stary > 600:
        starx = random.randint(900, 1000)
        stary = random.randint(-100, 500)


def moveright(event):
    global xdir
    xdir += 0.5
    if xdir > 50:
        xdir = 50


def moveleft(event):
    global xdir
    xdir -= 0.5
    if xdir < -50:
        xdir = -50


def moveup(event):
    global ydir
    ydir -= 0.5
    if ydir < -50:
        ydir = -50


def movedown(event):
    global ydir
    ydir += 0.5
    if ydir > 50:
        ydir = 50


def teleport(event):
    global x
    global y
    x = random.randint(0, 900) - 90
    y = random.randint(0, 600) - 45


def funny(event):
    c.itemconfig(ship, image=funnyimg)


def funnystop(event):
    c.itemconfig(ship, image=shipimg)


# _,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,__,.-'~'-.,_
# declaring variables


# background
bgimg1 = PhotoImage(file="phodo/spacebg-1.png")
bgimg2 = PhotoImage(file="phodo/spacebg-2.png")
bgimg3 = PhotoImage(file="phodo/spacebg-3.png")
bgimg4 = PhotoImage(file="phodo/spacebg-4.png")
bg = c.create_image(450, 300, image=bgimg1)
twink = kle = 0

# Ship variables
x = 300
y = 300
xdir = 0
ydir = 0
shipimg = PhotoImage(file="phodo/ship.png").subsample(10, 10)
ship = c.create_image(x, y, image=shipimg)

# interface
xaxis = Label(c, foreground="red", text=f"X-Speed = {xdir}")
yaxis = Label(c, foreground="red", text=f"Y-Speed = {ydir}")
xaxis.place(x=10, y=580)
yaxis.place(x=100, y=580)

# shooting stars
starimg = PhotoImage(file="phodo/shootingstar.png").subsample(4, 4)
starx = 0
stary = 0
star = c.create_image(starx, stary, image=starimg)

# # portals
# blueportalimg = PhotoImage(file="phodo/blueportal.png").subsample(6,6)
# orangeportalimg = PhotoImage(file="phodo/orangeportal.png").subsample(6,6)
# blueportal = c.create_image(100, 150, image=blueportalimg)
# orageportal = c.create_image(500, 550, image=orangeportalimg)
# r = c.create_rectangle(50, 100, 150, 200, fill="red")

# funny image
funnyimg = PhotoImage(file="phodo/glorb.png").subsample(12, 12)

# keybinds
root.bind("<Right>", moveright)
root.bind("<Left>", moveleft)
root.bind("<Up>", moveup)
root.bind("<Down>", movedown)
root.bind("z", teleport)
root.bind("<KeyPress-x>", funny)
root.bind("<KeyRelease-x>", funnystop)


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

moving = True

while moving:

    # move in x and y directions
    x = x + xdir
    y = y + ydir

    # twinkle background
    twink += 1
    if twink == 4:
        twink = 0
        kle += 1
        if kle == 1:
            c.itemconfig(bg, image=bgimg1)
        elif kle == 2:
            c.itemconfig(bg, image=bgimg2)
        elif kle == 3:
            c.itemconfig(bg, image=bgimg3)
        else:
            kle = 0
            c.itemconfig(bg, image=bgimg4)

    # update interface
    xaxis.config(text=f"X-Speed = {xdir}")
    yaxis.config(text=f"Y-Speed = {ydir}")

    # move star and ships
    starx = starx - 100
    stary = stary + 30
    checkwrap()
    Shootstar()
    c.moveto(star, starx, stary)
    c.moveto(ship, x, y)  # move ship after to make star behined

    time.sleep(0.05)
    root.update()  # redraw canvas

mainloop()