game.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. var blockSize = 55;
  2. var numBubbles = 5;
  3. var board = new Array(numBubbles);
  4. var component;
  5. var gameStarted = false;
  6. var bubbleStep = 5;
  7. var bubblesClicked = 0;
  8. var updateTime = 200
  9. var score = 0;
  10. var startTime = 0;
  11. function onUpdate()
  12. {
  13. var step = 5;
  14. score = (new Date() - startTime)/1000;
  15. // %2.02f
  16. var p1 = Math.floor(score)
  17. var p2 = Math.floor(100*(score - p1))
  18. scoreText.text = "Time: " + p1 + "." + p2 + " ";
  19. for (var n = 0; n < numBubbles; n++)
  20. {
  21. var x = board[n].x + board[n].dx;
  22. var y = board[n].y + board[n].dy;
  23. if(x > (background.width - board[n].width) || x < 0)
  24. {
  25. board[n].dx = -board[n].dx;
  26. x = x + 2*board[n].dx;
  27. }
  28. if(y > (background.height - board[n].height) || y < 0)
  29. {
  30. board[n].dy = -board[n].dy;
  31. y = y + 2*board[n].dy;
  32. }
  33. board[n].x = x;
  34. board[n].y = y;
  35. }
  36. }
  37. function removeBubble(mx,my)
  38. {
  39. if(!gameStarted)
  40. return;
  41. for (var n = 0; n < numBubbles; n++)
  42. {
  43. if(board[n].visible)
  44. {
  45. if( (mx > board[n].x) && (mx < (board[n].x + board[n].width)) &&
  46. (my > board[n].y) && (my < (board[n].y + board[n].height)) )
  47. {
  48. board[n].visible = false;
  49. ++bubblesClicked;
  50. //bubblepop.play();
  51. break;
  52. }
  53. }
  54. }
  55. if(bubblesClicked >= numBubbles)
  56. {
  57. gameOverDialog.show("Congratulations !\n" +
  58. "You popped all bubbles in\n" +
  59. score + " seconds !");
  60. newGameBut.text = " Start "
  61. gameStarted = false;
  62. bubblesClicked = 0;
  63. gameTimer.stop();
  64. }
  65. }
  66. function aboutGame()
  67. {
  68. aboutDialog
  69. aboutDialog.show("Bubble kid by:\n" +
  70. "Marcelo Barros de Almeida\n" +
  71. "<marcelobarrosalmeida@gmail.com>");
  72. }
  73. function stopGame()
  74. {
  75. //Delete blocks from previous game
  76. for (var i = 0; i < numBubbles; i++)
  77. {
  78. if (board[i] != null)
  79. board[i].destroy();
  80. }
  81. newGameBut.text = " Start "
  82. gameStarted = false;
  83. bubblesClicked = 0;
  84. gameTimer.stop();
  85. }
  86. function startNewGame()
  87. {
  88. if(gameStarted)
  89. {
  90. stopGame();
  91. }
  92. else
  93. {
  94. //Initialize Board
  95. board = new Array(numBubbles);
  96. for (var n = 0; n < numBubbles; n++)
  97. {
  98. board[n] = null;
  99. createBlock(n);
  100. }
  101. bubblesClicked = 0;
  102. gameTimer.start();
  103. gameStarted = true;
  104. newGameBut.text = " Stop ";
  105. startTime = new Date();
  106. score = 0;
  107. }
  108. }
  109. function createBlock(n)
  110. {
  111. if (component == null)
  112. component = Qt.createComponent("Bubble.qml");
  113. // Note that if Block.qml was not a local file, component.status would be
  114. // Loading and we should wait for the component's statusChanged() signal to
  115. // know when the file is downloaded and ready before calling createObject().
  116. if (component.status == Component.Ready)
  117. {
  118. var obj = component.createObject(background);
  119. if (obj == null) {
  120. console.log("error creating bubble");
  121. console.log(component.errorString());
  122. return false;
  123. }
  124. obj.index = n;
  125. obj.width = blockSize;
  126. obj.height = blockSize;
  127. obj.x = Math.random()*(background.width-blockSize);
  128. obj.y = Math.random()*(background.height-blockSize);
  129. obj.dx = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
  130. obj.dy = Math.random() > 0.5 ? bubbleStep : -bubbleStep;
  131. board[n] = obj;
  132. }
  133. else
  134. {
  135. console.log("error loading bubble component");
  136. console.log(component.errorString());
  137. return false;
  138. }
  139. return true;
  140. }
  141. function quitGame()
  142. {
  143. Qt.quit()
  144. }
  145. function setGameSpeed(s)
  146. {
  147. var old = gameStarted;
  148. if(gameStarted)
  149. startNewGame();
  150. if(s == 0)
  151. updateTime = 200;
  152. else if(s == 1)
  153. updateTime = 75;
  154. else if(s == 2)
  155. updateTime = 25;
  156. gameTimer.interval = updateTime;
  157. if(old)
  158. startNewGame();
  159. }
  160. function getUpdateTime()
  161. {
  162. return updateTime;
  163. }
  164. function setNumBubbles(n)
  165. {
  166. var old = gameStarted;
  167. if(gameStarted)
  168. startNewGame();
  169. if(n == 0)
  170. numBubbles = 5;
  171. else if(n == 1)
  172. numBubbles = 10;
  173. else if(n == 2)
  174. numBubbles = 20;
  175. if(old)
  176. startNewGame();
  177. }
  178. function getNumBubbles()
  179. {
  180. return numBubbles;
  181. }
  182. function getScore()
  183. {
  184. if(gameStarted)
  185. return score;
  186. else
  187. return "--";
  188. }