scripts.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. function setState(state) {
  2. gameArea.state = state;
  3. }
  4. function newGame() {
  5. gameArea.state = "game";
  6. gameView.state = "";
  7. gameView.hole1.state = "";
  8. gameView.hole2.state = "";
  9. gameView.hole3.state = "";
  10. gameView.hole4.state = "";
  11. gameView.hole5.state = "";
  12. gameView.hole6.state = "";
  13. gameView.hole7.state = "";
  14. gameView.hole8.state = "";
  15. gameView.hole1.image2 = "hole";
  16. gameView.hole2.image2 = "hole";
  17. gameView.hole3.image2 = "hole";
  18. gameView.hole4.image2 = "hole";
  19. gameView.hole5.image2 = "hole";
  20. gameView.hole6.image2 = "hole";
  21. gameView.hole7.image2 = "hole";
  22. gameView.hole8.image2 = "hole";
  23. gameView.hole1.hasMonkey = false;
  24. gameView.hole2.hasMonkey = false;
  25. gameView.hole3.hasMonkey = false;
  26. gameView.hole4.hasMonkey = false;
  27. gameView.hole5.hasMonkey = false;
  28. gameView.hole6.hasMonkey = false;
  29. gameView.hole7.hasMonkey = false;
  30. gameView.hole8.hasMonkey = false;
  31. clickCount = 0;
  32. gameArea.gameTimerInterval = 1000;
  33. gameArea.hideTimerInterval = 1000;
  34. gameTimer.interval = gameTimerInterval;
  35. gameView.hole1.flipSpeed = gameArea.gameTimerInterval / 2;
  36. gameView.hole2.flipSpeed = gameArea.gameTimerInterval / 2;
  37. gameView.hole3.flipSpeed = gameArea.gameTimerInterval / 2;
  38. gameView.hole4.flipSpeed = gameArea.gameTimerInterval / 2;
  39. gameView.hole5.flipSpeed = gameArea.gameTimerInterval / 2;
  40. gameView.hole6.flipSpeed = gameArea.gameTimerInterval / 2;
  41. gameView.hole7.flipSpeed = gameArea.gameTimerInterval / 2;
  42. gameView.hole8.flipSpeed = gameArea.gameTimerInterval / 2;
  43. gameTimer.restart();
  44. score = 0;
  45. lives = 3;
  46. gameView.scoreText = score;
  47. gameView.livesText = lives;
  48. gameView.gamesPlayed = gameView.gamesPlayed + 1;
  49. }
  50. var blocks = []
  51. function getSize() {
  52. var size = 0;
  53. blocks = [];
  54. if (gameView.hole1.state != "hasMonkey") {
  55. size++;
  56. blocks.push(1);
  57. }
  58. if (gameView.hole2.state != "hasMonkey"){
  59. size++;
  60. blocks.push(2);
  61. }
  62. if (gameView.hole3.state != "hasMonkey"){
  63. size++;
  64. blocks.push(3);
  65. }
  66. if (gameView.hole4.state != "hasMonkey"){
  67. size++;
  68. blocks.push(4);
  69. }
  70. if (gameView.hole5.state != "hasMonkey") {
  71. size++;
  72. blocks.push(5);
  73. }
  74. if (gameView.hole6.state != "hasMonkey") {
  75. size++;
  76. blocks.push(6);
  77. }
  78. if (gameView.hole7.state != "hasMonkey") {
  79. size++;
  80. blocks.push(7);
  81. }
  82. if (gameView.hole8.state != "hasMonkey") {
  83. size++;
  84. blocks.push(8);
  85. }
  86. return size;
  87. }
  88. function gameLoop(loop)
  89. {
  90. var size = getSize();
  91. if (size == 0) {
  92. gameTimer.stop();
  93. endGame();
  94. gameTimer.stop();
  95. }
  96. else {
  97. var randomnumber = Math.floor(Math.random() * size);
  98. var randomnumber2 = Math.floor(Math.random() * 101);
  99. var block = blocks[randomnumber];
  100. if (randomnumber2 < 50) {
  101. setMonkey(block, "holebg1");
  102. }
  103. else if (randomnumber2 >= 50 && randomnumber2 < 80) {
  104. setMonkey(block, "holebg2");
  105. }
  106. else if (randomnumber2 >= 80) {
  107. setMonkey(block, "holebg3");
  108. }
  109. if (randomnumber2 % 7 === 0 && loop) {
  110. gameLoop(false);
  111. }
  112. }
  113. }
  114. function setMonkey(id, image) {
  115. if (id == 1) {
  116. gameView.hole1.state = "hasMonkey";
  117. gameView.hole1.hasMonkey = true;
  118. gameView.hole1.image = image;
  119. hideTimer1.restart();
  120. }
  121. else if (id == 2) {
  122. gameView.hole2.state = "hasMonkey";
  123. gameView.hole2.hasMonkey = true;
  124. gameView.hole2.image = image;
  125. hideTimer2.restart();
  126. }
  127. else if (id == 3) {
  128. gameView.hole3.state = "hasMonkey";
  129. gameView.hole3.hasMonkey = true;
  130. gameView.hole3.image = image;
  131. hideTimer3.restart();
  132. }
  133. else if (id == 4) {
  134. gameView.hole4.state = "hasMonkey";
  135. gameView.hole4.hasMonkey = true;
  136. gameView.hole4.image = image;
  137. hideTimer4.restart();
  138. }
  139. else if (id == 5) {
  140. gameView.hole5.state = "hasMonkey";
  141. gameView.hole5.hasMonkey = true;
  142. gameView.hole5.image = image;
  143. hideTimer5.restart();
  144. }
  145. else if (id == 6) {
  146. gameView.hole6.state = "hasMonkey";
  147. gameView.hole6.hasMonkey = true;
  148. gameView.hole6.image = image;
  149. hideTimer6.restart();
  150. }
  151. else if (id == 7) {
  152. gameView.hole7.state = "hasMonkey";
  153. gameView.hole7.hasMonkey = true;
  154. gameView.hole7.image = image;
  155. hideTimer7.restart();
  156. }
  157. else if (id == 8) {
  158. gameView.hole8.state = "hasMonkey";
  159. gameView.hole8.hasMonkey = true;
  160. gameView.hole8.image = image;
  161. hideTimer8.restart();
  162. }
  163. }
  164. function hideMonkey(id) {
  165. if (id == 1 && gameView.hole1.hasMonkey) {
  166. gameView.hole1.state = "";
  167. gameView.hole1.image2 = "holebgred";
  168. gameView.hole1.hasMonkey = false;
  169. hideTimer1.stop();
  170. gameArea.lives--;
  171. }
  172. else if (id == 2 && gameView.hole2.hasMonkey) {
  173. gameView.hole2.state = "";
  174. gameView.hole2.image2 = "holebgred";
  175. gameView.hole2.hasMonkey = false;
  176. hideTimer2.stop();
  177. gameArea.lives--;
  178. }
  179. else if (id == 3 && gameView.hole3.hasMonkey) {
  180. gameView.hole3.state = "";
  181. gameView.hole3.image2 = "holebgred";
  182. gameView.hole3.hasMonkey = false;
  183. hideTimer3.stop();
  184. gameArea.lives--;
  185. }
  186. else if (id == 4 && gameView.hole4.hasMonkey) {
  187. gameView.hole4.state = "";
  188. gameView.hole4.image2 = "holebgred";
  189. gameView.hole4.hasMonkey = false;
  190. hideTimer4.stop();
  191. gameArea.lives--;
  192. }
  193. else if (id == 5 && gameView.hole5.hasMonkey) {
  194. gameView.hole5.state = "";
  195. gameView.hole5.image2 = "holebgred";
  196. gameView.hole5.hasMonkey = false;
  197. hideTimer5.stop();
  198. gameArea.lives--;
  199. }
  200. else if (id == 6 && gameView.hole6.hasMonkey) {
  201. gameView.hole6.state = "";
  202. gameView.hole6.image2 = "holebgred";
  203. gameView.hole6.hasMonkey = false;
  204. hideTimer6.stop();
  205. gameArea.lives--;
  206. }
  207. else if (id == 7 && gameView.hole7.hasMonkey) {
  208. gameView.hole7.state = "";
  209. gameView.hole7.image2 = "holebgred";
  210. gameView.hole7.hasMonkey = false;
  211. hideTimer7.stop();
  212. gameArea.lives--;
  213. }
  214. else if (id == 8 && gameView.hole8.hasMonkey) {
  215. gameView.hole8.state = "";
  216. gameView.hole8.image2 = "holebgred";
  217. gameView.hole8.hasMonkey = false;
  218. hideTimer8.stop();
  219. gameArea.lives--;
  220. }
  221. gameView.livesText = gameArea.lives;
  222. if (gameArea.lives == 0) {
  223. endGame();
  224. gameTimer.stop();
  225. hideTimer1.stop();
  226. hideTimer2.stop();
  227. hideTimer3.stop();
  228. hideTimer4.stop();
  229. hideTimer5.stop();
  230. hideTimer6.stop();
  231. hideTimer7.stop();
  232. hideTimer8.stop();
  233. }
  234. }
  235. function addScore(score,id) {
  236. if (id == 1) {
  237. hideTimer1.stop();
  238. gameView.hole1.hasMonkey = false;
  239. gameView.hole1.state = "";
  240. gameView.hole1.image2 = "hole";
  241. }
  242. else if (id == 2) {
  243. hideTimer2.stop();
  244. gameView.hole2.hasMonkey = false;
  245. gameView.hole2.state = "";
  246. gameView.hole2.image2 = "hole";
  247. }
  248. else if (id == 3) {
  249. hideTimer3.stop();
  250. gameView.hole3.hasMonkey = false;
  251. gameView.hole3.state = "";
  252. gameView.hole3.image2 = "hole";
  253. }
  254. else if (id == 4) {
  255. hideTimer4.stop();
  256. gameView.hole4.hasMonkey = false;
  257. gameView.hole4.state = "";
  258. gameView.hole4.image2 = "hole";
  259. }
  260. else if (id == 5) {
  261. hideTimer5.stop();
  262. gameView.hole5.hasMonkey = false;
  263. gameView.hole5.state = "";
  264. gameView.hole5.image2 = "hole";
  265. }
  266. else if (id == 6) {
  267. hideTimer6.stop();
  268. gameView.hole6.hasMonkey = false;
  269. gameView.hole6.state = "";
  270. gameView.hole6.image2 = "hole";
  271. }
  272. else if (id == 7) {
  273. hideTimer7.stop();
  274. gameView.hole7.hasMonkey = false;
  275. gameView.hole7.state = "";
  276. gameView.hole7.image2 = "hole";
  277. }
  278. else if (id == 8) {
  279. hideTimer8.stop();
  280. gameView.hole8.hasMonkey = false;
  281. gameView.hole8.state = "";
  282. gameView.hole8.image2 = "hole";
  283. }
  284. if (gameView.state != "")
  285. return;
  286. gameArea.score += score;
  287. gameView.scoreText = gameArea.score;
  288. clickCount++;
  289. if (gameArea.gameTimerInterval > 250 && clickCount % 7 == 0) {
  290. gameArea.gameTimerInterval = gameArea.gameTimerInterval -50;
  291. gameTimer.interval = gameArea.gameTimerInterval;
  292. gameView.hole1.flipSpeed = gameArea.gameTimerInterval / 2;
  293. gameView.hole2.flipSpeed = gameArea.gameTimerInterval / 2;
  294. gameView.hole3.flipSpeed = gameArea.gameTimerInterval / 2;
  295. gameView.hole4.flipSpeed = gameArea.gameTimerInterval / 2;
  296. gameView.hole5.flipSpeed = gameArea.gameTimerInterval / 2;
  297. gameView.hole6.flipSpeed = gameArea.gameTimerInterval / 2;
  298. gameView.hole7.flipSpeed = gameArea.gameTimerInterval / 2;
  299. gameView.hole8.flipSpeed = gameArea.gameTimerInterval / 2;
  300. }
  301. }
  302. function saveHighScore() {
  303. //if (scoresURL != "")
  304. //sendHighScore(name);
  305. var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);
  306. var dataStr = "INSERT INTO Scores VALUES(?, ?, ?)";
  307. var data = ["", score, 8];
  308. db.transaction(function(tx) {
  309. tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');
  310. tx.executeSql(dataStr, data);
  311. });
  312. //getScores();
  313. //setState("highscore");
  314. }
  315. function endGame() {
  316. if (score == 0) {
  317. gameView.state = "gameOver1";
  318. return;
  319. }
  320. var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);
  321. db.transaction(function(tx) {
  322. tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');
  323. var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = 8 AND score >= ' + score + ' LIMIT 10');
  324. if (rs.rows.length == 10) {
  325. gameView.state = "gameOver1";
  326. }
  327. else {
  328. gameView.state = "gameOver";
  329. }
  330. });
  331. }
  332. function getScores() {
  333. var db = openDatabaseSync("HitTheMonkeyScores", "1.0", "Local HitTheMonkey High Scores", 100);
  334. db.transaction(function(tx) {
  335. tx.executeSql('CREATE TABLE IF NOT EXISTS Scores(name TEXT, score NUMBER, gridSize NUMBER)');
  336. var rs = tx.executeSql('SELECT * FROM Scores WHERE gridSize = 8 ORDER BY score desc LIMIT 10');
  337. var r = ""
  338. for (var i = 0; i < rs.rows.length; i++) {
  339. r += (i + 1) + ". " + rs.rows.item(i).score + ' points.\n';
  340. }
  341. highScoreView.highscores = r;
  342. });
  343. }