// Declarationsfunction udtPlayField(obj) { this.elm = obj; this.height = new Number(window.innerHeight-20); this.width = new Number(window.innerWidth-20); this.move = MovePlayField;}function udtPaddle(obj,wide) { this.elm = obj; this.height = new Number(15); this.width = new Number(wide); this.halfwidth = new Number(wide/2); this.left = new Number(((PlayField.width/2) - this.halfwidth)); this.top = new Number(PlayField.height - this.height); this.move = MovePaddle;}function udtPaddleWidthFrame(obj,high,wide,top,left) { this.elm = obj; this.height = new Number(high+3); this.width = new Number(wide+3); this.top = new Number(top-3); this.left = new Number(left-3);}function udtPaddleWidth(obj,objFrame,high) { this.elm = obj; this.height = new Number(high); this.width = new Number(20); this.top = new Number(10); this.left = new Number(PlayField.width - this.width -10); this.frame = new udtPaddleWidthFrame(objFrame,this.height,this.width,this.top,this.left); this.move = MovePaddleWidth;}function udtBall(obj,size) { this.elm = obj; this.angle = new Number(180); this.speed = new Number(iInitialBallSpeed); this.top = new Number(10); this.left = new Number((PlayField.width/2) - (size/2)); this.size = new Number(size);}function udtClock(obj,start) { this.elm = obj; this.time = new Date(start);}var PlayField = new udtPlayField(null);var PaddleWidth = new udtPaddleWidth(null,null,0);var Paddle = new udtPaddle(null,0);var Left = new Number(0);var Top = new Number(0);var Ball = new udtBall(null,0);var Clock = new udtClock(null,0);var iInitialBallSpeed = new Number(5);var iInitialInterval = new Number(80);var iInterval = new Number(80);var blnResume = new Boolean(false);var blnBusy = new Boolean(false);var idGameInterval = new Number();var idClockInterval = new Number();// functiesfunction MovePlayField() { with (PlayField.elm.style) {  height = PlayField.height + 'px';  width = PlayField.width + 'px'; } return true;}function MovePaddle() { with (Paddle.elm.style) {  height = Paddle.height + 'px';  width = Paddle.width + 'px';  top = Paddle.top + 'px';  left = Paddle.left + 'px'; } return true;}function MovePaddleWidth() { with (PaddleWidth.elm.style) {  height = PaddleWidth.height + 'px';  width = PaddleWidth.width + 'px';  top = PaddleWidth.top + 'px';  left = PaddleWidth.left + 'px'; } with (PaddleWidth.frame.elm.style) {  height = PaddleWidth.frame.height + 'px';  width = PaddleWidth.frame.width + 'px';  top = PaddleWidth.frame.top + 'px';  left = PaddleWidth.frame.left + 'px'; } return true;}function MoveBall() { with (Ball.elm.style) {  top = Ball.top + 'px';  left = Ball.left + 'px';  height = Ball.size + 'px';  width = Ball.size + 'px'; } return true;}function PaddleCoords(evt){Left = evt.clientX;Top = evt.clientY;  if (Left < Paddle.halfwidth) {     Left = 0;  } else if (Left < (Paddle.left + Paddle.halfwidth)) {    Left -= (Paddle.halfwidth-2);  } else if (Left > (PlayField.width - Paddle.halfwidth)) {    Left = (PlayField.width - Paddle.width);  } else if (Left > (Paddle.left + Paddle.halfwidth)) {    Left -= (Paddle.halfwidth+2);  } Paddle.left = Left; Paddle.elm.style.left = Left + 'px'; return true;}function WidthCoords(evt){ if ((evt.clientX > PaddleWidth.frame.left) && (evt.clientX < (PaddleWidth.frame.left+PaddleWidth.frame.width))) {  if ((evt.clientY > PaddleWidth.frame.top) && (evt.clientY < (PaddleWidth.frame.top+PaddleWidth.frame.height))) {   PaddleWidth.top = evt.clientY;   PaddleWidth.height = PaddleWidth.frame.top + PaddleWidth.frame.height - evt.clientY;   PaddleWidth.elm.style.top = evt.clientY + 'px';   PaddleWidth.elm.style.height = PaddleWidth.height + 'px';   Paddle.width = PaddleWidth.height + 15; //minimumwaarde voor bat = 15px   Paddle.halfwidth = Paddle.width/2;   Paddle.elm.style.width = Paddle.width + 'px';   Ball.size = ((PaddleWidth.height/10)+10);   Ball.elm.style.fontSize = Ball.size + 'px';   Ball.elm.style.height = Ball.size + 'px';   Ball.elm.style.width = Ball.size + 'px';  } } return true;}function Main() { PlayField = null; PaddleWidth = null; Paddle = null; Ball = null; Clock = null; PlayField = new udtPlayField(document.getElementById('PlayField')); if (PlayField!=null) {  PlayField.elm.onmousemove = PaddleCoords;  Clock = new udtClock(document.getElementById('Clock'),0);  if (Clock!=null) {    Clock.elm.focus();    Clock.elm.onclick = Start;  }  PaddleWidth = new udtPaddleWidth(document.getElementById('PaddleWidth'),document.getElementById('PaddleWidthFrame'),115);  if (PaddleWidth!=null) {   PaddleWidth.frame.elm.onmousemove = WidthCoords; /* terug omhoog */   PaddleWidth.elm.onmousemove = WidthCoords; /* omlaag */   PaddleWidth.move();   Paddle = new udtPaddle(document.getElementById('Paddle'),(PaddleWidth.height+15));   if (Paddle!=null) {    Paddle.move();    Ball = new udtBall(document.getElementById('Ball'),((PaddleWidth.height/10)+10));    if (Ball!=null) {     MoveBall();     return (Ball!=null);    } else { return false; }   } else { return false; }  } else { return false; } } else { return false; }}function BounceOffCeiling(direction) { if (direction > 0) { //rechts  Ball.angle = 180 - Ball.angle; //kloksgewijze draaiing over kwadranten } else if (direction < 0) { //left  Ball.angle = 180 + (360-Ball.angle); //antikloksgewijze draaiing over kwadranten } else { //recht omhoog  Ball.angle = 180; } CircleCorrection(); return true;}function BounceOffWall(which,direction) { switch (which) {  case 0 :   //linkerwand   if (direction > 0) { //omhoog    Ball.angle = 90 - (Ball.angle - 270);   } else if (direction < 0) { //omlaag    Ball.angle = 90 + (270 - Ball.angle);   }   CircleCorrection();   return true;   break;  case 1 :   //rechterwand   if (direction > 0) { //omhoog    Ball.angle = 270 + (90 - Ball.angle);   } else if (direction < 0) { //omlaag    Ball.angle = 90 - (Ball.angle-270);   }   CircleCorrection();   return true;   break; }}function BounceOffPaddle(side, direction) { switch (side) {  case -1 :   // links van het midden   if (direction>0) { // naar rechts    Ball.angle  = (180 - Ball.angle);   } else if (direction<0) { // naar left    Ball.angle = 360 - (Ball.angle-180);   } else { // recht omhoog of omlaag    Ball.angle = 320;   }   CircleCorrection();   return true;   break;  case 0 :   // exact in het midden   if (direction>0) { // naar rechts    Ball.angle = (180 - Ball.angle);   } else if (direction<0) { // naar left    Ball.angle = 360 - (Ball.angle-180);   } else { // recht omhoog of omlaag    Ball.angle = 0;   }   CircleCorrection();   return true;   break;  case 1 :   // rechts van het midden   if (direction>0) { // naar rechts    Ball.angle = (180 - Ball.angle);   } else if (direction<0) { // naar left    Ball.angle = 360 - (Ball.angle-180);   } else { // recht omhoog of omlaag    Ball.angle = 30;   }   CircleCorrection();   return true;   break; }}function CircleCorrection() { while (Ball.angle >= 360) {  Ball.angle-=360; } while (Ball.angle < 0) {  Ball.angle+=360; }}function HitTest() { 	// wandentest	if (Ball.left <= 0) {		if (blnBusy==false) {			blnBusy = true;			Ball.left = 0;			BounceOffWall(0,gradCos(Ball.angle));		}	} else if ((Ball.left+Ball.size) >= PlayField.width) {		if (blnBusy==false) {			blnBusy = true;			Ball.left = PlayField.width-Ball.size;			BounceOffWall(1,gradCos(Ball.angle));		}	}	if (Ball.top <= 0) { /* plafondtest */		if (blnBusy==false) {			blnBusy = true;			Ball.top = 0;			BounceOffCeiling(gradSin(Ball.angle));		} /* nog bezig */	} else if ((Ball.top + Ball.size) >= Paddle.top) { /* Paddletest */		if (((Ball.left+Ball.size) > Paddle.left) && (Ball.left < (Paddle.left+Paddle.width))) {			if (blnBusy==false) {				blnBusy = true;				if ((Ball.left + (Ball.size/2))==(Paddle.left+Paddle.halfwidth)) {  /* exact in het midden */					BounceOffPaddle(0, 0);				} else if (Ball.left>(Paddle.left+Paddle.halfwidth)) { /* rechts van het midden */					BounceOffPaddle(1, gradSin(Ball.angle));				} else { /* left van het midden */					BounceOffPaddle((-1), gradSin(Ball.angle));				} /* plaats op paddle */			} /* nog bezig */		} else if (Ball.top >= Paddle.top) { /* vloertest */			Loose();		}	} else {		blnBusy = false;	}	return true;}function Loose() { Start(); Ball = null; Ball = new udtBall(document.getElementById('Ball'),((PaddleWidth.height/10)+10)); if (Ball!=null) {  MoveBall(); } alert('Oops!'); return true;} // Sin en Cosin van Math object telt radialen. // Radialen zijn (Graden/180)*pi()function gradSin(grad) { var t = Math.sin((grad/180)*Math.PI); return (t.toFixed(2));}function gradCos(grad) { var t = Math.cos((grad/180)*Math.PI) return (t.toFixed(2));}function ShowTime() { return (Clock.time.getUTCHours() + ':' + Clock.time.getUTCMinutes() + ':' + Clock.time.getUTCSeconds());} function Go() { if (blnResume==true) {  Ball.top -= (Ball.speed*gradCos(Ball.angle));  Ball.left += (Ball.speed*gradSin(Ball.angle));  Ball.elm.style.top = Ball.top + 'px';  Ball.elm.style.left = Ball.left + 'px';  HitTest(); } return true;}function Start() { blnBusy = false; iInterval = iInitialInterval; Ball.speed = iInitialBallSpeed; if (blnResume == false) {   Clock.time.setTime(0);   idGameInterval = setInterval('Go()',iInterval);   idClockInterval = setInterval('Timer()',1000);   blnResume = true; } else {   blnResume = false;   clearInterval(idClockInterval);   clearInterval(idGameInterval); } return true;}function goFaster() {  if ((idGameInterval)&&(iInterval>1)) {    clearInterval(idGameInterval);    iInterval-=1;    Ball.speed += 1;    idGameInterval = setInterval('Go()',iInterval);  }	}function Timer() {  if (blnResume==true) {   Clock.time.setTime(1000+Clock.time.getTime());   Clock.elm.value = ShowTime();   if ((Clock.time.valueOf() % 1000 == 0)&&(iInterval>1)) {     goFaster();   }  } else {    Clock.elm.value = 'Resume';  }}
