# Dan Delsol - 12/2014
# Simple example of using the Brython library for making HTML5 games in Python.
# Developped and tested under Firefox 33. And some testing under Chrome.
# =========
# The original goal is to be able to port games, initialy developped on www.codeskultor.org over to Brython ( http://www.brython.info ) since the CodeSkultor 
# libraries are not open source and the owners, do not allow the use of their libraries outside of the codeskultor.org website.
# Brython, however, is open source and free (freedom) and the games developped with it can be hosted anywhere you choose. See licence info on the Brython website.

# Import Python modules 
from browser import window, document, timer
import javascript

# Load images
# Load JS image objects (bug in Brython 3.0 : can't load IMG object directly wthout using a javascript.JSConstructor object)
imgBall = javascript.JSConstructor(window.Image)()
imgBall.src = 'img/shot3.png'

# Load sound / music
soundBall = javascript.JSConstructor(window.Audio)('http://webswap.free.fr/brythonrocks/img/missile.wav')
soundBall.load()

# Get a reference to the HTML canvas object on which you will draw shapes and images. And its context (2d) property.
canvas = document["stage"]
ctx = canvas.getContext("2d")

# Define some global Python variables
WIDTH = 200          # canvas width
HEIGHT = 200         # canvas height
gameFPS = 20         # game display frequency: frames per seconde

class ball:
    def __init__(self):
        self.pos = (WIDTH/2, HEIGHT/2)
        self.vel = [0,0]
        self.radius = 90

    def draw(self):
        global ctx
        drawRotateImage(imgBall, self.pos[0], self.pos[1], rotationAngle = 0, 90, 90, 90, 90)
        #drawRotateImage(image, posX, posY, rotationAngle, imgSizeX, imgSizeY, displaySizeX, displaySizeY):

    def update(self):
        self.pos[0] += int(self.vel[0])
        self.pos[1] += int(self.vel[1])	
		# wrap screen so the ball does not dissapear from the canvas
        self.pos[0] %= WIDTH
        self.pos[1] %= HEIGHT

    def get_position(self):
        return self.pos  

    def get_radius(self):
        return self.radius 
	
# General utility function to draw image on the canvas, rotate it and scale it.
def drawRotateImage(image, posX, posY, rotationAngle, imgSizeX, imgSizeY, displaySizeX, displaySizeY):
    global ctx
    # ctx.clearRect(0,0,canvas.width, canvas.height)     
    ctx.save()
    # position the image where you want it to display on the canvas (posX, posY)
    ctx.translate(posX, posY)
    ctx.rotate(rotationAngle)	
    # display image at the coords of the center of the image, so the rotation happens around its own center: (-imgSizeX/2), (-imgSizeY/2)
    ctx.drawImage(image,(-imgSizeX/2), (-imgSizeY/2), displaySizeX, displaySizeY)
    ctx.restore()
				
def drawGame():
    global ctx, myBall
    ctx.clearRect(0,0,canvas.width, canvas.height)
    myBall.draw()
    # display ball position as text
    theText = "Ball position: " + str(myBall.get_position())
    ctx.font = "14px Arial"
    ctx.fillStyle = "black"
    ctx.fillText(theText,30,30)

def loopGame():
    # update ball object
    myBall.update()
    # draw the canvas
    drawGame()

def process_keyDown(ev):
    global myBall
    # The line below allows you to test key codes for new keyboard keys
    #print("keyCode = ", ev.keyCode)   # up: 38, left:37, right: 39, down:40	
    if(ev.keyCode == 37):  # left
        myBall.vel[0] = -2
    elif( ev.keyCode == 39):  # right 
        myBall.vel[0] = 2
    # prevent regular behavior of the browser for this key		
    ev.preventDefault()

def process_keyUp(ev):
    global myBall
    if(ev.keyCode == 37):  # left
        myBall.vel[0] = 0
        soundBall.play()
    elif(ev.keyCode == 39):  # right 
        myBall.vel[0] = 0
        soundBall.play()
    ev.preventDefault()	
	
# register keyboard handler
document.bind("keydown", process_keyDown)
document.bind("keyup", process_keyUp)

# create objects	
myBall = ball()

#Start game loop using a timer
gameLoop = timer.set_interval(loopGame,1000/gameFPS)