#
# Copyright 2023, Ali Arya
# Anyone Can Code: Algorithmic Thinking
# Example 4.2p
#

# graphics.py
# http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/graphics.html 
# http://www.pas.rochester.edu/~rsarkis/csc161/python/pip-graphics.html

# graphic library
from graphics import *
# for timer and sleep functions
import time
import random

#simple game
def ggame():

    ######################################################################
    # INIT part of the game
    # things that are done only once at the start (initialization)
    ######################################################################

    window = GraphWin('Game', 600, 600) # give title and dimensions
    #win.yUp() # make right side up coordinates, if preferred
 
    # Loop until the user clicks the close button.
    quit = False
    win = False

    # title
    label = Text(Point(20, 10), 'Game')
    label.draw(window)

    # graphics.py supports images and also basic graphical objects like circle 
    # we use both to show examples

    #player
    playerX = 0
    playerY = 0
    playerR = 30    #radius
    player = Circle(Point(playerX,playerY), playerR) # set center and radius
    player.setFill("yellow")

    #enemy
    enemyX = 100
    enemyY = 100
    enemy = Image(Point(enemyX, enemyY), "enemy.png")

    #prize
    prizeX = 200
    prizeY = 200
    prize = Image(Point(prizeX, prizeY), "prize.png")

    #draw in the order we want to see on screen (last one will be on top)
    prize.draw(window)
    enemy.draw(window)
    player.draw(window)     

    
    ######################################################################
    # -------- Main game Loop -----------
    # things that are done repeatedly
    ######################################################################
    while not quit:
        ######################################################################
        # UPDATE part of the main loop
        # --- Game logic should go here
        ######################################################################

        #check if we hit the prize
        #note that in graphics.py x and y are the coordinates for the centre of the objects
        distanceX = abs(playerX - prizeX)
        distanceY = abs(playerY - prizeY)
        if distanceX<10 and distanceY<10:
            prize.move(1000,1000)   #make it disappear (go off screen). There are other ways too!
            quit = True
            win = True

        #check if we hit the enemy. End the game if so
        #note that in graphics.py x and y are the coordinates for the centre of the objects
        distanceX = abs(playerX - enemyX)
        distanceY = abs(playerY - enemyY)
        if distanceX<10 and distanceY<10:
            quit = True

        #move the player
        dx = 0
        dy = 0  #remember that positive y is down
        needsMove = False
        key = window.checkKey()

        if key=='Escape':
            quit = True
        if key=='Up':
            dy -= 1
        if key=='Down':
            dy += 1
        if key=='Right':
            dx += 1
        if key=='Left':
            dx -= 1
        if dx != 0 or dy != 0:
            needsMove = True
 
        #animate the enemy (this only does the upadting part, no drawing)
        enemyMoveX = random.randint(-2,2)
        enemyMoveY = random.randint(-2,2)

        ######################################################################
        # DRAW part of the main loop
        # --- Rendering code goes here
        ######################################################################
        #in graphics.py the move() function actually moves and draws
        #no need to draw objects if they don't move/change, as they "stay" on screen
        if needsMove == True:
            player.move(dx,dy)    #the parameters are the amounts of movement in X and Y directions, not the destination
            playerX += dx
            playerY += dy
        
        #enemy needs drawing 
        enemy.move(enemyMoveX,enemyMoveY)

        time.sleep(0.05)

    window.close()
    if win == True:
        print("you win!")
    else:
        print("you lose!")

#starting point of the program
ggame()