import pygame
#import random
import time
from pygame.locals import *
import random
import asyncio


if sys.platform == "emscripten":
    pass


if 'wasm' in __import__('platform').machine():
    pass



pygame.init()

blank_image = pygame.image.load('blank.png')

# Set the size of the window
window_size = (850, 750)
screen = pygame.display.set_mode(window_size)
pygame.display.set_caption("MISSILE WARS")

font = pygame.font.Font("Andika-R.ttf", 30)
paused_font = pygame.font.Font("Andika-R.ttf", 80)
funds_font = pygame.font.Font("Andika-R.ttf", 50)

class warning_image_class:
    def __init__ (self, image):
        self.image = image


class attack_unit:
    def __init__ (self, building_type, image , x, y, x1, y1, x2, y2, y3):
        self.building_type = building_type
        self.damage = attack_unit_damage[building_type]
        self.image = image
        self.original_x = x
        self.original_y = y
        self.target_x = x1
        self.target_y = y1
        self.current_x = x2
        self.current_y = y2
        self.when_hit_y = y3
        self.duration = 0
        self.rect = self.image.get_rect()
    #def set_unit(self, building_type):
    #    self.damage = attack_unit_damage[building_type]


class defense_unit:
    def __init__ (self, building_type, image , x, y, x1, y1, x2, y2):
        self.building_type = building_type
        self.image = image
        self.original_x = x
        self.original_y = y
        self.target_x = x1
        self.target_y = y1
        self.current_x = x2
        self.current_y = y2
        self.rect = self.image.get_rect()
        self.fired = False


class highlighter:
    def __init__ (self, image, x, y):
        self.image = image
        self.x = x
        self.y = y


class tile_player1:
    def __init__ (self, image, building_type, x, y, hit_points, upgraded, attack_unit):
        self.image = image
        self.building_type = building_type
        self.x = x
        self.y = y
        self.hit_points = hit_points
        self.upgraded = upgraded
        # self.building_type = building_type
        self.attack_unit = attack_unit
        # print(building_type)
        self.upgrade = ""
        # if self.building_type != "blank":
        #     if self.building_type == "a":
        #         self.upgrade = green_a_upgraded
        #     elif self.building_type == "m":
        #         self.upgrade = green_m_upgraded
        #     elif self.building_type == "mm":
        #         self.upgrade = green_mm_upgraded
        # else:
        #     self.upgrade = ""
        
    def upgrade_building(self):
        if self.building_type != "blank":
            if self.building_type == "a":
                self.upgrade = green_a_upgraded
                self.hit_points = building_hit_points_dict["a_upgraded"]
                self.building_type = "a_upgraded"
                self.upgraded = True
            elif self.building_type == "m":
                self.upgrade = green_m_upgraded
                self.hit_points = building_hit_points_dict["m_upgraded"]
                self.building_type = "m_upgraded"
                self.upgraded = True
            # elif self.building_type == "mm":
            #     self.upgrade = green_mm_upgraded
            #     self.hit_points = building_hit_points_dict["mm_upgraded"]
            #     self.building_type = "mm_upgraded"
            return self.upgrade
        else:
            self.upgrade = "blank"
    
    def hit(self, damage):
        self.hit_points -= damage
        # print("Hit points" + str(self.hit_points))
        if self.hit_points <= 0:
            self.image = brown
            self.building_type = "brown"
            self.upgrade = "blank"
            self.upgraded = False
            self.attack_unit = False


class tile_player2:
    def __init__ (self, image, building_type, x, y, hit_points):
        self.image = image
        self.building_type = building_type
        self.x = x
        self.y = y
        # self.building_type = building_type
        self.hit_points = hit_points
    
    def hit(self, damage):
        self.hit_points -= damage
        print(self.hit_points)
        if self.hit_points <= 0:
            self.image = brown
            self.building_type = "brown"


class game_settings:
    def __init__ (self, mode):
        self.mode = mode
        self.turn = ""
        self.paused = False


class player_1:
    def __init__(self):
        self.score = 0


class player_2:
    def __init__(self):
        self.score = 0


x = 73
y = 164


blue = pygame.image.load('blue-square.png')
green = pygame.image.load('green-square.png')
blowing_up = pygame.image.load('blowing-up-square.png')
brown = pygame.image.load('brown-square.png')
green_a = pygame.image.load('green-square-a.png')
green_a_upgraded = pygame.image.load('green-square-a-upgraded.png')
green_g = pygame.image.load('green-square-g.png')
green_r = pygame.image.load('green-square-r.png')
green_m = pygame.image.load('green-square-m.png')
green_m_upgraded = pygame.image.load('green-square-m-upgraded.png')
green_mm = pygame.image.load('green-square-mm.png')
green_mm_upgraded = pygame.image.load('green-square-mm-upgraded.png')
green_b = pygame.image.load('green-square-b.png')





grid0_0_0 = tile_player1(blue, "blue", 50, 50, 0, False, False)
grid0_0_1 = tile_player1(blue, "blue", 100, 50, 0, False, False)
grid0_0_2 = tile_player1(blue, "blue", 150, 50, 0, False, False)
grid0_0_3 = tile_player1(blue, "blue", 200, 50, 0, False, False)
grid0_0_4 = tile_player1(blue, "blue", 250, 50, 0, False, False)
grid0_0_5 = tile_player1(blue, "blue", 300, 50, 0, False, False)

grid0_1_0 = tile_player1(blue, "blue", 50, 100, 0, False, False)
grid0_1_1 = tile_player1(green_r, "r", 100, 100, 0, False, False)
grid0_1_2 = tile_player1(green_a_upgraded, "a_upgraded", 150, 100, 0, False, False)
grid0_1_3 = tile_player1(green, "green", 200, 100, 0, False, False)
grid0_1_4 = tile_player1(green, "green", 250, 100, 0, False, False)
grid0_1_5 = tile_player1(blue, "blue", 300, 100, 0, False, False)

grid0_2_0 = tile_player1(blue, "blue", 50, 150, 0, False, False)
grid0_2_1 = tile_player1(green_m, "m", 100, 150, 20, False, True)
grid0_2_2 = tile_player1(green, "green", 150, 150, 0, False, False)
grid0_2_3 = tile_player1(green, "green", 200, 150, 0, False, False)
grid0_2_4 = tile_player1(green_a, "a", 250, 150, 0, False, False)
grid0_2_5 = tile_player1(blue, "blue", 300, 150, 0, False, False)

grid0_3_0 = tile_player1(blue, "blue", 50, 200, 0, False, False)
grid0_3_1 = tile_player1(green, "green", 100, 200, 0, False, False)
grid0_3_2 = tile_player1(green, "green", 150, 200, 0, False, False)
grid0_3_3 = tile_player1(green_r, "r", 200, 200, 0, False, False)
grid0_3_4 = tile_player1(green, "green", 250, 200, 0, False, False)
grid0_3_5 = tile_player1(blue, "blue", 300, 200, 0, False, False)

grid0_4_0 = tile_player1(blue, "blue", 50, 250, 0, False, False)
grid0_4_1 = tile_player1(green, "green", 100, 250, 0, False, False)
grid0_4_2 = tile_player1(green_a, "a", 150, 250, 0, False, False)
grid0_4_3 = tile_player1(green, "green", 200, 250, 0, False, False)
grid0_4_4 = tile_player1(green_a_upgraded, "a_upgraded", 250, 250, 0, False, False)
grid0_4_5 = tile_player1(blue, "blue", 300, 250, 0, False, False)

grid0_5_0 = tile_player1(blue, "blue", 50, 300, 0, False, False)
grid0_5_1 = tile_player1(blue, "blue", 100, 300, 0, False, False)
grid0_5_2 = tile_player1(blue, "blue", 150, 300, 0, False, False)
grid0_5_3 = tile_player1(blue, "blue", 200, 300, 0, False, False)
grid0_5_4 = tile_player1(blue, "blue", 250, 300, 0, False, False)
grid0_5_5 = tile_player1(blue, "blue", 300, 300, 0, False, False)



############################################################
def make_map_square(x, y):
    random_number = random.randint(0, 15)
    square_hit_points = {"a": 20, "r": 20, "m": 20, "m_upgraded": 20, "a_upgraded": 20, "green": 0}
    map_square_types = ["green", "a", "m", "r", "a", "m_upgraded", "green", "a", "r", "a_upgraded", "a", "green", "a", "green", "r", "m"]
    map_square_images = [green, green_a, green_m, green_r, green_a, green_m_upgraded, green, green_a, green_r, green_a_upgraded, green_a, green, green_a, green, green_r, green_m]
    
    square_type = map_square_types[random_number]
    square_image = map_square_images[random_number]
    
    map_object = tile_player2(square_image, square_type, x, y, square_hit_points[map_square_types[random_number]])
    
    
    return map_object



grid1_0_0 = tile_player2(blue, "blue", 500, 400, 0)
grid1_0_1 = tile_player2(blue, "blue", 550, 400, 0)
grid1_0_2 = tile_player2(blue, "blue", 600, 400, 0)
grid1_0_3 = tile_player2(blue, "blue", 650, 400, 0)
grid1_0_4 = tile_player2(blue, "blue", 700, 400, 0)
grid1_0_5 = tile_player2(blue, "blue", 750, 400, 0)

#X(starting):500   Y:450
grid1_1_0 = tile_player2(blue, "blue", 500, 450, 0)
grid1_1_1 = make_map_square(550, 450)
grid1_1_2 = make_map_square(600, 450)
grid1_1_3 = make_map_square(650, 450)
grid1_1_4 = make_map_square(700, 450)
grid1_1_5 = tile_player2(blue, "blue", 750, 450, 0)

#X(starting):500   Y:500
grid1_2_0 = tile_player2(blue, "blue", 500, 500, 0)
grid1_2_1 = make_map_square(550, 500)
grid1_2_2 = make_map_square(600, 500)
grid1_2_3 = make_map_square(650, 500)
grid1_2_4 = make_map_square(700, 500)
grid1_2_5 = tile_player2(blue, "blue", 750, 500, 0)

#X(starting):500   Y:550
grid1_3_0 = tile_player2(blue, "blue", 500, 550, 0)
grid1_3_1 = make_map_square(550, 550)
grid1_3_2 = make_map_square(600, 550)
grid1_3_3 = make_map_square(650, 550)
grid1_3_4 = make_map_square(700, 550)
grid1_3_5 = tile_player2(blue, "blue", 750, 550, 0)

#X(starting):500   Y:600
grid1_4_0 = tile_player2(blue, "blue", 500, 600, 0)
grid1_4_1 = make_map_square(550, 600)
grid1_4_2 = make_map_square(600, 600)
grid1_4_3 = make_map_square(650, 600)
grid1_4_4 = make_map_square(700, 600)
grid1_4_5 = tile_player2(blue, "blue", 750, 600, 0)

grid1_5_0 = tile_player2(blue, "blue", 500, 650, 0)
grid1_5_1 = tile_player2(blue, "blue", 550, 650, 0)
grid1_5_2 = tile_player2(blue, "blue", 600, 650, 0)
grid1_5_3 = tile_player2(blue, "blue", 650, 650, 0)
grid1_5_4 = tile_player2(blue, "blue", 700, 650, 0)
grid1_5_5 = tile_player2(blue, "blue", 750, 650, 0)




x = 50
y = 100

build_highlighter = pygame.image.load('square-highlighter.png')
attack_highlighter = pygame.image.load('attack-highlighter.png')
build_image = pygame.image.load('build-image.png')
build_a = pygame.image.load('build-a.png')
build_g = pygame.image.load('build-g.png')
build_r = pygame.image.load('build-r.png')
build_m = pygame.image.load('build-m.png')
build_mm = pygame.image.load('build-mm.png')
build_destroy = pygame.image.load('build-destroy.png')
build_upgrade = pygame.image.load('build-upgrade.png')
build_clear = pygame.image.load('build-clear.png')
build_attack = pygame.image.load('build-attack.png')



building_hit_points_dict = {"a": 20, "r": 20, "mm": 40, "g": 40, "m": 20, "mm_upgraded": 60, "m_upgraded": 20, "a_upgraded": 20}
building_cost = {"a": 20, "r": 50, "m": 20, "upgrade": 50}
tool_image_list = [build_a, build_r, build_m, build_destroy, build_upgrade, build_clear, build_attack]
tool_image_square_list = [green_a, green_r, green_m, green, "upgrade", "clear", "attack"]
tool_image_list_index = 0
build_building_image = build_a


cannot_build_on_water = pygame.image.load('cannot-build-on-water.png')
cannot_build_on_building = pygame.image.load('cannot-build-on-building.png')

cannot_destroy_land = pygame.image.load('cannot-destroy-land.png')
cannot_destroy_water = pygame.image.load('cannot-destroy-water.png') 

cannot_upgrade_building = pygame.image.load('cannot-upgrade-building.png')
cannot_upgrade_already_upgraded_building = pygame.image.load('cannot-upgrade-already-upgraded-building.png')
cannot_upgrade_water = pygame.image.load('cannot-upgrade-water.png')
cannot_upgrade_land = pygame.image.load('cannot-upgrade-land.png')


do_not_need_to_clear_land = pygame.image.load('do-not-need-to-clear-land.png')
cannot_clear_water = pygame.image.load('cannot-clear-water.png')
do_not_use_clear_to_clear_building = pygame.image.load('do-not-use-clear-to-clear-building.png')

unit_does_not_attack = pygame.image.load('unit-does-not-attack.png')

missiles_were_shot_down = pygame.image.load('missiles-were-shot-down.png')
missile_was_shot_down = pygame.image.load('missile-was-shot-down.png')

missile_blue = pygame.image.load('missile-blue.png')
missile_blue_double = pygame.image.load('missile-blue-double.png')
missile_white = pygame.image.load('missile-white.png')
missile_explosion = pygame.image.load('explosion.png')
big_explosion = pygame.image.load('big-explosion.png')

funds_box = pygame.image.load('funds-box.png')

pygame.key.set_repeat(2000, 2000)

highlighter.image = build_highlighter
highlighter.x = 100
highlighter.y = 150

game_settings.mode = "build"
game_settings.turn = "player_1"
game_settings.paused = False

player_1.score = 0
player_2.score = 0

attack_unit.building_type = "m"
attack_unit.image = blank_image
attack_unit.original_x = 0
attack_unit.original_y = 0
attack_unit.target_x = 0
attack_unit.target_y = 0
attack_unit.current_x = 0
attack_unit.current_y = 0
attack_unit.when_hit_y = 0
attack_unit.duration = 0
attack_unit.rect = 0
attack_unit_damage = {"m": 20, "m_upgraded": 40}

defense_unit.building_type = "a"
defense_unit.image = blank_image
defense_unit.original_x = 0
defense_unit.original_y = 0
defense_unit.target_x = 0
defense_unit.target_y = 0
defense_unit.current_x = 550
defense_unit.current_y = 450
defense_unit.fired = False

warning_image = ""
warning_image_class.image = blank_image


launch_sound0 = pygame.mixer.Sound("Explosion-v1-Missile War Sounds.wav")
launch_sound1 = pygame.mixer.Sound("Explosion-v3-Missile War Sounds.wav")
explosion_sound0 = pygame.mixer.Sound("Explosion-v4-Missile War Sounds.wav")
explosion_sound1 = pygame.mixer.Sound("Explosion-v5-Missile War Sounds.wav")


def hit_and_destroy(grid):
    x = attack_unit.target_x
    y = attack_unit.target_y
    for map_object in grid:
        sx = map_object.x
        sy = map_object.y
        # print(dir(attack_unit))
        damage = attack_unit.damage


        if (sx == x) and (sy == y):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x + 50) and (sy == y):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x) and (sy == y + 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x + 50) and (sy == y + 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x - 50) and (sy == y):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x - 50) and (sy == y - 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x + 50) and (sy == y - 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x - 50) and (sy == y + 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
        elif (sx == x) and (sy == y - 50):
            if map_object.building_type != "blue":
                if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
                    if game_settings.turn == "player_2":
                        player_2.score += 1
                    else:
                        player_1.score += 1
                map_object.hit(damage)
            elif map_object.building_type == "blue":
                print("YOU JUST HIT WATER!!!")
    
    pygame.mixer.Sound(explosion_sound1).play()


def do_tool(grid, tool, warning_image, grid2, funds_amount_player_1):
    x = highlighter.x
    y = highlighter.y
    warning_image_class.image = blank_image
    
    if game_settings.paused == False:
        if game_settings.mode != "attacking":
            for grid_object in grid:
                # sx = grid_object.x + 50 - (x)
                # sy = grid_object.y + 50 - (y)
                sx = grid_object.x
                sy = grid_object.y
                # if (sx <= 50 and sx >= 0) and (sy <= 50 and sy >= 0):
                if (sx == x) and (sy == y):
                    #print("Inside: ", coord, "x:  ", x, "y:  ", y, color.building_type)
                    #print(color.building_type)
                    if game_settings.mode == "build":
                        if tool != green and tool != "upgrade" and tool != "clear" and tool != "attack":
                            highlighter.image = build_highlighter
                            if grid_object.building_type == "green":
                                print("YOU CAN BUILD THERE!!!")
                                grid_object.image = tool
                                if tool == green_a:
                                    grid_object.building_type = "a"
                                    grid_object.attack_unit = False
                                    grid_object.hit_points = building_hit_points_dict["a"]
                                    funds_amount_player_1 -= building_cost["a"]
                                elif tool == green_g:
                                    grid_object.building_type = "g"
                                    grid_object.attack_unit = False
                                    grid_object.hit_points = building_hit_points_dict["g"]
                                elif tool == green_r:
                                    grid_object.building_type = "r"
                                    grid_object.attack_unit = False
                                    grid_object.hit_points = building_hit_points_dict["r"]
                                    funds_amount_player_1 -= building_cost["r"]
                                elif tool == green_m:
                                    grid_object.building_type = "m"
                                    grid_object.attack_unit = True
                                    grid_object.hit_points = building_hit_points_dict["m"]
                                    funds_amount_player_1 -= building_cost["m"]
                                elif tool == green_mm:
                                    grid_object.building_type = "mm"
                                    grid_object.attack_unit = True
                                    grid_object.hit_points = building_hit_points_dict["mm"]
                            elif grid_object.building_type == "blue":
                                print("YOU CAN NOT BUILD ON WATER!!!")
                                warning_image_class.image = cannot_build_on_water
                            elif grid_object.building_type == "brown":
                                print("YOU HAVE TO CLEAR THIS LAND TO BUILD ON IT!!!")
                            elif grid_object.building_type != "green" and grid_object.building_type != "blue":
                                print("YOU CAN NOT BUILD ON A BUILDING!!!")
                                warning_image_class.image = cannot_build_on_building
                        elif tool == green:
                            highlighter.image = build_highlighter
                            if grid_object.building_type != "green" and grid_object.building_type != "blue":
                                grid_object.image = green
                                grid_object.building_type = "green"
                                grid_object.hit_points = 0
                            elif grid_object.building_type == "green":
                                print("YOU CAN NOT DESTROY LAND!!!")
                                warning_image_class.image = cannot_destroy_land
                            elif grid_object.building_type == "blue":
                                print("YOU CAN NOT DESTROY WATER!!!")
                                warning_image_class.image = cannot_destroy_water
                        elif tool == "upgrade":
                            highlighter.image = build_highlighter
                            if grid_object.building_type != "green" and grid_object.building_type != "blue" and grid_object.upgraded == False and grid_object.image != green_g and grid_object.image != green_r:
                                grid_object.image = grid_object.upgrade_building()
                                grid_object.upgraded = True
                                funds_amount_player_1 -= building_cost["upgrade"]
                            elif grid_object.image == green_g or grid_object.image == green_r:
                                print("YOU CAN NOT UPGRADE THIS UNIT!!!")
                                warning_image_class.image = cannot_upgrade_building
                            elif grid_object.upgraded == True:
                                print("YOU CAN NOT UPGRADE A BULDING THAT HAS ALREADY BEEN UPGRADED!!!")
                                warning_image_class.image = cannot_upgrade_already_upgraded_building
                                # counter_tick.tick(40)
                            elif grid_object.building_type == "green":
                                print("YOU CAN NOT UPGRADE LAND!!!")
                                warning_image_class.image = cannot_upgrade_land
                            elif grid_object.building_type == "blue":
                                print("YOU CAN NOT UPGRADE WATER!!!")
                                warning_image_class.image = cannot_upgrade_water
                        elif tool == "clear":
                            highlighter.image = build_highlighter
                            if grid_object.building_type == "brown":
                                grid_object.image = green
                                grid_object.building_type = "green"
                            elif grid_object.building_type == "green":
                                print("YOU DO NOT NEED TO CLEAR GREEN LAND!!!")
                                warning_image_class.image = do_not_need_to_clear_land
                            elif grid_object.building_type == "blue":
                                print("YOU CAN NOT CLEAR WATER!!!")
                                warning_image_class.image = cannot_clear_water
                            elif grid_object.building_type == "green" and grid_object.building_type == "blue":
                                warning_image_class.image = do_not_use_clear_to_clear_building
                        elif tool == "attack":
                            # highlighter.image = attack_highlighter
                            game_settings.mode = "attack"
                            # attack(grid, grid2, x, y, tool_image, build_image)
                            print("ATTACK MODE!!!")
                    elif game_settings.mode == "attack":
                        if grid_object.attack_unit == True:
                            print(grid_object.building_type)
                            game_settings.mode = "attacking"
                            attack_unit.damage = attack_unit_damage[grid_object.building_type]
                            attack_unit.building_type = grid_object.building_type
                            attack_unit.original_x = grid_object.x
                            attack_unit.original_y = grid_object.y
                            highlighter.x = 550
                            highlighter.y = 450
                        elif grid_object.attack_unit == False:
                            print("THIS UNIT DOES NOT ATTACK!!!")
                            warning_image_class.image = unit_does_not_attack
        elif game_settings.mode == "attacking":
            x = highlighter.x
            y = highlighter.y
            for map_object in grid2:
                sx = map_object.x
                sy = map_object.y
                
                if (sx == x) and (sy == y):
                    attack_unit.target_x = map_object.x
                    attack_unit.target_y = map_object.y
                    if attack_unit.building_type == "m_upgraded":
                        attack_unit.image = missile_blue_double
                    elif attack_unit.building_type == "m":
                        attack_unit.image = missile_blue
                    pygame.mask.from_surface(attack_unit.image)
                    
                    attack_unit.current_x = attack_unit.original_x
                    attack_unit.current_y = attack_unit.original_y
                    
                    if attack_unit.building_type == "m":
                        funds_amount_player_1 -= 4
                    elif attack_unit.building_type == "m_upgraded":
                        funds_amount_player_1 -= 8
                    pygame.mixer.Sound(launch_sound0).play()
            
            highlighter.x = 100
            highlighter.y = 150

            warning_image_class.image = blank_image
    
    print(warning_image)
    return funds_amount_player_1




def check_collision_aa(pos1, pos2):
    collided = False
    dx = pos1[0] - pos2[0]
    dy = pos1[1] - pos2[1]
    distance0 = dx
    distance1 = dy
    
    if game_settings.turn == "player_1":
        if distance0 < 1.5 and distance0 < 1.5 and distance0 > -1.5 and distance1 > -1.5 and game_settings.mode == "attack_launch":
            collided = True
    else:
        if distance0 < 6 and distance1 < 6 and distance0 > -6 and distance1 > -6 and game_settings.mode == "attack_launch":
            collided = True
    
    return collided


def check_collision_missile(pos1, pos2):
    collided = False
    dx = pos1[0] - pos2[0]
    dy = pos1[1] - pos2[1]
    distance0 = dx
    distance1 = dy
    
    if abs(distance0) < 2 and abs(distance1) < 2 and game_settings.mode == "attack_launch":
        collided = True
    
    return collided


def missile_move(target_grid, defense_units, radar_units):
    au_current_pos = [attack_unit.current_x, attack_unit.current_y]
    au_target_pos = [attack_unit.target_x, attack_unit.target_y]
    
    # pygame.mask.from_surface(attack_unit.image)
    # print("AU.rect:  " + str(attack_unit.rect))
    
    if attack_unit.target_x != 0 and attack_unit.image != missile_explosion and attack_unit.image != big_explosion:
        
        # if (attack_unit.target_x / attack_unit.original_x) <= 1:
        #     attack_unit.current_x += (attack_unit.target_x / attack_unit.original_x)+0.5
        # else:
        #     attack_unit.current_x += (attack_unit.target_x / attack_unit.original_x)
        
        # if (attack_unit.target_y / attack_unit.original_y) <= 1:
        #     attack_unit.current_y += (attack_unit.target_y / attack_unit.original_y)+0.5
        # else:
        #     attack_unit.current_y += (attack_unit.target_y / attack_unit.original_y)
        
        ## Original missile flying pattern
        # attack_unit.current_x += ((attack_unit.target_x) / attack_unit.original_x)-4
        # attack_unit.current_y += ((attack_unit.target_y) / attack_unit.original_y)-2
        
        ## Improved missile flying pattern
        if game_settings.turn == "player_1":
            if attack_unit.duration > 50:
                attack_unit.current_x -= 1 / random.randint(1,50)*8
                attack_unit.current_y -= 1 / random.randint(1,100)*7.5
                attack_unit.duration -= 5
            else:
                if attack_unit.current_x <= attack_unit.target_x:
                    attack_unit.current_x += 1 / random.randint(1,50)*7
                    attack_unit.duration += 1
                else:
                    attack_unit.current_x -= 1 / random.randint(1,50)*7
                    attack_unit.duration += 1
                
                if attack_unit.current_y <= attack_unit.target_y:
                    attack_unit.current_y += 1 / random.randint(1,50)*5
                    attack_unit.duration += 1
                else:
                    attack_unit.current_y -= 1 / random.randint(1,50)*5
                    attack_unit.duration += 1
        elif game_settings.turn == "player_2":
            if attack_unit.duration > 50:
                attack_unit.current_x += 1 / random.randint(1,50)*8
                attack_unit.current_y += 1 / random.randint(1,100)*7.5
                attack_unit.duration -= 5
            else:
                if attack_unit.current_x >= attack_unit.target_x:
                    attack_unit.current_x -= 1 / random.randint(1,50)*7
                    attack_unit.duration += 1
                else:
                    attack_unit.current_x += 1 / random.randint(1,50)*7
                    attack_unit.duration += 1
                
                if attack_unit.current_y >= attack_unit.target_y:
                    attack_unit.current_y -= 1 / random.randint(1,50)*5
                    attack_unit.duration += 1
                else:
                    attack_unit.current_y += 1 / random.randint(1,50)*5
                    attack_unit.duration += 1
            # print(game_settings.turn)
        
        game_settings.mode = "attack_launch"
        
        #print("AUT.y DIVIDED BY AU.original_y:  " + str(attack_unit.target_y / attack_unit.original_y))
        #print("AUT.x DIVIDED BY AU.original_x:  " + str(attack_unit.target_x / attack_unit.original_x))
        #print("AU.x:  " + str(attack_unit.current_x) + "  AU.y:  " + str(attack_unit.current_y)+ "  AUTU.x:  " + str(attack_unit.target_x) + "  AUTU.y:  " + str(attack_unit.target_y))
        screen.blit(attack_unit.image, (attack_unit.current_x, attack_unit.current_y))
    
    if check_collision_missile(au_target_pos, au_current_pos) == True:
        if game_settings.turn == "player_1":
            print("THE MISSILE HIT THE TARGET!!!")
            attack_unit.image = big_explosion
            attack_unit.when_hit_y = attack_unit.current_y
            hit_and_destroy(target_grid)
            defense_units = find_aas(player_1_map)
            screen.blit(attack_unit.image, (attack_unit.current_x-50, attack_unit.current_y-55))
            
            random_number = random.randint(0, 5)
            if random_number > 2:
                game_settings.turn = "player_2"
        else:
            print("THE MISSILE HIT THE TARGET--PLAYER 2!!!")
            attack_unit.image = big_explosion
            attack_unit.when_hit_y = attack_unit.current_y
            hit_and_destroy(target_grid)
            defense_units = find_aas(target_grid)
            screen.blit(attack_unit.image, (attack_unit.current_x-50, attack_unit.current_y-50))
            
            game_settings.turn = "player_1"
            # random_number = random.randint(0, 5)
            # if random_number > 3:
                # game_settings.turn = "player_2"
    
    if attack_unit.image == missile_explosion:
        # if attack_unit.when_hit_y == 0:
        #     attack_unit.when_hit_y = attack_unit.current_y
        
        if attack_unit.current_y < (attack_unit.when_hit_y-50):
            attack_unit.image = blank_image
            attack_unit.current_x = 0
            attack_unit.current_y = 0
            attack_unit.target_x = 0
            attack_unit.target_y = 0
            attack_unit.when_hit_y = 0
            game_settings.mode = "build"
            if game_settings.turn == "player_2":
                game_settings.turn = "player_2"
        else:
            attack_unit.image = missile_explosion
            attack_unit.current_y -= 0.1
        screen.blit(attack_unit.image, (attack_unit.current_x, attack_unit.current_y))
    elif attack_unit.image == big_explosion:
        # if attack_unit.when_hit_y == 0:
        #     attack_unit.when_hit_y = attack_unit.current_y
        
        if attack_unit.current_y < (attack_unit.when_hit_y-50):
            attack_unit.image = blank_image
            attack_unit.current_x = 0
            attack_unit.current_y = 0
            attack_unit.target_x = 0
            attack_unit.target_y = 0
            attack_unit.when_hit_y = 0
            game_settings.mode = "build"
            if game_settings.turn == "player_2":
                game_settings.turn = "player_2"
        else:
            attack_unit.image = big_explosion
            attack_unit.current_y -= 0.1
            # print(attack_unit.when_hit_y)
        screen.blit(attack_unit.image, (attack_unit.current_x-50, attack_unit.current_y-50))
    
    units_to_update = [defense_units, radar_units]
    
    return units_to_update


def check_if_missile_is_close(pos1, pos2):
    close = False
    dx = pos1[0] - pos2[0]
    dy = pos1[1] - pos2[1]
    distance0 = dx
    distance1 = dy
    
    if game_settings.turn == "player_1":
        if abs(distance0) < 350 and abs(distance1) < 350 and abs(distance0) > 0 and abs(distance1) > 0 and attack_unit.image != missile_explosion and attack_unit.image != big_explosion:
            close = True
    else:
        if abs(distance0) < 200 and abs(distance1) < 200 and abs(distance0) > 0 and abs(distance1) > 0 and attack_unit.image != missile_explosion and attack_unit.image != big_explosion:
            close = True
    
    return close


def aa_move(defense_unit0, radar_units):
    # defense_unit0.original_x = defense_unit0.original_x + 450
    # defense_unit0.original_y = defense_unit0.original_y + 350
    # defense_unit0.current_x = defense_unit0.current_x + 450
    # defense_unit0.current_y = defense_unit0.current_y + 350 
    # if reset_aa == True:
    #     defense_unit0.current_x = defense_unit0.original_x
    #     defense_unit0.current_y = defense_unit0.original_y
    # print("AU.x:  " + str(attack_unit.current_x) + "  AU.y:  " + str(attack_unit.current_y))
    if attack_unit.current_x == 0:
        defense_unit0.current_x = defense_unit0.original_x
        defense_unit0.current_y = defense_unit0.original_y
    # print("DU.x:  " + str(defense_unit0.original_x) + "  DU.y:  " + str(defense_unit0.original_y))
    # print("DU.x:  " + str(defense_unit0.current_x) + "  DU.y:  " + str(defense_unit0.current_y))
    
    # s.image = pygame.image.load("sprite.png").convert() #load a sprite image
    # s.rect = b.image.get_rect() # set collision rectangle
    
    # pygame.draw.rect(defense_unit0.image, (0,0,0,[0]), (defense_unit0.image))
    # pygame.mask.from_surface(defense_unit0.image)
    # print("DU.rect:  " + str(defense_unit0.rect))
    
    attack_pos = [attack_unit.current_x, attack_unit.current_y]
    defense_original_pos = [defense_unit0.original_x, defense_unit0.original_y]
    defense_pos = [defense_unit0.current_x, defense_unit0.current_y]
    
    if check_if_missile_is_close(defense_original_pos, attack_pos):
        if defense_unit0.fired == False:
            pygame.mixer.Sound(launch_sound1).play()
            defense_unit0.fired = True
        
        defense_unit0.target_x = attack_unit.current_x
        defense_unit0.target_y = attack_unit.current_y
        if game_settings.turn == "player_1":
            defense_unit0.image = missile_white
        else:
            defense_unit0.image = pygame.transform.flip(missile_white, True, False)
    
    # if attack_unit.image == blank_image:
    #     defense_unit0.image = blank_image
    #     defense_unit0.current_x = defense_unit0.original_x
    #     defense_unit0.current_y = defense_unit0.original_x
    
    
    if check_if_missile_is_close(defense_original_pos, attack_pos):
        # defense_unit0.current_x -= defense_unit0.target_x / defense_unit0.current_x
        # defense_unit0.current_y -= defense_unit0.target_y / defense_unit0.current_y
        if attack_unit.image != missile_explosion and attack_unit.image != big_explosion and attack_unit.image != blank_image:
            if defense_unit0.target_x != 0:
                # defense_unit0.current_x += defense_unit0.current_x / defense_unit0.target_x
                # defense_unit0.current_y += defense_unit0.current_y / defense_unit0.target_y
                
                ## Old AA launch algorithm
                # defense_unit0.current_x -= 1
                # defense_unit0.current_y -= 1
                
                
                # defense_unit0.image = missile_white
                
                if game_settings.turn == "player_1":
                    ## New AA launch algorithm
                    defense_unit0.current_x -= (attack_unit.current_x / 200)
                    
                    #lock in the height for the aa missiles
                    if defense_unit0.current_y - attack_unit.current_y < 20:
                        defense_unit0.current_y -= (0.6 - (radar_units/10))
                        defense_unit0.current_x -= 1
                    else:
                        defense_unit0.current_y -= 2.42 - (attack_unit.current_y / 200)
                else:
                    ## New AA launch algorithm
                    defense_unit0.current_x += (attack_unit.current_x / 200)
                    
                    #lock in the height for the aa missiles
                    if defense_unit0.current_y - attack_unit.current_y < 2:
                        defense_unit0.current_y += (attack_unit.current_y / 200)
                    else:
                        defense_unit0.current_y += (0.55 - (radar_units/10))
                        defense_unit0.current_x += 1
                
                # print("CURRENT RADAR UNITS:  " + str(0.6-(radar_units/10)))
                # defense_unit0.current_x -= 16
                # defense_unit0.current_y -= 16
            
            # print("DU.x:  " + str(defense_unit0.current_x) + "  DU.y:  " + str(defense_unit0.current_y)+ "  DUTU.x:  " + str(defense_unit0.target_x) + "  DUTU.y:  " + str(defense_unit0.target_y))
            screen.blit(defense_unit0.image, (defense_unit0.current_x, defense_unit0.current_y))
            
            # if pygame.sprite.collide_mask(attack_unit, defense_unit0):
            if check_collision_aa(attack_pos, defense_pos):

                if attack_unit.building_type == "m_upgraded":
                    if game_settings.turn == "player_1":
                        attack_unit.image = missile_blue
                    else:
                        attack_unit.image = pygame.transform.flip(missile_blue, True, False)
                    
                    attack_unit.building_type = "m"
                    attack_unit.current_y = attack_unit.current_y - 20
                    
                    pygame.mixer.Sound(explosion_sound0).play()
                    defense_unit0.image = blank_image
                elif attack_unit.building_type == "m":
                    print("HAA!!! SHOT DOWN")
                    attack_unit.image = missile_explosion
                    attack_unit.when_hit_y = attack_unit.current_y
                    attack_unit.current_y = attack_unit.current_y
                    defense_unit0.target_x = 0
                    defense_unit0.target_y = 0
                    defense_unit0.current_x = defense_unit0.original_x
                    defense_unit0.current_y = defense_unit0.original_y
                    
                    if game_settings.turn == "player_1":
                        random_number = random.randint(0, 5)
                        if random_number > 2:
                            game_settings.turn = "player_2"
                    else:
                        game_settings.turn = "player_1"
                    
                    pygame.mixer.Sound(explosion_sound0).play()
                
                # defense_unit0.image = blank_image
                screen.blit(attack_unit.image, (attack_unit.current_x, attack_unit.current_y))
                screen.blit(defense_unit0.image, (defense_unit0.current_x, defense_unit0.current_y))
            
    return defense_unit0


def reset_aa(defense_unit0):
    defense_unit0.target_x = 0
    defense_unit0.target_y = 0
    defense_unit0.current_x = defense_unit0.original_x
    defense_unit0.current_y = defense_unit0.original_y
    
    
    return defense_unit0


def find_aas(map):
    defense_units = []
    
    for map_object in map:
        if map_object.building_type == "a":
            defense_units.append(defense_unit("a",blank_image,map_object.x+25,map_object.y+25,0,0,map_object.x+25,map_object.y+25)) 
        elif map_object.building_type == "a_upgraded":
            defense_units.append(defense_unit("a",blank_image,map_object.x+12.5,map_object.y+12.5,0,0,map_object.x+12.5,map_object.y+12.5)) 
            defense_units.append(defense_unit("a",blank_image,map_object.x+37.5,map_object.y+37.5,0,0,map_object.x+37.5,map_object.y+37.5))
    
    
    return defense_units


def find_radars(map):
    radar_units = 0
    
    for map_object in map:
        if map_object.building_type == "r":
            radar_units += 1 
    
    return radar_units


def find_missile(map, funds):
    found_upgraded_missile = False
    found_missile = False
    attack_unit.original_x = 0
    attack_unit.original_y = 0
    attack_unit.current_x = 0
    attack_unit.current_y = 0
    attack_unit.target_x = 0
    attack_unit.target_y = 0
    attack_unit.when_hit_y = 0
    attack_unit.damage = 0
    attack_unit.image = blank_image
    attack_unit.building_type = ""
    attack_unit.duration = 0
    
    for map_object in map:
        if map_object.building_type == "m_upgraded":
            attack_unit.original_x = map_object.x
            attack_unit.original_y = map_object.y
            attack_unit.current_x = map_object.x
            attack_unit.current_y = map_object.y
            attack_unit.target_x = 0
            attack_unit.target_y = 0
            attack_unit.when_hit_y = 0
            attack_unit.image = pygame.transform.flip(missile_blue_double, True, False)
            attack_unit.building_type = "m_upgraded"
            attack_unit.duration = 0
            attack_unit.damage = 40
            found_upgraded_missile = True
            found_missile = True
            break
    
    if found_upgraded_missile == False:
        for map_object in map:
            if map_object.building_type == "m":
                attack_unit.original_x = map_object.x
                attack_unit.original_y = map_object.y
                attack_unit.current_x = map_object.x
                attack_unit.current_y = map_object.y
                attack_unit.target_x = 0
                attack_unit.target_y = 0
                attack_unit.when_hit_y = 0
                attack_unit.image = pygame.transform.flip(missile_blue, True, False)
                attack_unit.building_type = "m"
                attack_unit.duration = 0
                attack_unit.damage = 20
                found_missile = True
                break
    
    if found_missile == False and funds > 28:
        built_missile = True
        for map_object in map:
            if map_object.building_type == "green":
                map_object.building_type = "m"
                map_object.image = green_m
                map_object.hit_points = building_hit_points_dict[map_object.building_type]
                attack_unit.original_x = map_object.x
                attack_unit.original_y = map_object.y
                attack_unit.current_x = map_object.x
                attack_unit.current_y = map_object.y
                attack_unit.target_x = 0
                attack_unit.target_y = 0
                attack_unit.when_hit_y = 0
                attack_unit.image = pygame.transform.flip(missile_blue, True, False)
                attack_unit.building_type = "m"
                attack_unit.duration = 0
                attack_unit.damage = 20
                built_missile = True
                funds -= building_cost["m"]
                break
            elif map_object.building_type == "brown":
                map_object.building_type = "m"
                map_object.image = green_m
                map_object.hit_points = building_hit_points_dict[map_object.building_type]
                attack_unit.original_x = map_object.x
                attack_unit.original_y = map_object.y
                attack_unit.current_x = map_object.x
                attack_unit.current_y = map_object.y
                attack_unit.target_x = 0
                attack_unit.target_y = 0
                attack_unit.when_hit_y = 0
                attack_unit.image = pygame.transform.flip(missile_blue, True, False)
                attack_unit.building_type = "m"
                attack_unit.duration = 0
                attack_unit.damage = 20
                built_missile = True
                funds -= building_cost["m"]
                break
        
        if built_missile == False:
            print("NO MISSILES WERE FOUND!!!")
            print("QUITTING NOW!!!")
            print("\n\n\nYOUR SCORE:  " + str(player_1.score))
            print("COMPUTER'S SCORE:  " + str(player_2.score))
            pygame.quit()


def attack():
    attack_unit.target_x = random.randrange(100, 301, 50)
    attack_unit.target_y = random.randrange(100, 301, 50)
    if attack_unit.building_type == "m":
        attack_unit.image = attack_unit.image = pygame.transform.flip(missile_blue, True, False)
    elif attack_unit.building_type == "m_upgraded":
        attack_unit.image = attack_unit.image = pygame.transform.flip(missile_blue_double, True, False)
    
    pygame.mixer.Sound(launch_sound0).play()


def check_if_won(map):
    number_of_no_buildings = 0
    won = False
    
    for map_object in map:
        if map_object.building_type == "green" or map_object.building_type == "blue" or map_object.building_type == "brown":
            number_of_no_buildings += 1
    
    if number_of_no_buildings == 36:
        won = True
    
    return won


def make_funds(map):
    funds = 0
    for map_object in map:
        if map_object.building_type != "green" and map_object.building_type != "blue" and map_object.building_type != "brown":
            funds += 1
        elif map_object.building_type == "brown":
            funds -= 1
    return funds


def computer_build(map, funds):
    if funds > 66:
        for map_object in map:
            if "_upgraded" not in map_object.building_type and map_object.building_type != "blue" and map_object.building_type != "brown" and map_object.building_type != "green" and map_object.building_type != "r":
                map_object.building_type = map_object.building_type + "_upgraded"
                map_object.hit_points = building_hit_points_dict[map_object.building_type]
                if map_object.building_type == "a_upgraded":
                    map_object.image = green_a_upgraded
                else:
                    map_object.image = green_m_upgraded
                
                funds -= building_cost["upgrade"]



found_defense_units = False
## Regular list, will be later replaced by a function:
# defense_units = [defense_unit("a",missile_white,550,450,0,0,550,450),defense_unit("a",missile_white,600,450,0,0,600,450),defense_unit("a",missile_white,650,450,0,0,650,450),defense_unit("a",missile_white,600,500,0,0,600,500),defense_unit("a",missile_white,600,600,0,0,600,600),defense_unit("a",missile_white,625,600,0,0,650,600)]

## List to test missile strike accuracy:
# defense_units = [defense_unit("a",missile_white,0,0,0,0,0,0)]

defense_unit0 = defense_unit("a",missile_white,550,450,0,0,550,450)
# reset_aa = False



player_1_map = [
    grid0_0_0, grid0_0_1, grid0_0_2, grid0_0_3, grid0_0_4, grid0_0_5,
    grid0_1_0, grid0_1_1, grid0_1_2, grid0_1_3, grid0_1_4, grid0_1_5,
    grid0_2_0, grid0_2_1, grid0_2_2, grid0_2_3, grid0_2_4, grid0_2_5,
    grid0_3_0, grid0_3_1, grid0_3_2, grid0_3_3, grid0_3_4, grid0_3_5,
    grid0_4_0, grid0_4_1, grid0_4_2, grid0_4_3, grid0_4_4, grid0_4_5,
    grid0_5_0, grid0_5_1, grid0_5_2, grid0_5_3, grid0_5_4, grid0_5_5
]

player_2_map = [
    grid1_0_0, grid1_0_1, grid1_0_2, grid1_0_3, grid1_0_4, grid1_0_5,
    grid1_1_0, grid1_1_1, grid1_1_2, grid1_1_3, grid1_1_4, grid1_1_5,
    grid1_2_0, grid1_2_1, grid1_2_2, grid1_2_3, grid1_2_4, grid1_2_5,
    grid1_3_0, grid1_3_1, grid1_3_2, grid1_3_3, grid1_3_4, grid1_3_5,
    grid1_4_0, grid1_4_1, grid1_4_2, grid1_4_3, grid1_4_4, grid1_4_5,
    grid1_5_0, grid1_5_1, grid1_5_2, grid1_5_3, grid1_5_4, grid1_5_5
]


defense_units = find_aas(player_2_map)
radar_units = find_radars(player_2_map)
clock_var = False
warning_image_timer = 0
funds_timer = 0
attack_timer = 0
funds_amount_player_1 = 150
funds_amount_player_2 = 50


async def main():
    while True:
        pygame.mixer.stop()
        
        flipped_time = time.time()


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            if event.type == pygame.KEYDOWN:
                # pygame.key.set_repeat(2000, 0)
                pygame.key.set_repeat()
                # print(pygame.key.building_type(event.key))
                if event.key == K_LEFT:
                    highlighter.x -= 50 # Move the square selector left
                if event.key == K_RIGHT:
                    highlighter.x += 50  # Move the square selector right
                if event.key == K_UP:
                    highlighter.y -= 50  # Move the square selector up
                if event.key == K_DOWN:
                    highlighter.y += 50 # Move the square selector down
                if  event.key == K_r:
                    highlighter.x = 50 # Move the square selector to its starting position
                    highlighter.y = 100 # Move the square selector to its starting position
                if  event.key == K_q:
                    player_1.score = player_1.score + (2 * funds_amount_player_1)
                    player_2.score = player_2.score + (2 * funds_amount_player_2)
                    print("\n\n\nYOUR SCORE:  " + str(player_1.score))
                    print("COMPUTER'S SCORE:  " + str(player_2.score))
                    pygame.quit()
                if event.key == K_PERIOD:
                    tool_image_list_index += 1
                if event.key == K_COMMA:
                    tool_image_list_index -= 1
                if event.key == K_z:
                    # print(grid_object.type)
                    funds_amount_player_1 = do_tool(player_1_map, tool_image_square_list[tool_image_list_index], warning_image, player_2_map, funds_amount_player_1)
                if event.key == K_ESCAPE:
                    warning_image_class.image = blank_image
                if event.key == K_RETURN:
                    game_settings.turn = "player_2"
                if event.key == K_1:
                    game_settings.turn = "player_1"
                if event.key == K_b:
                    game_settings.mode = "build"
                    attack_unit.current_x = 0
                    attack_unit.current_y = 0
                    highlighter.x = 100 # Move the square selector to its starting position
                    highlighter.y = 150 # Move the square selector to its starting position
                if event.key == K_p:
                    if game_settings.paused == False:
                        game_settings.paused = True
                    else:
                        game_settings.paused = False
        
        
        if game_settings.mode == "build":
            highlighter.image = build_highlighter
        elif game_settings.mode == "attack":
            highlighter.image = attack_highlighter
        
        if tool_image_list_index < 0:
            tool_image_list_index = 6
        elif tool_image_list_index > 6:
            tool_image_list_index = 0
        
        
        
        player_1_map_label = font.render("Player 1:", True, (255,255,255))
        player_2_map_label = font.render("Player 2:", True, (255,255,255))
        
        player_1_map = [
            grid0_0_0, grid0_0_1, grid0_0_2, grid0_0_3, grid0_0_4, grid0_0_5,
            grid0_1_0, grid0_1_1, grid0_1_2, grid0_1_3, grid0_1_4, grid0_1_5,
            grid0_2_0, grid0_2_1, grid0_2_2, grid0_2_3, grid0_2_4, grid0_2_5,
            grid0_3_0, grid0_3_1, grid0_3_2, grid0_3_3, grid0_3_4, grid0_3_5,
            grid0_4_0, grid0_4_1, grid0_4_2, grid0_4_3, grid0_4_4, grid0_4_5,
            grid0_5_0, grid0_5_1, grid0_5_2, grid0_5_3, grid0_5_4, grid0_5_5
        ]
        
        player_2_map = [
            grid1_0_0, grid1_0_1, grid1_0_2, grid1_0_3, grid1_0_4, grid1_0_5,
            grid1_1_0, grid1_1_1, grid1_1_2, grid1_1_3, grid1_1_4, grid1_1_5,
            grid1_2_0, grid1_2_1, grid1_2_2, grid1_2_3, grid1_2_4, grid1_2_5,
            grid1_3_0, grid1_3_1, grid1_3_2, grid1_3_3, grid1_3_4, grid1_3_5,
            grid1_4_0, grid1_4_1, grid1_4_2, grid1_4_3, grid1_4_4, grid1_4_5,
            grid1_5_0, grid1_5_1, grid1_5_2, grid1_5_3, grid1_5_4, grid1_5_5
        ]

        screen.fill((10, 20, 149))  # Make background grey


        
        screen.blit(player_1_map_label, (150, 1))
        screen.blit(player_2_map_label, (600, 350))
        
        
        #how do you iterate through a multidimensional list?
        for grid_object in player_1_map:
            screen.blit(grid_object.image, (grid_object.x, grid_object.y))
        
        
        for grid_object in player_2_map:
            screen.blit(grid_object.image, (grid_object.x, grid_object.y))


        # Draw the highlighter
        screen.blit(highlighter.image, (highlighter.x, highlighter.y))
        
        # Drawing the tool box
        screen.blit(build_image, (650, 50))
        
        # Drawing the currently selected tool    
        screen.blit(tool_image_list[tool_image_list_index], (690, 85))

        
        won = check_if_won(player_2_map)
        if won == True:
            player_1.score = player_1.score + (2 * funds_amount_player_1)
            player_2.score = player_2.score + (2 * funds_amount_player_2)
            print("\n\n\nYOUR SCORE:  " + str(player_1.score))
            print("COMPUTER'S SCORE:  " + str(player_2.score))
            print("YOU BEAT THE COMPUTER!!! CONGRATULATIONS!")
            grid1_1_0 = tile_player2(blue, "blue", 500, 450, 0)
            grid1_1_1 = make_map_square(550, 450)
            grid1_1_2 = make_map_square(600, 450)
            grid1_1_3 = make_map_square(650, 450)
            grid1_1_4 = make_map_square(700, 450)
            grid1_1_5 = tile_player2(blue, "blue", 750, 450, 0)

            grid1_2_0 = tile_player2(blue, "blue", 500, 500, 0)
            grid1_2_1 = make_map_square(550, 500)
            grid1_2_2 = make_map_square(600, 500)
            grid1_2_3 = make_map_square(650, 500)
            grid1_2_4 = make_map_square(700, 500)
            grid1_2_5 = tile_player2(blue, "blue", 750, 500, 0)

            grid1_3_0 = tile_player2(blue, "blue", 500, 550, 0)
            grid1_3_1 = make_map_square(550, 550)
            grid1_3_2 = make_map_square(600, 550)
            grid1_3_3 = make_map_square(650, 550)
            grid1_3_4 = make_map_square(700, 550)
            grid1_3_5 = tile_player2(blue, "blue", 750, 550, 0)

            grid1_4_0 = tile_player2(blue, "blue", 500, 600, 0)
            grid1_4_1 = make_map_square(550, 600)
            grid1_4_2 = make_map_square(600, 600)
            grid1_4_3 = make_map_square(650, 600)
            grid1_4_4 = make_map_square(700, 600)
            grid1_4_5 = tile_player2(blue, "blue", 750, 600, 0)
            
            funds_amount_player_1 = round(150 + (player_1.score / 2))
            player_1.score = 0
            player_2.score = 0
        
        
        lost = check_if_won(player_1_map)
        if lost == True:
            player_1.score = player_1.score + (2 * funds_amount_player_1)
            player_2.score = player_2.score + (2 * funds_amount_player_2)
            print("\n\n\nYOUR SCORE:  " + str(player_1.score))
            print("COMPUTER'S SCORE:  " + str(player_2.score))
            print("THE COMPUTER BEAT YOU!!! GAME OVER!")
            pygame.quit()
        
        
        
        if game_settings.paused == False:
            if game_settings.turn == "player_1":
                if game_settings.mode != "attack_launch":
                    defense_units = find_aas(player_2_map)
                    radar_units = find_radars(player_2_map)
                
                units_to_update = missile_move(player_2_map, defense_units, radar_units)
                
                defense_units = units_to_update[0]
                radar_units = units_to_update[1]
                
                for defense_unit0 in defense_units:
                    aa_move(defense_unit0, radar_units)
            elif game_settings.turn == "player_2":
                if game_settings.mode != "attack_launch":
                    defense_units = find_aas(player_1_map)
                    radar_units = find_radars(player_1_map)
                    find_missile(player_2_map, funds_amount_player_2)
                    attack()
                
                units_to_update = missile_move(player_1_map, defense_units, radar_units)
                
                defense_units = units_to_update[0]
                radar_units = units_to_update[1]
                
                for defense_unit0 in defense_units:
                    aa_move(defense_unit0, radar_units)

        
        if warning_image_class.image != blank_image:
            counter_tick = pygame.time.Clock()
            counter_tick.tick(30)
            xxye = counter_tick.get_time()
            warning_image_timer += 1
            print (warning_image_timer)
            if warning_image_timer == 100:
                warning_image_class.image = blank_image
                warning_image_timer = 0
        
        
        if game_settings.mode != "attack" and game_settings.mode != "attacking" and game_settings.mode != "attack_launch" and game_settings.paused == False:
            funds_timer += 1
            if funds_timer == 1620:
                funds_amount_player_1 += make_funds(player_1_map)
                funds_amount_player_2 += make_funds(player_2_map)
                funds_timer = 0
        
        
        screen.blit(funds_box, (50, 580))
        if funds_amount_player_1 < 0:
            funds = funds_font.render("$"+str(funds_amount_player_1), True, (255,0,0))
        else:
            funds = funds_font.render("$"+str(funds_amount_player_1), True, (0,0,0))
        
        screen.blit(funds, (70, 607))
        
        screen.blit(warning_image_class.image, (50, 550))
        
        paused_text = paused_font.render("PAUSED", True, (255,255,255))
        
        if game_settings.turn != "player_2" and game_settings.mode != "attack" and game_settings.mode != "attacking" and game_settings.mode != "attack_launch" and game_settings.paused == False:
            attack_timer += 1
            if attack_timer == 5400:
                game_settings.turn = "player_2"
                attack_timer = 0
        
        
        if game_settings.paused == True:
            screen.blit(paused_text, (270, 280))
            
        
        computer_build(player_2_map, funds_amount_player_2)

        pygame.display.flip()  # Update the full display
        
        await asyncio.sleep(0) # do not forget that one, it must be called on every frame


if __name__ == "__main__":
    asyncio.run(main())