Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1.  
  2. require(
  3. // Use this raphael.lonce library to "fix" some annoying things about Raphel paper and graphical elements:
  4. // a) paper.put(relement) - to put an Element created by paper back on a paper after it has been removed
  5. // b) call element.addEventListener(...) instead of element.node.addEventListner(...)
  6. ["../jslibs/raphael.lonce"], // include a custom-built library
  7.  
  8. function (tutSndFactory, raphHelper) {
  9. alert("Click the green disk as many times as you can in 10 seconds.")
  10.  
  11. // function myFunction() {
  12. // var txt;
  13. // var person = prompt("Please enter your name:", "Harry Potter");
  14. // if (person == null || person == "") {
  15. // txt = "User cancelled the prompt.";
  16. // } else {
  17. // txt = "Hello " + person + "! How are you today?";
  18. // }
  19. // // document.getElementById("demo").innerHTML = txt;
  20. // //
  21. // };
  22.  
  23. // myFunction();
  24.  
  25. var level=document.getElementById("levelSlider");
  26. level.addEventListener("input", function (ev) {
  27. if (level.value == 1) {
  28.  
  29. }
  30.  
  31. });
  32.  
  33.  
  34.  
  35. console.log("Yo, I am alive!");
  36. var background = document.getElementById("background");
  37.  
  38. // Grab the div where we will put our Raphael paper
  39. var centerDiv = document.getElementById("centerDiv");
  40.  
  41. // Create the Raphael paper that we will use for drawing and creating graphical objects
  42. var paper = new Raphael(centerDiv);
  43.  
  44. // put the width and heigth of the canvas into variables for our own convenience
  45. var pWidth = paper.width;
  46. var pHeight = paper.height;
  47. console.log("pWidth is " + pWidth + ", and pHeight is " + pHeight);
  48.  
  49. // Just create a nice black background
  50. var bgRect = paper.rect(0,0,pWidth, pHeight);
  51. // bgRect.attr({"fill": "black"});
  52.  
  53. // var p = Raphael("paper",800,800);
  54.  
  55. // var img = p.image("resources/backgroundimage.jpg", 10,10,300,300).attr({"clip-rect":"20,40,300,300"});
  56.  
  57. // A dot for us to play with
  58. var dot = paper.circle(pWidth/2, pHeight/2, 20);
  59. dot.attr({"fill": "green"});
  60.  
  61.  
  62. //-------------------
  63.  
  64. //HTML5 audio element
  65.  
  66.  
  67. //-------------------
  68.  
  69. // Add some properties to dot just to keep track of it's "state"
  70. dot.xpos=pWidth/2;
  71. dot.ypos=pHeight/2;
  72. dot.xrate=5;
  73. dot.yrate=5;
  74.  
  75. // create an array to hold 4 sounds for each wall.
  76. var soundTop = new Audio("resources/446100__justinvoke__bounce.wav");
  77. var soundBottom = new Audio("resources/383240__josepharaoh99__bounce.wav");
  78. var soundLeft = new Audio("resources/456563__bumpelsnake__bounce1.wav");
  79. var soundRight = new Audio("resources/361230__someguy22__8-bit-bounce.wav");
  80.  
  81. // our drawing routine, will use as a callback for the interval timer
  82. var draw = function(){
  83.  
  84. // Update the position where we want our dot to be
  85. dot.xpos += dot.xrate;
  86. dot.ypos += dot.yrate;
  87.  
  88. // Now actually move the dot using our 'state' variables
  89. dot.attr({'cx': dot.xpos, 'cy': dot.ypos});
  90.  
  91. //---------------------------------------------
  92. // Set sound parameters based on the position of the moving dots
  93.  
  94. // When dots hit any of the 4 walls, reverse its direction
  95. if (dot.xpos > pWidth) {
  96. dot.xrate = -dot.xrate;
  97. soundTop.play();
  98. }
  99. if (dot.ypos > pHeight) {
  100. dot.yrate = - dot.yrate;
  101. soundBottom.play();
  102. };
  103. if (dot.xpos < 0) {
  104. dot.xrate = -dot.xrate;
  105. soundLeft.play();
  106.  
  107. }
  108. if (dot.ypos < 0) {
  109. dot.yrate = - dot.yrate;
  110. soundRight.play();
  111. };
  112.  
  113. if (startTime == 10000) { // after 10 seconds have passed, stop the game and call function endGame
  114. endGame();
  115. }
  116. };
  117.  
  118.  
  119.  
  120. // call draw() periodically
  121. // Start the timer with a button (instead of as program loads) so that sound models have time to load before we try play or set their parameters in the draw() function.
  122. var toggle="off";
  123. // var timer;
  124. document.getElementById("startButtonID").addEventListener('click', function(ev){
  125. if (toggle=="off"){
  126. setInterval(draw, 60);
  127.  
  128. toggle="on";
  129.  
  130. setTimeout(function(){ // when i press button stop counting the time
  131. if(toggle=="on")
  132. alert("Your count = " + numClicks);
  133. endGame();
  134. }, 10000);
  135.  
  136. } else {
  137. toggle="off"
  138. endGame();
  139.  
  140. }
  141. });
  142.  
  143. var startTime;
  144. var numClicks = 0;
  145. var gamePlay;
  146.  
  147. var welcomeSound = new Audio("resources/336472__djfroyd__groovy-techno-loop.wav");
  148.  
  149. var toggleButton = document.getElementById("startButtonID")
  150.  
  151. toggleButton.addEventListener("click", function playSound() {
  152. welcomeSound.play();
  153. numClicks++;
  154.  
  155. if (numClicks % 2 == 1) { // if odd number click, want to start ball move and start music
  156. soundTop.play();
  157. startTime = new Date();
  158.  
  159. numClicks = 0;
  160. };
  161.  
  162. });
  163.  
  164.  
  165. // function endGame ()
  166. // if (numClicks % 2 == 0) { // if even number click, want to restart ball to center and pause music
  167. // dot.attr({'cx': dot.xpos, 'cy': dot.ypos});
  168. // }
  169.  
  170. // create a sound loop
  171. welcomeSound.loop=true
  172.  
  173. dot.addEventListener("click", function (){
  174. soundTop.play();
  175. numClicks++;
  176. });
  177.  
  178. function endGame(){ // move dot back to the center
  179. clearInterval(timer);
  180. welcomeSound.pause();
  181. dot.attr({
  182. "cx": pWidth/2,
  183. "cy": pHeight/2
  184.  
  185. });
  186. };
  187. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement