# Juego prueba -Jueves 4 / 10 / 2012- escuela 200, MakeUy
# NEXO 2012, Nicolas Novalic
import pygame
import random
import pygame

SPRITE_SIZE = 62
SPRITE_STEP = 20
SCREEN_SIZE_X = 1240
SCREEN_SIZE_Y = 744
INITIAL_POSITION_PLAYER_X = SCREEN_SIZE_X / 2 - SPRITE_SIZE
INITIAL_POSITION_PLAYER_Y = SCREEN_SIZE_Y / 2

#GRAPHICS
WATER_IMG = pygame.image.load("media/water.png")
COAST_IMG = pygame.image.load("media/coast.png")
SAND_IMG = pygame.image.load("media/floor.png")


class Recs(object):
    def __init__(self):
        self.collision_list=[]
        self.collision_list.append(pygame.Rect(0, 0, 20, SCREEN_SIZE_Y)) # CANT GO OFF SCREEN ON THE LEFT
        self.collision_list.append(pygame.Rect(SCREEN_SIZE_X - 20, 0, 20, SCREEN_SIZE_Y)) # DITTO RIGHT
        self.collision_list.append(pygame.Rect(0, SCREEN_SIZE_Y - 20, SCREEN_SIZE_X, 20)) # DITTO DOWN
        self.collision_list.append(pygame.Rect(0, 0, SCREEN_SIZE_X, 62)) # DITTO UP

class Player(pygame.sprite.Sprite):
    def __init__(self, player_img):
        self.player_img = player_img
        self.rect = self.player_img.get_rect()
        self.rect.top,self.rect.left = (INITIAL_POSITION_PLAYER_Y, INITIAL_POSITION_PLAYER_X)

    def change_position(self,vx,vy):
        self.rect.move_ip(vx,vy)

    def update(self,layer):
        layer.blit(self.player_img, self.rect)

    def rotate_position(self, angle):
        self.player_img = pygame.transform.rotate(self.player_img, angle)

def collision_detector(player,recs):
    for rec in recs.collision_list:
        if player.rect.colliderect(rec):
            return True
    return False



def main():

    #SCREEN SETUP
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y))
    pygame.display.set_caption("8 bit Game Demo")
    game_clock = pygame.time.Clock()
    PLAYER_IMG = pygame.image.load("media/car.png").convert_alpha()
    #MUSIC
    pygame.mixer.music.load("media/music.mp3")
    pygame.mixer.music.set_volume(0.5)
    pygame.mixer.music.play(-1)
    #GAME SET UP
    quit_game = False
    vx,vy = 0,0
    screen_recs = Recs()
    player1 = Player(PLAYER_IMG)
    collision = False
    direction = "UP"

    #MAIN LOOP
    while quit_game != True:


        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                quit_game = True

            #MOVEMENT
            if collision == False:
                if event.type == pygame.KEYDOWN:

                    if event.key == pygame.K_LEFT:
                        if direction == "UP":
                            player1.rotate_position(90)
                        elif direction == "RIGHT":
                            player1.rotate_position(180)
                        elif direction == "DOWN":
                            player1.rotate_position(270)
                        direction = "LEFT"

                        player1.change_position(-1 * SPRITE_STEP, 0)
                        if collision_detector(player1,screen_recs):
                            player1.change_position(SPRITE_STEP, 0)

                    if event.key == pygame.K_RIGHT:
                        if direction == "DOWN":
                            player1.rotate_position(90)
                        elif direction == "LEFT":
                            player1.rotate_position(180)
                        elif direction == "UP":
                            player1.rotate_position(270)
                        direction = "RIGHT"

                        player1.change_position(SPRITE_STEP, 0)
                        if collision_detector(player1,screen_recs):
                            player1.change_position(-1 * SPRITE_STEP, 0)

                    if event.key == pygame.K_UP:
                        if direction == "RIGHT":
                            player1.rotate_position(90)
                        elif direction == "DOWN":
                            player1.rotate_position(180)
                        elif direction == "LEFT":
                            player1.rotate_position(270)
                        direction = "UP"

                        player1.change_position(0,-1 * SPRITE_STEP)
                        if collision_detector(player1, screen_recs):
                            player1.change_position(0, SPRITE_STEP)

                    if event.key == pygame.K_DOWN:
                        if direction == "LEFT":
                            player1.rotate_position(90)
                        elif direction == "UP":
                            player1.rotate_position(180)
                        elif direction == "RIGHT":
                            player1.rotate_position(270)
                        direction = "DOWN"

                        player1.change_position(0, SPRITE_STEP)
                        if collision_detector(player1, screen_recs):
                            player1.change_position(0,-1 * SPRITE_STEP)


                if event.type == pygame.KEYUP:
                    if event.key == pygame.K_3:
                        vx = 0
                    if event.key == pygame.K_2:
                        vx = 0
                    if event.key== pygame.K_SPACE:
                        vy = 0
                    if event.key == pygame.K_4:
                        vy = 0

        game_clock.tick(20)
        if collision == False:
            player1.change_position(vx, vy)


        # PRINT GRAPHICS
        range_x = SCREEN_SIZE_X / SPRITE_SIZE
        range_y = SCREEN_SIZE_Y / SPRITE_SIZE
        for printing_x in range(0, range_x):
            for printing_y in range(1, range_y):
                screen.blit(SAND_IMG, (SPRITE_SIZE * printing_x, SPRITE_SIZE * printing_y))

        for printing in range(0, range_x):
            screen.blit(WATER_IMG, (SPRITE_SIZE * printing,0))
            screen.blit(COAST_IMG, (SPRITE_SIZE * printing, SPRITE_SIZE))

        player1.update(screen)
        pygame.display.update()

    pygame.quit()

main()
