##Credit to scriptedfun (http://www.scriptedfun.com) for the help with the typing portion
##Still a very VERY barebones typing script. Next I have to work on
##the basic shoot-em-up part and get the scoring in place. Progress
##is being made!

import pygame
from pygame.locals import *
import helpers

SCREENRECT = Rect(0, 0, 640, 480)

##User Events. Stuff like game over and eventually
##bosses dying, getting supernatural borders and
##using spellcards ("bombs") if I so wish to do these.
SUBTRACT_TIMER = pygame.USEREVENT + 1
GAME_OVER_L = pygame.USEREVENT + 2
GAME_OVER_W = pygame.USEREVENT + 3

class Textsprite(pygame.sprite.Sprite):
    poscmb = 0
    stringcmb = 0
    pos = 0
    current_string = 0
    def __init__(self, text):
        pygame.sprite.Sprite.__init__(self)
        self.text = text
        self.update()
    def update(self):
        self.image = pygame.font.Font(None, 36).render(self.text[Textsprite.current_string], 1, (0, 0, 0))
        self.highlight = pygame.font.Font(None, 36).render(self.text[Textsprite.current_string][:Textsprite.pos], 1, (0, 0, 255))
        self.image.blit(self.highlight, (0, 0))
        self.rect = self.image.get_rect()
        self.rect.midtop = pygame.display.get_surface().get_rect().midtop
    def keyin(self, key):
        if key == self.text[Textsprite.current_string][Textsprite.pos]:
            Textsprite.pos = Textsprite.pos + 1
            Textsprite.poscmb = Textsprite.poscmb + 1
            if Textsprite.poscmb <= 10:
                Scoresprite.score = Scoresprite.score + 2**Textsprite.poscmb
            else:
                Scoresprite.score = Scoresprite.score + 2**10
        elif key == K_LSHIFT or key == K_RSHIFT:
            Textsprite.poscmb = Textsprite.poscmb
            Textsprite.stringcmb = Textsprite.stringcmb
            Scoresprite.perfect = Scoresprite.perfect
        else:
            Textsprite.poscmb = 0
            Textsprite.stringcmb = 0
            Scoresprite.perfect = 0
        if len(self.text[Textsprite.current_string]) == Textsprite.pos:
            Textsprite.pos = 0
            if Textsprite.current_string +1 == len(self.text):
                pygame.event.Event(GAME_OVER_W)
            else:
                Textsprite.current_string = Textsprite.current_string + 1
                Textsprite.stringcmb = Textsprite.stringcmb + 1
                if Textsprite.stringcmb <= 12:
                    Scoresprite.score = Scoresprite.score + 2500*Textsprite.stringcmb
                else:
                    Scoresprite.score = Scoresprite.score + 2500*12

class Poscombosprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.update()
    def update(self):
        self.image = pygame.font.Font(None, 18).render('Letters correctly typed in a row:' + str(Textsprite.poscmb), 1, (0, 255, 0))
        self.image.blit(self.image, (50, 150)) 
        self.rect = self.image.get_rect()
        self.rect.center = pygame.display.get_surface().get_rect().center

class Stringcombosprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.update()
    def update(self):
        self.image = pygame.font.Font(None, 18).render('Words correctly typed in a row: ' + str(Textsprite.stringcmb), 1, (255, 0, 0))
        self.image.blit(self.image, (50,50))
        self.rect = self.image.get_rect()
        self.rect.center = pygame.display.get_surface().get_rect().center
        
class Scoresprite(pygame.sprite.Sprite):
    score = 0
    perfect = 1
    mistakes = 0
    timeleft = 5
    maxcombo = 0
    
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.update()
    def update(self):
        self.image = pygame.font.Font(None, 18).render('Score: ' + str(Scoresprite.score), 1, (45, 45, 245))
        self.image.blit(self.image, (50,200))
        self.rect = self.image.get_rect()
        self.rect.center = pygame.display.get_surface().get_rect().center
    def scoretimers(self):
        pygame.time.set_timer(SUBTRACT_TIMER, 1000)

    
class Timersprite(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.update()
    def update(self):
        self.image = pygame.font.Font(None, 18).render('Time Remaining: ' + str(Scoresprite.timeleft), 1, (0, 0, 0))
        self.image.blit(self.image, (50,250))
        self.rect = self.image.get_rect()
        self.rect.center = pygame.display.get_surface().get_rect().center

def main():
    pygame.init()

    screen = pygame.display.set_mode(SCREENRECT.size)

    # make background
    background = pygame.Surface(SCREENRECT.size).convert()
    background.fill((255, 255, 255))
    screen.blit(background, (0, 0))
    pygame.display.update()

    # keep track of sprites
    all = pygame.sprite.RenderUpdates()

    # keep track of time
    clock = pygame.time.Clock()

    textsprite = Textsprite(["test string1", "another test!", "can you type this?", "Hey Mr Ottman!", "It works!"])
    poscombosprite = Poscombosprite()
    stringcombosprite = Stringcombosprite()
    scoresprite = Scoresprite()
    timersprite = Timersprite()
    all.add(textsprite, poscombosprite, stringcombosprite, scoresprite, timersprite)
    
    #Set moving to false
    moving = 0
    
    # game loop
    while 1:

        # get input
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYUP:
                if event.key == K_TAB:
                    moving = 0
            elif event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    return
                elif event.key == K_TAB:
                    moving = 1
                elif moving == 0:
                    textsprite.keyin(event.unicode)
            if event.type == SUBTRACT_TIMER:
                if Scoresprite.timeleft == 0:
                    pygame.event.Event(GAME_OVER_L)
                else:
                    Scoresprite.timeleft = Scoresprite.timeleft - 1
            if event.type == GAME_OVER_L:
                print 'You loose. Your total score is ' + Scoresprite.score + '.'
                Textsprite.pos = 0
                Textsprite.current_string = 0
                Scoresprite.score = 0
            if event.type == GAME_OVER_W:
                print 'You win! Your total score is ' + Scoresprite.score + '!'
                Textsprite.pos = 0
                Textsprite.current_string = 0
                Scoresprite.score = 0

        # clear sprites
        all.clear(screen, background)

        # update sprites
        all.update()

        # redraw sprites
        dirty = all.draw(screen)
        pygame.display.update(dirty)

        # maintain frame rate
        clock.tick(30)

if __name__ == '__main__': main()
