Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. function iniciar(){
  2.  
  3. var bot=document.getElementById('boton');
  4. boton.disabled=true;
  5. boton.style.background="#F7D358";
  6. ctx=$('#canvas')[0].getContext("2d");
  7. contenedor=document.getElementById('contenedor');
  8. textopantalla=document.getElementById('pantalla');
  9. textopuntuacion=document.getElementById('puntuacion');
  10. x=150;
  11. y=280;
  12.  
  13. $(document).mousemove(onMouseMove);
  14.  
  15. pantalla=1;
  16. puntuacion=0;
  17. vidas=6;
  18. bloques=15;
  19.  
  20.  
  21. muerto=false;
  22. textopantalla.innerHTML="<div>"+' Pantalla : '+pantalla+"</div>";
  23. contenedor.innerHTML="<div>"+' Vidas : '+vidas+"</div>";
  24. textopuntuacion.innerHTML="<div>"+' Puntuacion : '+puntuacion+"</div>";
  25. WIDTH=$('#canvas').width();
  26. HEIGHT=$('#canvas').height();
  27. paddlex=WIDTH/2; //PARA PONER EL RECTANGULO EN LA MITAD DEL ANCHO;
  28. paddleh=10; //ANCHO DEL RECTANGULO
  29. paddley=HEIGHT-paddleh; //PARA PONER EL RECTANGULO UN POCO POR ENCIMA DEL FINAL DEL ANCHO
  30. paddlew=75; //LARGO DEL RECTANGULO
  31. //PARA EL RATON
  32. canvasMinX=$('#canvas').offset().left;
  33. canvasMaxX=canvasMinX + WIDTH;
  34. //PARA BLOQUES
  35. //NCOLS=5;
  36. NROWS=3;
  37. BRICKWIDTH=(WIDTH/NCOLS) - 1;
  38. BRICKWEIGHT=15;
  39. PADDING=1;
  40. bricks=new Array(NROWS);
  41. for (i=0;i<NROWS;i++){
  42. bricks[i]=new Array(NCOLS);
  43. for (j=0;j<NCOLS;j++){
  44. bricks[i][j]=1;
  45. }
  46. }
  47. dureza=new Array(NROWS);
  48. for (i=0;i<NROWS;i++){
  49. dureza[i]=new Array(NCOLS);
  50. for (j=0;j<NCOLS;j++){
  51. dureza[i][j]=2;
  52. }
  53. }
  54.  
  55.  
  56.  
  57. return setInterval(draw,10+tiempo);
  58. }
  59.  
  60. function circle(x,y,r){
  61. ctx.beginPath();
  62. ctx.arc(x,y,r,0,Math.PI*2,true);
  63. ctx.closePath();
  64. ctx.fill();
  65. }
  66.  
  67. function rect(x,y,w,h){
  68. ctx.beginPath();
  69. ctx.rect(x,y,w,h);
  70. ctx.closePath();
  71. ctx.fill();
  72. }
  73.  
  74. function clear(){
  75. ctx.clearRect(0,0,WIDTH,HEIGHT);
  76. }
  77.  
  78. function draw(){
  79. textopuntuacion.innerHTML="<div>"+' Puntuacion : '+puntuacion+"</div>";
  80. if(vidas==0){
  81. ctx.textAlign="center";
  82. ctx.textBaseline="middle";
  83. ctx.font="bold 30px verdana";
  84. ctx.fillStyle="#FF0080";
  85. ctx.fillText("HAS PERDIDO!!",150,150);}
  86.  
  87. ctx.fillStyle="#2ECCFA";
  88. clear();
  89.  
  90. ctx.fillStyle="#00FF00";
  91. circle(x,y,10);
  92.  
  93. if(rightDown){
  94. if(paddlex<=WIDTH-75){
  95. paddlex +=5;
  96. }
  97. else{
  98. paddlex=WIDTH-73;
  99. }
  100. }
  101. else if(leftDown){
  102. if(paddlex>=5){
  103. paddlex -=5;
  104. }
  105. else{
  106. paddlex=0;
  107. }
  108. }
  109.  
  110. ctx.fillStyle="#0000FF";
  111. rect(paddlex,paddley,paddlew,paddleh);
  112.  
  113. //bloques
  114.  
  115. for (i=0;i<NROWS;i++){
  116. for (j=0;j<NCOLS;j++){
  117. if(bricks[i][j]==1 && dureza[i][j]==2){
  118. ctx.fillStyle="#FF0000";
  119. //ctx.fillStyle='["#FF1C0A","#FFFD0A","#00A308","#0008DB","#EB0093"]';
  120. rect((j * (BRICKWIDTH+PADDING)) + PADDING, (i*(BRICKWEIGHT+PADDING))+PADDING,BRICKWIDTH,BRICKWEIGHT);
  121. }
  122. else if(bricks[i][j]==1 && dureza[i][j]==1){
  123. ctx.fillStyle="#FFFF00";
  124. //ctx.fillStyle='["#FF1C0A","#FFFD0A","#00A308","#0008DB","#EB0093"]';
  125. rect((j * (BRICKWIDTH+PADDING)) + PADDING, (i*(BRICKWEIGHT+PADDING))+PADDING,BRICKWIDTH,BRICKWEIGHT);
  126. }
  127. }
  128. }
  129. ctx.fillStyle="#848484";
  130. rect(PADDING,PADDING,BRICKWIDTH,BRICKWEIGHT);
  131. rect(PADDING, (BRICKWEIGHT+PADDING)+PADDING,BRICKWIDTH,BRICKWEIGHT);
  132. rect(4*(BRICKWIDTH+PADDING) + PADDING, 2*(BRICKWEIGHT+PADDING)+PADDING,BRICKWIDTH,BRICKWEIGHT);
  133. //si golpea la bola un bloque
  134. rowheight = BRICKWEIGHT+PADDING;
  135. colwidth = BRICKWIDTH+PADDING;
  136. row=Math.floor(y/rowheight);
  137. col=Math.floor(x/colwidth);
  138.  
  139.  
  140. //rompemos el bloque y la bola vuelve
  141. if(y < NROWS * rowheight && row >= 0 && col >= 0 && bricks[row][col]==1 && dureza[row][col]==2){
  142. dy=-dy;
  143. puntuacion=puntuacion+50;
  144. bricks[row][col]=1;
  145. dureza[row][col]=1;
  146. }
  147. else if(y < NROWS * rowheight && row >= 0 && col >= 0 && bricks[row][col]==1 && dureza[row][col]==1){
  148. dy=-dy;
  149. bricks[row][col]=0;
  150. bricks[0][0]=1;
  151. bricks[1][0]=1;
  152. bricks[2][4]=1;
  153. if (bricks[row][col]==0){
  154. bloques--;
  155. puntuacion=puntuacion+100;
  156. }
  157. }
  158.  
  159. if(x + dx > WIDTH || x + dx <0){
  160. dx=-dx;
  161. }
  162. if (y + dy <0){
  163. dy=-dy;
  164. }
  165. else if(y + dy +10> HEIGHT-paddleh){
  166. if(x > paddlex && x < paddlex + paddlew){
  167. dx=8*((x-(paddlex+paddlew/2))/paddlew);
  168. dy=-dy;
  169. }
  170. else if(y+dy+10>HEIGHT){
  171. muerto=true;
  172. clearInterval(intervalId);
  173. }
  174. }
  175. if(muerto){
  176. vidas--;
  177. muerto=false;
  178. contenedor.innerHTML="<div>"+' Vidas : '+vidas+"</div>";
  179. }
  180.  
  181. x += dx;
  182. y += dy;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement