Ein Projekt das es ermöglicht Beerpong über das Internet von zwei unabhängigen positionen aus zu spielen. Entstehung im Rahmen einer Praktikumsaufgabe im Fach Interaktion.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.js 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. var html = document.getElementById("html");
  2. var reshoot = document.getElementById("reshoot");
  3. var ball = document.getElementById("ball");
  4. var root = document.documentElement;
  5. var restartbuttonvisible = 1;
  6. var cupsOut = [];
  7. //function shootY(){
  8. //let top = window.getComputedStyle(ball).getPropertyValue("top");
  9. //ball.classList.remove("shootY");
  10. //ball.classList.add("shootX");
  11. //ball.style.top = top;
  12. //let onclick = "shootX('".concat(top.toString(), "')");
  13. //html.setAttribute("onclick", onclick);
  14. //}
  15. function shoot(event){
  16. //let top = valueY;
  17. //let topInt = parseInt(valueY);
  18. //let left = window.getComputedStyle(ball).getPropertyValue("left");
  19. //let leftInt = parseInt(left);
  20. //let newtop = topInt-325;
  21. //root.style.setProperty('--top', (topInt)+ "px");
  22. //root.style.setProperty('--top325', (newtop)+ "px");
  23. ball.classList.add("Shoot");
  24. switch (event.key)
  25. {
  26. case 'e':
  27. case 'E':
  28. root.style.setProperty('--top325', "-350"+ "px");
  29. ball.style.top = "-350px"
  30. ball.style.left = "-155px";
  31. removeCup("1");
  32. break;
  33. case 'r':
  34. case 'R':
  35. root.style.setProperty('--top325', "-350"+ "px");
  36. ball.style.top = "-350px"
  37. ball.style.left = "-50px";
  38. removeCup("2");
  39. break;
  40. case 't':
  41. case 'T':
  42. root.style.setProperty('--top325', "-350"+ "px");
  43. ball.style.top = "-350px"
  44. ball.style.left = "55px";
  45. removeCup("3");
  46. break;
  47. case 'z':
  48. case 'Z':
  49. root.style.setProperty('--top325', "-350"+ "px");
  50. ball.style.top = "-350px"
  51. ball.style.left = "160px";
  52. removeCup("4");
  53. break;
  54. case 'd':
  55. case 'D':
  56. root.style.setProperty('--top325', "-290"+ "px");
  57. ball.style.top = "-290px"
  58. ball.style.left = "-100px";
  59. removeCup("5");
  60. break;
  61. case 'f':
  62. case 'F':
  63. root.style.setProperty('--top325', "-290"+ "px");
  64. ball.style.top = "-290px"
  65. ball.style.left = "0px";
  66. removeCup("6");
  67. break;
  68. case 'g':
  69. case 'G':
  70. root.style.setProperty('--top325', "-290"+ "px");
  71. ball.style.top = "-290px"
  72. ball.style.left = "110px";
  73. removeCup("7");
  74. break;
  75. case 'c':
  76. case 'C':
  77. root.style.setProperty('--top325', "-220"+ "px");
  78. ball.style.top = "-220px"
  79. ball.style.left = "-50px";
  80. removeCup("8");
  81. break;
  82. case 'v':
  83. case 'V':
  84. root.style.setProperty('--top325', "-220"+ "px");
  85. ball.style.top = "-220px"
  86. ball.style.left = "55px";
  87. removeCup("9");
  88. break;
  89. case 'b':
  90. case 'B':
  91. root.style.setProperty('--top325', "-160"+ "px");
  92. ball.style.top = "-160px"
  93. ball.style.left = "0px";
  94. removeCup("10");
  95. break;
  96. default:
  97. break;
  98. }
  99. setTimeout(function(){
  100. if(cupsOut.length == 10 && restartbuttonvisible == 1){
  101. let restartBtn = document.createElement("BUTTON");
  102. restartBtn.innerHTML = "Restart";
  103. restartBtn.setAttribute("onclick","location.reload()");
  104. restartBtn.setAttribute("id","restart");
  105. document.body.appendChild(restartBtn);
  106. restartbuttonvisible = 2;
  107. ball.style.top = "0px";
  108. ball.style.left = "0px";
  109. }else{
  110. reshot();
  111. //let reshootBtn = document.createElement("BUTTON");
  112. //reshootBtn.innerHTML = "Reshoot";
  113. //reshootBtn.setAttribute("onclick","reshot()");
  114. //reshootBtn.setAttribute("id","reshoot");
  115. //document.body.appendChild(reshootBtn);
  116. }
  117. },1000);
  118. }
  119. //Cup entfernen bei Treffer
  120. function removeCup(cup){
  121. let element = "cup".concat(cup);
  122. let alreadyExists = cupsOut.includes(cup);
  123. if(alreadyExists==false){
  124. cupsOut.push(cup);
  125. }
  126. setTimeout(function(){
  127. document.getElementById(element).classList.add("fadeAway");
  128. },1000);
  129. }
  130. function reshot(){
  131. //document.getElementById("reshoot").remove();
  132. ball.classList.remove("Shoot");
  133. ball.style.top = "0px";
  134. ball.style.left = "0px";
  135. //root.style.setProperty('--top325', "0"+ "px");
  136. //setTimeout(function(){
  137. // html.setAttribute("onclick", "shoot()");
  138. //},1000);
  139. }
  140. // Cup Positionen + Toleranz
  141. //Cup 1: -20<topInt && topInt<20 && -125<leftInt && leftInt<-60
  142. //Cup 2: -20<topInt && topInt<20 && -40<leftInt && leftInt<40
  143. //Cup 3: -20<topInt && topInt<20 && 60<leftInt && leftInt<125
  144. //Cup 4: 60<topInt && topInt<100 && -90<leftInt && leftInt<-25
  145. //Cup 5: 60<topInt && topInt<100 && 15<leftInt && leftInt<80
  146. //Cup 6: 140<topInt && topInt<180 && -45<leftInt && leftInt<45