import pygame, math
from game import map, ui

window = pygame.display.set_mode([800, 600])
ui.window = window
screen = "game"
s = {"fullscreen": False}
running = True
gamedata = {"level": 0, "coal": 0, "iron": 1, "copper":0}
tiles = pygame.sprite.Group()
rails = pygame.sprite.Group()
carts = pygame.sprite.Group()
interactables = pygame.sprite.Group()
listmap = []
clock = pygame.time.Clock()
selected = pygame.image.load("./resources/images/selected.png")
selected2 = pygame.image.load("./resources/images/selected2.png")
box = pygame.image.load("./resources/images/box.png")
uibox = pygame.image.load("./resources/images/ui box.png")

class Mouse(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.surface.Surface([1, 1])
        self.rect = self.image.get_rect()
        self.rect.topleft = [0, 0]
        self.clickedcart = None
        self.hoveritem = None
        self.tl = self.rect.topleft
        self.mode = "select"

    def pos(self, position):
        self.rect.topleft = position
        self.tl = self.rect.topleft


m = Mouse()


def snaptogrid(pos):
    return [int(math.floor(pos[0] / 40)), int(math.floor(pos[1] / 40))]


def loadlevel(number):
    global tiles, rails, carts, gamedata, listmap, interactables
    tiles, rails, interactables, listmap = map.loadmap(int(number))
    carts.empty()
    gamedata["level"] = number
    gamedata["coal"] = 0
    gamedata["iron"] = 1
    gamedata["copper"] = 0

loadlevel(0)

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEBUTTONDOWN:
            m.pos(pygame.mouse.get_pos())
            if screen == "game":
                if pygame.sprite.spritecollide(m, carts, False) and m.mode == "select":
                    carts.update("select", m, listmap)
                    if m.clickedcart != None:
                        m.mode = "action"
                elif m.mode == "action" and m.clickedcart != None and listmap[snaptogrid(m.tl)[0]][snaptogrid(m.tl)[1]] > 0:
                    m.clickedcart.pathfind(listmap, snaptogrid(m.tl))
                    m.clickedcart = None
                    m.mode = "select"
        elif event.type == pygame.MOUSEMOTION:
            m.pos(pygame.mouse.get_pos())
            if screen == "game":
                m.hoveritem = None
                if len(pygame.sprite.spritecollide(m, carts, False)) > 0:
                    m.hoveritem = pygame.sprite.spritecollide(m, carts, False)[0]
                elif len(pygame.sprite.spritecollide(m, interactables, False)) > 0:
                    m.hoveritem = pygame.sprite.spritecollide(m, interactables, False)[0]
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                carts.add(map.Cart(snaptogrid(m.tl), "miner"))
    if screen == "game":
        window.fill([100, 100, 100])
        tiles.draw(window)
        carts.draw(window)
        carts.update("update", m, listmap)
        if not m.hoveritem == None and not m.mode == "action":
            window.blit(box, [m.rect.left+10, m.rect.top+10])
            ui.Resize(30)
            ui.Text(m.hoveritem.type.upper(), [m.rect.left+27, m.rect.top+25])
            if m.hoveritem.type.startswith("mine") and m.hoveritem not in carts:
                ui.Resize(18)
                ui.Text("Carts Inside: " + str(m.hoveritem.data["carts"]), [m.rect.left+27, m.rect.top+47])
                ui.Text("Max Carts: " + str(m.hoveritem.data["max"]), [m.rect.left+27, m.rect.top+60])
        if not m.clickedcart == None:
            window.blit(selected2, [m.clickedcart.rect.left-2, m.clickedcart.rect.top-2])
            if m.mode == "action":
                window.blit(box, [m.rect.left+10, m.rect.top+10])
                ui.Resize(30)
                try:
                    ui.Text(m.hoveritem.type.upper(), [m.rect.left+27, m.rect.top+25])
                except:
                    ui.Text(m.clickedcart.type.upper(), [m.rect.left+27, m.rect.top+25])
                if listmap[snaptogrid(m.tl)[0]][snaptogrid(m.tl)[1]] > 0:
                    ui.Resize(22)
                    ui.Text("Click to move", [m.rect.left+27, m.rect.top+45])
                    ui.Text("Cart Here", [m.rect.left+27, m.rect.top+60])
        window.blit(selected, [snaptogrid(m.tl)[0]*40-2, snaptogrid(m.tl)[1]*40-2])
        window.blit(uibox, [555, 475])
    pygame.display.flip()
    clock.tick(60)
    fps = clock.get_fps()
pygame.quit()

[((4, 9, 4, 44), 'pygame.display.set_mode', 'pygame.display.set_mode', ({(4, 33, 4, 43): '[800, 600]'}, {}), '([800, 600])', False, 'import pygame, math\n'), ((10, 8, 10, 29), 'pygame.sprite.Group', 'pygame.sprite.Group', ({}, {}), '()', False, 'import pygame, math\n'), ((11, 8, 11, 29), 'pygame.sprite.Group', 'pygame.sprite.Group', ({}, {}), '()', False, 'import pygame, math\n'), ((12, 8, 12, 29), 'pygame.sprite.Group', 'pygame.sprite.Group', ({}, {}), '()', False, 'import pygame, math\n'), ((13, 16, 13, 37), 'pygame.sprite.Group', 'pygame.sprite.Group', ({}, {}), '()', False, 'import pygame, math\n'), ((15, 8, 15, 27), 'pygame.time.Clock', 'pygame.time.Clock', ({}, {}), '()', False, 'import pygame, math\n'), ((16, 11, 16, 63), 'pygame.image.load', 'pygame.image.load', ({(16, 29, 16, 62): '"""./resources/images/selected.png"""'}, {}), "('./resources/images/selected.png')", False, 'import pygame, math\n'), ((17, 12, 17, 65), 'pygame.image.load', 'pygame.image.load', ({(17, 30, 17, 64): '"""./resources/images/selected2.png"""'}, {}), "('./resources/images/selected2.png')", False, 'import pygame, math\n'), ((18, 6, 18, 53), 'pygame.image.load', 'pygame.image.load', ({(18, 24, 18, 52): '"""./resources/images/box.png"""'}, {}), "('./resources/images/box.png')", False, 'import pygame, math\n'), ((19, 8, 19, 58), 'pygame.image.load', 'pygame.image.load', ({(19, 26, 19, 57): '"""./resources/images/ui box.png"""'}, {}), "('./resources/images/ui box.png')", False, 'import pygame, math\n'), ((112, 0, 112, 13), 'pygame.quit', 'pygame.quit', ({}, {}), '()', False, 'import pygame, math\n'), ((56, 17, 56, 35), 'pygame.event.get', 'pygame.event.get', ({}, {}), '()', False, 'import pygame, math\n'), ((109, 4, 109, 25), 'pygame.display.flip', 'pygame.display.flip', ({}, {}), '()', False, 'import pygame, math\n'), ((23, 8, 23, 43), 'pygame.sprite.Sprite.__init__', 'pygame.sprite.Sprite.__init__', ({(23, 38, 23, 42): 'self'}, {}), '(self)', False, 'import pygame, math\n'), ((24, 21, 24, 51), 'pygame.surface.Surface', 'pygame.surface.Surface', ({(24, 44, 24, 50): '[1, 1]'}, {}), '([1, 1])', False, 'import pygame, math\n'), ((41, 16, 41, 39), 'math.floor', 'math.floor', ({(41, 27, 41, 38): '(pos[0] / 40)'}, {}), '(pos[0] / 40)', False, 'import pygame, math\n'), ((41, 46, 41, 69), 'math.floor', 'math.floor', ({(41, 57, 41, 68): '(pos[1] / 40)'}, {}), '(pos[1] / 40)', False, 'import pygame, math\n'), ((88, 12, 88, 25), 'game.ui.Resize', 'ui.Resize', ({(88, 22, 88, 24): '(30)'}, {}), '(30)', False, 'from game import map, ui\n'), ((91, 16, 91, 29), 'game.ui.Resize', 'ui.Resize', ({(91, 26, 91, 28): '(18)'}, {}), '(18)', False, 'from game import map, ui\n'), ((98, 16, 98, 29), 'game.ui.Resize', 'ui.Resize', ({(98, 26, 98, 28): '(30)'}, {}), '(30)', False, 'from game import map, ui\n'), ((60, 18, 60, 40), 'pygame.mouse.get_pos', 'pygame.mouse.get_pos', ({}, {}), '()', False, 'import pygame, math\n'), ((104, 20, 104, 33), 'game.ui.Resize', 'ui.Resize', ({(104, 30, 104, 32): '(22)'}, {}), '(22)', False, 'from game import map, ui\n'), ((105, 20, 105, 77), 'game.ui.Text', 'ui.Text', ({(105, 28, 105, 43): '"""Click to move"""', (105, 45, 105, 76): '[m.rect.left + 27, m.rect.top + 45]'}, {}), "('Click to move', [m.rect.left + 27, m.rect.top + 45])", False, 'from game import map, ui\n'), ((106, 20, 106, 73), 'game.ui.Text', 'ui.Text', ({(106, 28, 106, 39): '"""Cart Here"""', (106, 41, 106, 72): '[m.rect.left + 27, m.rect.top + 60]'}, {}), "('Cart Here', [m.rect.left + 27, m.rect.top + 60])", False, 'from game import map, ui\n'), ((62, 19, 62, 63), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', ({(62, 47, 62, 48): 'm', (62, 50, 62, 55): 'carts', (62, 57, 62, 62): '(False)'}, {}), '(m, carts, False)', False, 'import pygame, math\n'), ((71, 18, 71, 40), 'pygame.mouse.get_pos', 'pygame.mouse.get_pos', ({}, {}), '()', False, 'import pygame, math\n'), ((74, 23, 74, 67), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', ({(74, 51, 74, 52): 'm', (74, 54, 74, 59): 'carts', (74, 61, 74, 66): '(False)'}, {}), '(m, carts, False)', False, 'import pygame, math\n'), ((75, 34, 75, 78), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', ({(75, 62, 75, 63): 'm', (75, 65, 75, 70): 'carts', (75, 72, 75, 77): '(False)'}, {}), '(m, carts, False)', False, 'import pygame, math\n'), ((76, 25, 76, 77), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', ({(76, 53, 76, 54): 'm', (76, 56, 76, 69): 'interactables', (76, 71, 76, 76): '(False)'}, {}), '(m, interactables, False)', False, 'import pygame, math\n'), ((77, 34, 77, 86), 'pygame.sprite.spritecollide', 'pygame.sprite.spritecollide', ({(77, 62, 77, 63): 'm', (77, 65, 77, 78): 'interactables', (77, 80, 77, 85): '(False)'}, {}), '(m, interactables, False)', False, 'import pygame, math\n')]