//var screenSizeX = screen.width; //var screenSizeY = screen.height; var screenSizeX = 640; var screenSizeY = 880; var screenGroundLevel = 28; var screenEdgeTop = 0; var screenEdgeLeft = 0; var screenEdgeRight = screenSizeX; var screenEdgeBottom = screenSizeY - screenGroundLevel; var ballGravity = 7; var ballElasticity = 6; var ballAirResistance = 0.015; var ballMaxSpeed = 150; var ballMaxSpeed2 = -150; var ballWidth = 100; var ballHeight = 100; var ballEdgeTop = 0; var ballEdgeLeft = 0; var ballEdgeRight = 100; var ballEdgeBottom = 100; var ballX = 0; var ballY = 0; var ballVelocityX = 0; var ballVelocityY = 0; var debug = false; var frameNext; var basketY; var throwBall = false; var gameRoundTime = 181; var gameScore = 0; var gameTime = gameRoundTime; var timeNext; var gPause = false; var ballHitX; var arrive = function(){ new Media('player_arrive.wav').play(); } var button = function(){ new Media('button.wav').play(); } var click = function(){ new Media('click.wav').play(); } window.addEventListener( 'load', function() { window.resizeTo( screen.width, screen.height ); window.moveTo( 0, 0 ); basketball = document.getElementById("ball"); ballCenter(); ballMove(); updateScore(); updateTime(); }, false ); function updateTime() { gameTime = gameTime - 1; gameTimeS = "00" + gameTime.toString(); gameTime1 = gameTimeS.substr(-3,1); gameTime2 = gameTimeS.substr(-2,1); gameTime3 = gameTimeS.substr(-1,1); document.getElementById("time1").src = "img/" + gameTime1 + ".png" document.getElementById("time2").src = "img/" + gameTime2 + ".png" document.getElementById("time3").src = "img/" + gameTime3 + ".png" if( gameTime != 0 ) timeNext = setTimeout( "updateTime()", 1000 ); } function ballScore() { if( gameTime != 0 ) { if( gameScore < 999 ) { if( ballHitX < ( screenSizeX / 2 )) gameScore = gameScore + 2; else gameScore = gameScore + 3; updateScore() ; arrive(); } } } function updateScore() { gameScoreS = "00" + gameScore.toString(); gameScore1 = gameScoreS.substr(-3,1); gameScore2 = gameScoreS.substr(-2,1); gameScore3 = gameScoreS.substr(-1,1); document.getElementById("score1").src = "img/" + gameScore1 + ".png" document.getElementById("score2").src = "img/" + gameScore2 + ".png" document.getElementById("score3").src = "img/" + gameScore3 + ".png" } function gameRestart() { if( gPause ) gamePause(); clearTimeout( timeNext ); clearTimeout( frameNext ); gameScore = 0; gameTime = gameRoundTime; ballCenter(); ballMove(); updateScore(); updateTime(); } function gameClose() { window.close(); } function pauseHover() { document.getElementById("pause").style.background = "url(img/bPause_hover.png)"; } function pauseOut() { if( !gPause ) document.getElementById("pause").style.background = "url(img/bPause.png)"; } function gamePause() { if( gPause ) { ballMove(); if( gameTime != 0 ) updateTime(); gPause = false; document.getElementById("pause").style.background = "url(img/bPause.png)"; } else { clearTimeout( timeNext ); clearTimeout( frameNext ); gPause = true; document.getElementById("pause").style.background = "url(img/bPause_hover.png)"; } } function ballCenter() { ballX = ( screenSizeX / 2 ) - 20; ballY = screenSizeY - 200; ballVelocityX = 0; ballVelocityY = -50; basketball.style.top = ballY + "px"; basketball.style.left = ballX + "px"; basketball.style.display = "block"; basketY = screenSizeY - 600; document.getElementById("basket").style.top = basketY + "px" ; document.getElementById("basket").style.display = "block" ; document.getElementById("scoreTable").style.left = (( screenSizeX / 2 ) - 150) + "px" ; document.getElementById("scoreTable").style.display = "block" ; } function ballMove() { ballApplyGravity(); ballApplyAirResistance(); // Cut max speeds if( ballVelocityX > ballMaxSpeed ) ballVelocityX = ballMaxSpeed; if( ballVelocityX < ballMaxSpeed2 ) ballVelocityX = ballMaxSpeed2; if( ballVelocityY > ballMaxSpeed ) ballVelocityY = ballMaxSpeed; if( ballVelocityY < ballMaxSpeed2 ) ballVelocityY = ballMaxSpeed2; ballX = ballX + ballVelocityX; ballY = ballY + ballVelocityY; ballHitBorder(); ballHitBasket(); // Cut tiny speeds if( ballVelocityX > 0 && ballVelocityX < 2 ) ballVelocityX = 0; if( ballVelocityX < 0 && ballVelocityX > -2 ) ballVelocityX = 0; if( ballVelocityY > 0 && ballVelocityY < 2 ) ballVelocityY = 0; if( ballVelocityY < 0 && ballVelocityY > -2 ) ballVelocityY = 0; basketball.style.top = ballY + "px"; basketball.style.left = ballX + "px"; if( ballIsMoving() ) { frameNext = setTimeout( "ballMove()", 30 ); } } function ballApplyGravity() { ballVelocityY = ballVelocityY + ballGravity; } function ballApplyAirResistance() { if( ballVelocityY > 0 ) { ballVelocityY = ballVelocityY - ( ballVelocityY * ballAirResistance ); if( ballVelocityY < 0 ) ballVelocityY = 0; } else if( ballVelocityY < 0 ) { ballVelocityY = ballVelocityY - ( ballVelocityY * ballAirResistance ); if( ballVelocityY > 0 ) ballVelocityY = 0; } if( ballVelocityX > 0 ) { ballVelocityX = ballVelocityX - ( ballVelocityX * ballAirResistance ); if( ballVelocityX < 0 ) ballVelocityX = 0; } else if( ballVelocityX < 0 ) { ballVelocityX = ballVelocityX - ( ballVelocityX * ballAirResistance ); if( ballVelocityX > 0 ) ballVelocityX = 0; } } function ballHitBorder() { if( ( ballY + ballEdgeBottom ) > screenEdgeBottom ) { ballY = screenEdgeBottom - 100 + ballEdgeTop; ballVelocityY = ( ballVelocityY * -1 ) + ballElasticity; button(); if( ballVelocityX > 0 ) { ballVelocityX = ballVelocityX - ( ballVelocityX * ballAirResistance * 10 ); if( ballVelocityX < 0 ) ballVelocityX = 0; } else if( ballVelocityX < 0 ) { ballVelocityX = ballVelocityX - ( ballVelocityX * ballAirResistance * 10 ); if( ballVelocityX > 0 ) ballVelocityX = 0; } } if( ( ballX + ballEdgeLeft ) < screenEdgeLeft ) { ballX = screenEdgeLeft + ballEdgeLeft; ballVelocityX = ( ballVelocityX * -1 ) - ballElasticity; if( ballVelocityX < 0 ) ballVelocityX = 0; } if( ( ballX + ballEdgeRight ) > screenEdgeRight ) { ballX = screenEdgeRight - 100 + ballEdgeLeft; ballVelocityX = ( ballVelocityX * -1 ) + ballElasticity; if( ballVelocityX > 0 ) ballVelocityX = 0; } } function ballHitBasket() { if( ballX < 45 ) { if( ( ballY > basketY - 20 ) && ( ballY < basketY + 100 )) { if( ballVelocityY > 0 ) { if( ( ballVelocityX > -100 ) && ( ballVelocityX < 100 ) ) { if( throwBall ) { ballVelocityY = ballVelocityY / 2; ballScore(); throwBall = false; } } } else if( ballVelocityY < 0 ) { ballVelocityY = ( ballVelocityY * -1 ); } } } else if( ballX < 140 ) { if( ( ballY > basketY - 100 ) && ( ballY < basketY )) { ballVelocityY = ( ballVelocityY * -1 ); ballVelocityX = ballX - 80; if( ballVelocityX > -5 && ballVelocityX < 5 ) { ballVelocityX = -2; } } } } function ballIsMoving() { if( ( ballVelocityX == 0 ) && ( ballVelocityY == 0 )) { if( ( ballY + ballEdgeBottom ) >= screenEdgeBottom ) return false; } return true; } function ballHit() { if( gPause ) return; throwBall = true; ballHitX = ballX; e = window.event; ballVelocityY = ballVelocityY - (e.offsetY - ( ballHeight / 2 + ballEdgeTop ) ) * 3; ballVelocityX = ballVelocityX + ( e.offsetX - ( ballWidth / 2 + ballEdgeLeft ) ) * -1.5; clearTimeout( frameNext ); ballMove(); }