"""
Created on Sep 30, 2022

@author: YOUR NAME HERE!!

This module represents a template for making Arcade games.
"""
import random
import arcade


# Choose a name for your game to appear in the title bar of the game window
gameName = 'TO BE DETERMINED'
# Choose the size of your game window
screenWidth = 1000
screenHeight = 1000
# Choose how fast player moves
speedIncrement = 0.25
speedMax = 5


class Mover(arcade.SpriteCircle):
    """
    This class represents a user controlled object that moves around the screen.
    """
    def __init__(self, x, y, size, color):
        # create with radius and color
        super().__init__(size, color)
        # center_x and center_y are inherited variables from Sprite superclass to represent position (x, y)
        self.center_x = x
        self.center_y = y
        # change_x and change_y are inherited variables from Sprite superclass to represent movement (dx, dy)
        self.change_x = 0
        self.change_y = 0

    def reset(self):
        """
        Reset by randomizing velocity.
        """
        self.change_x = random.randint(-2, 2)
        self.change_y = random.randint(-2, 2)


class Target(arcade.SpriteSolidColor):
    """
    This class represents a target to try to hit.
    """
    def __init__(self, size, color):
        # create with width and height in pixels and its color
        super().__init__(size, size, color)

    def reset(self):
        """
        Reset to a random location.
        """
        # Note coordinates go from 0 to SIZE in both width and height
        self.center_x = random.randint(0, screenWidth)
        self.center_y = random.randint(0, screenHeight)


class Game(arcade.Window):
    """
    This class represents the entire game:
    - setting up any objects to appear in game
    - updating their values (in method on_update)
    - drawing them (in method on_draw)
    - responding to key input (in method on_key_press)
    - responding to mouse input (in method on_mouse_press)
    """
    def __init__(self):
        # create with size (width, height) and a title
        super().__init__(screenWidth, screenHeight, gameName)
        # create our own game objects to show during the game
        self.mover = Mover(screenWidth // 2, screenHeight // 2, 10, arcade.color.ORCHID)
        self.target = Target(40, arcade.color.DUKE_BLUE)

    def setup(self):
        """
        Set up the initial game scene at the beginning of the game or to allow playing again after a win or loss
        """
        arcade.set_background_color(arcade.color.LIGHT_BLUE)
        self.mover.reset()
        self.target.reset()

    def on_key_press(self, key, modifiers):
        """
        Called whenever a key is pressed.
        """
        print(self.mover.change_x, self.mover.change_y)
        if key == arcade.key.UP:
            self.mover.change_y = min(self.mover.change_y + speedIncrement, speedMax)
        if key == arcade.key.DOWN:
            self.mover.change_y = max(self.mover.change_y - speedIncrement, -speedMax)
        if key == arcade.key.LEFT:
            self.mover.change_x = max(self.mover.change_x - speedIncrement, -speedMax)
        if key == arcade.key.RIGHT:
            self.mover.change_x = min(self.mover.change_x + speedIncrement, speedMax)

    def on_mouse_press(self, x, y, button, modifiers):
        """
        Called whenever a mouse button is pressed.
        """
        self.mover.reset()

    def on_draw(self):
        """
        Render all the game objects on the screen.
        """
        # DO NOT CHANGE -- always clear the screen as the FIRST step
        self.clear()
        # now draw your game objects
        self.mover.draw()
        self.target.draw()

    def on_update(self, dt):
        """
        Handle game "rules" for every step (i.e., frame or "moment"):
         - movement: move the game objects each step
         - collisions: check if the game objects collided with each other or the sides and respond appropriately
        """
        # move game object based on its speed (built into Arcade Sprites when using proper inherited attributes)
        self.mover.update()

        # check if mover hits left or right side, wrap it to the other side
        if self.mover.center_x < 0:
            self.mover.center_x = screenWidth
        elif self.mover.center_x > screenWidth:
            self.mover.center_x = 0

        # check if mover hits top or bottom side, wrap it to the other side
        if self.mover.center_y < 0:
            self.mover.center_y = screenHeight
        elif self.mover.center_y > screenHeight:
            self.mover.center_y = 0

        # check if mover hits target, move target to new random location and "bounce" mover
        if arcade.check_for_collision(self.target, self.mover):
            self.target.reset()


# Create the game and set initial scene
game = Game()
game.setup()
# Play the game forever
arcade.run()
