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

pygame.init()

FPS = 30
FramePerSec = pygame.time.Clock()

# Define some colours for drawing later
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
LIGHTBLUE = (150,200,255)

SCREEN_WIDTH = 600
SCREEN_HEIGHT = 600

# Set up the screen/window
DISPLAYSURF = pygame.display.set_mode((600, 600))
DISPLAYSURF.fill(WHITE)
pygame.display.set_caption("Makey Makey Grid")

#Setting up Fonts
font = pygame.font.SysFont("Verdana", 60)
font_small = pygame.font.SysFont("Verdana", 20)
game_over = font.render("Game Over", True, BLACK)

draw_circle = False

######################################################################
# 18x18 grid (two Makey Makeys)
l1 = "......######......"
l2 = "....##########...."
l3 = "...############..."
l4 = "..##############.."
l5 = ".################."
l6 = ".################."
l7 = "##################"
l8 = "##################"
l9 = "##################"
l10 = "##################"
l11 = "##################"
l12 = "##################"
l13 = ".################."
l14 = ".################."
l15 = "..##############.."
l16 = "...############..."
l17 = "....##########...."
l18 = "......######......"

global map
map = [
    l18, l17, l16, l15, l14, l13, l12, l11, l10, l9, l8, l7, l6, l5, l4, l3,
    l2, l1
]

# Key dictionaries
xkeys = [
    K_a, K_b, K_c, K_d, K_e, K_f, K_g, K_h, K_i, K_j, K_k, K_l, K_m, K_n, K_o,
    K_p, K_q, K_r
]
ykeys = [
    K_UP,
    K_DOWN,
    K_LEFT,
    K_RIGHT,
    K_SPACE,
    K_s,
    K_t,
    K_u,
    K_0,
    K_1,
    K_2,
    K_3,
    K_4,
    K_5,
    K_6,
    K_7,
    K_8,
    K_9,
]

# Drawing coordinates
coordinates_list = [pygame.math.Vector2] * 1
coordinates_list[0] = pygame.math.Vector2(20,20)

# Class for the 'hit' indicator
class HitIndicator(pygame.sprite.Sprite):

    prev_x = -1  # Track when x and y values _change_
    prev_y = -1

    def __init__(self):
        super().__init__()
        self.image = pygame.image.load("dot.png")
        self.rect = self.image.get_rect()
        self.rect.center = (25, 25)

    def move(self):

        # Get key input
        xcoord = -1
        ycoord = -1
        # Check for x coordinate
        for k in xkeys:
            if pressed_keys[k]:
                # An x key was pressed
                xcoord = xkeys.index(k)
        for k in ykeys:
            if pressed_keys[k]:
                ycoord = ykeys.index(k)

        if xcoord != -1 and ycoord != -1 and (xcoord != self.prev_x
                                              or ycoord != self.prev_y):
            # Draw at new position (graphics only)
            print(xcoord, ycoord)
            self.rect.center = (50 + (xcoord * 20), 50 + (ycoord * 20))
            self.prev_x = xcoord
            self.prev_y = ycoord

        # Add coordinates to list
        coordinates_list.append(pygame.math.Vector2(self.rect.left, self.rect.top))

    def draw(self, surface):
        # Draw self
        surface.blit(self.image, self.rect)


# Make one sprite
indicator = HitIndicator()

# ************** MAIN PYGAME LOOP/graphics update *************
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # Redraws the background every frame
    #DISPLAYSURF.fill(WHITE)
    DISPLAYSURF.fill(WHITE)

    # Draw 18x18 grid
    for i in range(17):
        x = 50 + (i * 20)
        for j in range(17):
            y = 50 + (j * 20)
            pygame.draw.rect(DISPLAYSURF, LIGHTBLUE, pygame.Rect(x, y, 20, 20), 1)
    
    pressed_keys = pygame.key.get_pressed()

    # Tick/update
    indicator.move()
    DISPLAYSURF.blit(indicator.image, indicator.rect)

    # Draw lines
    if len(coordinates_list) > 1:
      pygame.draw.lines(DISPLAYSURF, BLUE, False, coordinates_list, 1)

    pygame.display.update()
  
    FramePerSec.tick(FPS)
