Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. <!-- question 4 -->
  2. var fish;
  3. var imageSchedule;
  4.  
  5. function startAnimation(){
  6. var fishImage = document.getElementById("fish");
  7. fishImage.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";
  8. imageSchedule = setInterval(showimage, 1000);
  9. }
  10.  
  11. function showimage(){
  12.  
  13. var fishImage = document.getElementById("fish");
  14.  
  15. if (fishImage.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png") {
  16. fishImage.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png";
  17.  
  18. }
  19.  
  20. else if (fishImage.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png") {
  21. fishImage.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";
  22. }
  23.  
  24. }
  25.  
  26. function stopAnimation(){
  27. clearInterval(imageSchedule);
  28.  
  29. }
  30.  
  31. <!-- question 5 -->
  32. var image;
  33. var imageSchedule;
  34. var image2 = 0;
  35. var userScore = 0 ;
  36.  
  37. function startGame(){
  38. var image2 = document.getElementById("image1");
  39. image2.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";
  40. imageSchedule = setInterval(showFish, 1000);
  41. }
  42.  
  43.  
  44.  
  45. function showFish(){
  46.  
  47. var image2 = document.getElementById("image1");
  48.  
  49. if (image2.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png") {
  50. image2.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png";
  51.  
  52. }
  53.  
  54. else if (image2.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png") {
  55. image2.src = "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png";
  56. }
  57.  
  58. }
  59.  
  60. function stopGame(){
  61. clearInterval(imageSchedule);
  62.  
  63. }
  64.  
  65. function score(){
  66. var image = document.getElementById("image1");
  67.  
  68. if (image.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/happy_fish.png") {
  69. userScore += 5;
  70. }
  71. else if (image.src == "https://www.uow.edu.au/~dong/w3/assignment/a5/sad_fish.png"){
  72. userScore -= 5;
  73. }
  74.  
  75. var span = document.getElementById("image_1");
  76. span.innerHTML = userScore;
  77.  
  78. if (userScore == 10){
  79. span.innerHTML = userScore + " you win" ;
  80.  
  81. }
  82. else if (userScore == - 10){
  83. span.innerHTML = userScore + " you lose" ;
  84.  
  85. }
  86.  
  87. }
  88.  
  89. <button onclick="startAnimation()" > Start Animation </button>
  90.  
  91. <button onclick="stopAnimation()" > Stop Animation </button>
  92.  
  93. <br>
  94. <br>
  95.  
  96. <img id="fish">
  97.  
  98. <hr>
  99.  
  100. <button onClick="startGame()"> Start Animation </button>
  101.  
  102. <button onClick="stopGame()"> Stop Animation </button>
  103.  
  104. <br>
  105. <br>
  106. <br>
  107.  
  108. <img id="image1" onclick="score()">
  109.  
  110. <br>
  111.  
  112. Your score: <span id="image_1" ></span>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement