#Bryan Medina
#6/18/17
#movement.py
#A look at movement with the arrow keys

# Edited on 3/23/2020 by Arup Guha for COP 2930 - added a couple key events
# Pressing 0 moves circle to top left.
# Pressing 4 starts the circle moving counter-clockwise in a circle and ignores
#            all other keyboard input!

import pygame, sys
from pygame.locals import *
import math

pygame.init()
DISPLAYSURF = pygame.display.set_mode((600, 600)) #Creating a window...
pygame.display.set_caption("Movement Demo!") #Caption at the top of the win

black   = pygame.Color(0  ,0  ,0)
purple  = pygame.Color(255,0  ,255)

x  = 300 #The balls initial x position!
y  = 300 #The balls initial y position!
dx = 5   #How fast the ball is moving in the x
dy = 5   #How fast the ball is moving in the y

angle = 0
dAngle = 0
spinCircle = False
clock = pygame.time.Clock()

while True: #Game Loop

    for event in pygame.event.get():

        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if spinCircle:
            break

        if event.type == KEYDOWN: #If any key on the keyboard is pressed, then we want to determine which key was pressed...
            #Our basic structure for checking to see which key was pressed is to use if statements checking if event.key equals a certain keycde
            
            if event.key == K_DOWN: #If the down arrow was pressed, then we want our ball to move downward
                y += dy
            if event.key == K_UP: #If the up arrow was pressed, then we want our ball to move upward
                y -= dy
            if event.key == K_LEFT: #If the left arrow was pressed, then we want our ball to move to the left
                x -= dx
            if event.key == K_RIGHT: #If the right arrow was pressed, then we want our ball to move to the right
                x += dx

            # Move back to top left if we press 0 key.
            if event.key == K_0:
                x = 15
                y = 25

            # Our mode where we only spin in a circle and ignore the rest of
            # the input.
            if event.key == K_4:
                spinCircle = True
                dAngle = math.pi/100
                
        #Each key on the keyboard has a specific key code.
        #If you need a specific one, please check out this link:
        #https://www.pygame.org/docs/ref/key.html

    if spinCircle:
        angle += dAngle
        x = int(300 + 250*math.cos(angle))
        y = int(300 + 250*math.sin(angle))
        

    DISPLAYSURF.fill(black) #make the background black
    pygame.draw.circle(DISPLAYSURF, purple, (x, y), 20, 0)
    pygame.display.update() #Updates the frame
    clock.tick(100)


