#Simple Mario style game
#By Chad Brubaker
#For the GREAT High School camp
#The scrolling is handled by storing a rectangle that is the part of the world we 'view'(called screen_loc)
#We move the screen_loc to keep 'player' in the center of the screen(if possible)
#and then offseting all of our drawing so that something at screen_loc.left + 30 would be at (30,y) once drawn
import pygame,sys,display
import level
from inventory import Inventory
from knight import Knight
#init pygame
pygame.init()
#set up dimensions and saved colors
size = width,height = 1024,600
black = 0,0,0
white = 255,255,255
#setup a clock for FPS stuffs
clock = pygame.time.Clock()
#set up the screen
screen = pygame.display.set_mode(size)
screen_loc = screen.get_rect()
#save game stuff
game_level = level.level_from_file("level")
inventory = Inventory()
player = Knight(pygame.Rect(game_level.startpos,(56,76)), inventory)
#font(not used)
font = pygame.font.Font(None,32)
speed = 5
keepGoing = True
while keepGoing:
    elapsed = clock.tick(20)
    for event in pygame.event.get():
        #handle closing the game
        if event.type == pygame.QUIT:
            keepGoing = False
        #handle movement using key state
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.speed[0] = -speed
            if event.key == pygame.K_RIGHT:
                player.speed[0] = speed
            if event.key == pygame.K_UP:
                player.speed[1] = -speed
            if event.key == pygame.K_DOWN:
                player.speed[1] = speed
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                player.speed[0] = 0
            if event.key == pygame.K_DOWN or event.key == pygame.K_UP:
                player.speed[1] = 0
    #update the player
    player.update(game_level,elapsed)
    for a in game_level.animates:
        a.update(game_level,player)
    #check if mario has reached the level's end(for now we just quit)
    if player.location.colliderect(game_level.endrect):
        keepGoing = False
    #figure out the screen offset
    screen_loc.center = player.location.center
    #keep the screen from moving off the 'level'
    #depending on the game you might want to handle going off the top/bottom too
    if screen_loc.left < 0:
        screen_loc.left = 0
    if screen_loc.right > game_level.width:
        screen_loc.right = game_level.width
    if screen_loc.bottom > game_level.height:
        screen_loc.bottom = game_level.height
    #keep the player from moving off to the left/right
    if display.abs_to_screen(player.location,screen_loc).left < 0:
        player.location.left = 0
    if display.abs_to_screen(player.location,screen_loc).right > width:
        player.location.right = screen_loc.right
    #draw everything to the screen
    #clear the screen
    screen.fill(white)
    #draw the level
    game_level.draw(screen,screen_loc)
    player.draw(screen,screen_loc)
    inventory.draw(screen,screen_loc)
    #tell the screen to update
    pygame.display.flip()
    #use the clock to limit the game to 40 frames/sec

#stop the music before we quit
pygame.mixer.music.stop()
