import gui
import settings
import pygame
import variables
import utils
from object_list import ObjectsList
from objects import Player, Portal
from environment import Stars


class FPS:
    def __init__(self):
        self.clock = pygame.time.Clock()

    def render(self):
        gui.text('FPS: ' + str(round(self.clock.get_fps(), 2)), settings.SCREEN_WIDTH - 100, 10, 12)


def start():
    utils.delay_list = {}
    variables.objects = ObjectsList()
    variables.global_tick = 0
    variables.objects.add(Player(variables.player_parameters))
    variables.portal = Portal()
    variables.player_score = 0
    variables.game_start = True


def main():
    variables.stars = Stars(settings.STARS_COUNT)
    button_start = gui.Button(
        settings.SCREEN_WIDTH / 2 - 200,
        settings.SCREEN_HEIGHT / 2 - 50,
        400,
        100,
        'Начать',
        start
    )

    fps = FPS()
    running = True
    while running:
        fps.clock.tick(settings.TICK)
        variables.current_fps = fps.clock.get_fps()
        variables.screen.fill((0, 0, 0))

        if variables.game_start:
            variables.global_tick += 1
            variables.stars.update()
            variables.portal.add()
            #variables.objects.update()
            variables.objects.render()

            fps.render()
            gui.text(variables.objects.player.text['hull'], 10, 10, 19)
            gui.text(variables.objects.player.text['energy'], 10, 40, 16)
            gui.text(variables.objects.player.text['player_cannon'], 200, 10, 12)
            gui.text(variables.objects.player.text['player_launcher'], 200, 30, 12)
            gui.text(variables.objects.player.text['player_score'], 10, 70, 16)
        else:
            for button in variables.buttons:
                button.process()

        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()


if __name__ == "__main__":
    main()
