# -*- coding: utf-8 -*-
import pygame
import os
import random

from pygame.locals import *

class Ball(object):
    
    def __init__(self, surface, color, width, height):
        self.surface = surface
        self.color = color
        self.width = width
        self.height = height
        self.speedx = 3
        self.speedy = 3
        self.x = 1
        self.y = 600
        self.crash = False
        
    def update(self):
        self.x += self.speedx
        self.y += self.speedy
        self.check()
        
    def check(self):
        if self.y > self.height or self.y < 0:
            self.speedy *= -1
        if self.x > self.width or self.x < 0:
            self.crash = True
            
    def reverse(self):
        self.speedx *= -1
        
    def draw(self):
        pygame.draw.circle(self.surface, self.color,(self.x, self.y), 20, 0)
    
    
class Bars(object):

    def __init__(self, surface, color, width, height):
        self.surface = surface
        self.color = color
        self.width = width
        self.height = height
        self.y = 10
        self.speed = 0
        
    def update(self):
        #if self.y>=0 and self.y<=self.height:
        if (self.y>0 and self.speed<0) or (self.y+100<self.height and self.speed>0):
            self.y += self.speed
        
    def events(self, key):
        if key == K_UP:
            self.speed = -5
        elif key == K_DOWN:
            self.speed = 5
            
    def collide(self, item):
        if item.x < 30 and (self.y<=item.y and self.y+100>=item.y):
                return True
        elif item.x > self.width-30 and (self.y<=item.y and self.y+100>=item.y):
            return True
        else:
            return False
        
    def draw(self):
        pygame.draw.rect(self.surface, self.color,(self.width-20, self.y, 10, 100), 0)
        pygame.draw.rect(self.surface, self.color,(0+10, self.y, 10, 100), 0)


class Score(object):

    font    = None
    last_score = -1
    fgcolor = None
    bgcolor = None
    image   = None
    
    def __init__(self, score, pos=None, font=None, ptsize=30, fgcolor="0xffff00", bgcolor=None):
        self.score  = score
        self.fgcolor = pygame.color.Color(fgcolor)
        if bgcolor:
            self.bgcolor = pygame.color.Color(bgcolor)
        self.pos = pos or [ 0, 0 ]
        self.font = pygame.font.Font(font, ptsize)

        self.last_rect = None

    def draw(self, screen):
        text = "Score: % 4d" % self.score
        if self.bgcolor:
            self.image = self.font.render(text, False, self.fgcolor, self.bgcolor)
        else:
            self.image = self.font.render(text, False, self.fgcolor, (255, 0, 255))
            self.image.set_colorkey(( 255, 0, 255), RLEACCEL)

        self.last_rect = Rect(self.pos, self.image.get_size())

        screen.blit(self.image, self.pos)

    def clear(self, screen, background):
        if self.last_rect:
            screen.blit(background, self.last_rect)


class Game(object):
    screen = None
    run = True
    background  = None
    score = 0
    
    def __init__(self, size, fullscreen):
        self.screen = pygame.display.set_mode(size)
        
        self.ball = Ball(self.screen, (255, 0, 0), size[0], size[1])
        self.bars = Bars(self.screen, (255, 230, 0), size[0], size[1])

        pygame.display.set_caption('Pong v0.1')

    def handle_events(self):
        for event in pygame.event.get():
            if event.type == KEYDOWN:
                if event.key in (K_UP, K_DOWN):
                    self.bars.events(event.key)
                elif event.key == K_ESCAPE:
                    self.run = False
            elif event.type == QUIT:
                self.run = False

    def draw(self):
        self.screen.fill((0, 0, 0))
        self.ball.update()
        self.ball.draw()
        self.bars.update()
        self.bars.draw()
        
    def manage(self):
        if self.bars.collide(self.ball):
            self.ball.reverse()
        
        if self.ball.crash:
            self.run = False
    
    def loop(self):
        
        while self.run:
            self.handle_events()
            self.manage()
            self.draw()

            pygame.display.flip()

def main():
    game = Game((800,600), True)
    game.loop()

if __name__ == '__main__':
    main()
