#Basic platform style controls
import pygame, sys, time
from pygame.locals import *

pygame.init()

winWidth = 400
winHeight = 400
winCenterx = int(winWidth / 2)
window = pygame.display.set_mode((winWidth, winHeight), 0, 32)

bkGroundImage = pygame.image.load('Background.jpg')
bkGroundImage = pygame.transform.scale(bkGroundImage, (1000, 1000))
bkGroundRect = pygame.Rect(0,  0, 1000, 1000)

levelImage = pygame.image.load('Floor.jpg')
levelImage = pygame.transform.scale(levelImage, (1000, 50))
levelRect = pygame.Rect(0, 250, 1000, 50)

forward = False
backward = False
jump = False
UP = 8
DOWN = 2

charImage = pygame.image.load('Ball.jpg')
charImage = pygame.transform.scale(charImage, (50, 50))
charRect = {'rect':pygame.Rect(10, 300, 50, 50), 'dir':UP}

#-- Don't make spA > 5 or controls won't work --
spA = 5
spB = 7

while True:

    window.blit(bkGroundImage, bkGroundRect)

    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                forward = True
                backward = False
            if event.key == K_LEFT:
                backward = True
                forward = False
            if event.key == K_UP:
                jump = True

        if event.type == KEYUP:
            if event.key == K_RIGHT:
                forward = False
            if event.key == K_LEFT:
                backward = False

    window.blit(levelImage, levelRect)
    levelRect.bottom = winHeight

    if charRect['rect'].centerx == winCenterx:
        if forward and levelRect.right > winWidth:
            levelRect.left -= spA
        if backward and levelRect.left < 0:
            levelRect.left += spA
            
    if levelRect.left == 0:
        if forward and charRect['rect'].centerx < winCenterx:
            charRect['rect'].centerx += spA
        if backward and charRect['rect'].left > 0:
            charRect['rect'].left -= spA
    if levelRect.right == winWidth:
        if forward and charRect['rect'].right < winWidth:
            charRect['rect'].right += spA
        if backward and charRect['rect'].centerx > winCenterx:
            charRect['rect'].centerx -= spA

    window.blit(charImage, charRect['rect'])

    if jump == True:
        if charRect['dir'] == UP:
            charRect['rect'].top -= spB
        if charRect['dir'] == DOWN:
            charRect['rect'].bottom += spB

        if charRect['rect'].top < 100:
            if charRect['dir'] == UP:
                charRect['dir'] = DOWN
        if charRect['rect'].bottom >= levelRect.top:
            if charRect['dir'] == DOWN:
                charRect['dir'] = UP

        if charRect['rect'].bottom >= levelRect.top:
            jump = False


    pygame.display.update()
