Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. var interval;
  2. var intervalanima;
  3. var inc=0;
  4. var doh=new Audio;
  5. doh.src="audio/doh.mp3";
  6. var lado="right";
  7. var segundos=0;
  8. var jumping = false;
  9. var contador=0;
  10.  
  11. window.onload=startAnimation;
  12. document.onkeypress = function (event) {
  13. processaTecla(event);
  14. };
  15.  
  16. function startAnimation() {
  17. for (var i=0; i<=2; i++){
  18. document.getElementById("buraco"+i).style.position="absolute";
  19. document.getElementById("buraco"+i).style.top="195px";
  20. document.getElementById("buraco"+i).style.left="-250px";
  21. }
  22. document.getElementById("buraco0").style.left=Math.floor(Math.random()*751+100)+"px";
  23. document.getElementById("homer").style.left="0px";
  24. document.getElementById("homer").style.top="135px";
  25. interval=setInterval(homerAnimation,100)
  26. }
  27.  
  28. function homerAnimation() {
  29. contador++;
  30. if(contador==10){
  31. segundos++;
  32. contador=0;
  33. }
  34. if(lado=="right") {
  35. document.getElementById("homer").style.left = parseInt(document.getElementById("homer").style.left) + 5 + "px";
  36. inc++;
  37. document.getElementById("homer").src = "imagens/homer_mov/" + lado + "/homer_" + inc + ".png";
  38. if (inc == 8) {
  39. inc = 0;
  40. }
  41. }
  42.  
  43. if(lado=="left") {
  44. document.getElementById("homer").style.left = parseInt(document.getElementById("homer").style.left) - 5 + "px";
  45. inc++;
  46. document.getElementById("homer").src = "imagens/homer_mov/" + lado + "/homer_" + inc + ".png";
  47. if (inc == 8) {
  48. inc = 0;
  49. }
  50. }
  51.  
  52. if(parseInt(document.getElementById("homer").style.left)+41>=1000) {
  53. lado="left";
  54. }
  55. detetaColisao();
  56. }
  57.  
  58. function detetaColisao() {
  59. for (var i=0; i<=2; i++){
  60. if (parseInt(document.getElementById("homer").style.left)+35>=parseInt(document.getElementById("buraco"+i).style.left) &&
  61. parseInt(document.getElementById("homer").style.left)+35<=parseInt(document.getElementById("buraco"+i).style.left)+76
  62. ) {
  63. clearInterval(interval);
  64. doh.play();
  65. alert("Durou "+segundos+" segundos")
  66. }
  67. }
  68.  
  69.  
  70.  
  71.  
  72. }
  73.  
  74. function processaTecla(evt) {
  75. switch (evt.key){
  76. case " ":
  77. if (!jumping){
  78. jumping=true;
  79. salta();
  80. }
  81. }
  82.  
  83. }
  84.  
  85. function salta() {
  86. var homerV = parseInt(document.getElementById("homer").style.top);
  87. var height = 0;
  88. var up = true;
  89. var salto = setInterval(function () {
  90. if (height == 65)
  91. up = false;
  92. if (up)
  93. height += 5;
  94. else {
  95. height -= 5;
  96. if (height == 0) {
  97. clearInterval(salto);
  98. up = true;
  99. jumping = false;}
  100.  
  101. }
  102. document.getElementById("homer").style.top = homerV - height + "px";
  103.  
  104.  
  105.  
  106. }, 100)
  107.  
  108.  
  109.  
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement