Guest User

Untitled

a guest
Jan 22nd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. let klick = 0;
  2.  
  3. display = document.querySelector('#time');
  4.  
  5. $("#start").click(function() { //clickfunktion beim starten.
  6. $("#start").fadeToggle(); //Der Startbutton geht weg
  7. $("#welcome").fadeToggle(); // Das Willkommensschild geht weg
  8. $("#zeitauswahl").fadeToggle(); //Die Auswahl der Sekunden verschwindet
  9.  
  10. $("#time").fadeToggle(900); //Timer kommt
  11. $("#clicker").animate({
  12. height: 'toggle' //inverse aktion...keine height --> klappt auf // aufgeklappt ---> macht height weg
  13. }); //clicker wird gestartet
  14. var d = $("#zeitauswahl option:selected").val(); //referenziert auf zeitauswahl und option
  15.  
  16. var dauer = parseInt(d); //verwandelt Dauer in einen Int
  17.  
  18. startTimer(dauer); //übergibt die variable dauer, und die Funktion wird gestartet.
  19. })
  20.  
  21. function startTimer(dauer) {
  22. let timer = parseInt(dauer);
  23. runTimer();
  24. zeit = setInterval(runTimer, 1000); //zahl gibt an, wie oft die Function pro zeit wiederholt wird. Hier eine Sekunde (1000Millisekunden)
  25. function runTimer(){
  26. display.textContent = parseInt(timer); // zeigt sekunden-variable
  27.  
  28. --timer; //setzt den timer immer einen herab
  29.  
  30. if (timer < 0.00) {
  31.  
  32. timer = 5;
  33. console.log(timer); //debug info
  34. $("#start").fadeToggle();
  35. $("#welcome").fadeToggle();
  36. $("#zeitauswahl").fadeToggle();
  37. $("#time").fadeToggle();
  38. $("#clicker").fadeToggle();
  39. $("#clicker").css("margin-top", "10%");
  40. $("#clicker").css("margin-left", "50%");
  41.  
  42. alert("Sauber du hast " + klick + " klicks in 5 Sekunden geschafft!");
  43. alert('High Score ist ' + highScore(klick)); // zeigt den HighScore
  44.  
  45. klick = 0
  46. console.log(timer);
  47.  
  48. clearInterval(zeit);
  49.  
  50. } //wenn timer auf 0 ist, wird alles wieder angezeigt und die Interval-Function beendet
  51.  
  52. }
  53.  
  54.  
  55. };
  56.  
  57. $("#clicker").click(function() {
  58. let zufall = Math.floor(Math.random() * 400) -200 //setzt eine zufällige höhe für den clicker
  59. let zufal = Math.floor(Math.random() * 450) //Zufällige Variable für den Linkswert
  60.  
  61. klick = klick + 1 //setzt den zähler beim klicken eins hoch
  62. if (klick % 2 == 0) {
  63. $("#clicker").animate({
  64. opacity: '0.3', //macht den klicker leicht durchsichtig
  65. left: zufall + "px",
  66. top: zufal + "px"
  67. }, "fast"); //bewegt den Klick-Block auf eine zufällige Stelle
  68. } else {
  69. $("#clicker").animate({
  70. opacity: '1.0', //macht den Klicker sichtbar
  71. left: zufall + "px",
  72. top: zufal + "px"
  73. }, "fast")
  74.  
  75. }
  76.  
  77.  
  78.  
  79. });
  80.  
  81. var fireworks = {
  82.  
  83. ///////////////////////////// configuration ////////////////////////////
  84.  
  85. // random colors
  86. _color: ['#D0D0D0', '#FF0000', '#FFFF00', '#22FF00', '#2040FF', '#00CCFF', '#FF00FF', '#A319D6'],
  87. // gravity factor
  88. _gravity: 0.07,
  89. // air resistance factor
  90. _resistance: 0.975,
  91. // zIndex of particles
  92. _zIndex: 20000,
  93. // maximal age of particles (in msec)
  94. _maxAge: 2000,
  95. // interval of appearing explosions (in msec)
  96. _interval: [500, 2500],
  97. // amount of particles per explosion
  98. _particlesPerExplosion: 40,
  99. // maximal speed of particle at moment of explosion
  100. _speed: 5,
  101. // minimal/maximal size of particle
  102. _minSize: 8,
  103. _maxSize: 12,
  104.  
  105.  
  106.  
  107. ///////////////////////////// private vars /////////////////////////////
  108.  
  109. _particles: [],
  110. _bodyWidth: 0,
  111. _bodyHeight: 0,
  112. _count: 0,
  113. _lastInterval: 0,
  114.  
  115.  
  116.  
  117. ////////////////////////////// functions ///////////////////////////////
  118.  
  119. // init fireworks
  120. init: function()
  121. {
  122. this._addEventListener(window, 'resize', function() { return fireworks.resize.apply(fireworks); });
  123. this._addEventListener(window, 'load', function() { return fireworks.start.apply(fireworks); });
  124. },
  125.  
  126.  
  127. // add an event listener
  128. _addEventListener: function(el, name, handler)
  129. {
  130. if (el.addEventListener)
  131. el.addEventListener(name, handler, false);
  132. else if (el.attachEvent)
  133. el.attachEvent('on' + name, handler);
  134. },
  135.  
  136.  
  137. // start fireworks
  138. start: function()
  139. {
  140. // init window size
  141. this.resize();
  142.  
  143. // start to move particles
  144. this._animFn = function() {fireworks._move();};
  145. this._lastInterval = this._time();
  146. requestAnimFrame(this._animFn);
  147.  
  148. this._addExplosion();
  149. },
  150.  
  151.  
  152. // get current time
  153. _time: function()
  154. {
  155. return +new Date();
  156. },
  157.  
  158.  
  159. // return a random integer
  160. _random: function(value)
  161. {
  162. return Math.random() * value;
  163. },
  164.  
  165.  
  166. // return a random array element
  167. _randomArray: function(arr)
  168. {
  169. return arr[
  170. Math.floor(
  171. Math.random() * (arr.length)
  172. )
  173. ];
  174. },
  175.  
  176.  
  177. // add a new explosion
  178. _addExplosion: function()
  179. {
  180. var x = Math.floor(this._random(this._bodyWidth)),
  181. y = Math.floor((this._random(.5) + .1) * this._bodyHeight),
  182. dx = this._random(10) - 5,
  183. dy = this._random(-2) - 1,
  184. c1 = this._randomArray(this._color),
  185. c2 = this._randomArray(this._color);
  186.  
  187. for (var i = 0; i < this._particlesPerExplosion; i++)
  188. {
  189. this._createParticle(
  190. x,
  191. y,
  192. dx,
  193. dy,
  194. i / (this._particlesPerExplosion - 1) * 180 * Math.PI,
  195. this._random(this._speed),
  196. this._random(1) > .5 ? c1 : c2
  197. );
  198. }
  199.  
  200. window.setTimeout(
  201. function() { return fireworks._addExplosion.apply(fireworks);},
  202. this._random(this._interval[1] - this._interval[0]) + this._interval[0]
  203. );
  204. },
  205.  
  206.  
  207. // creates a new particle
  208. _createParticle: function(x, y, dx, dy, rot, speed, color)
  209. {
  210. var el = null,
  211. foundEl = false,
  212. p = this._particles;
  213.  
  214. // look for old particle
  215. for (var i = 0, l = p.length; i < l; i++)
  216. if (p[i].data.age > 1)
  217. {
  218. el = p[i];
  219. foundEl = true;
  220. break;
  221. }
  222.  
  223. // create span child for particles
  224. if (!foundEl)
  225. {
  226. el = document.createElement('div');
  227. el.className = 'particle';
  228. el.style.position = 'absolute';
  229. el.style.fontSize = this._maxSize + 'px';
  230. el.style.zIndex = this._zIndex;
  231. el.style.width = this._maxSize + 'px';
  232. el.style.textAlign = 'center';
  233. el.style.overflow = 'hidden';
  234. el.innerHTML = '&#x25cf;';
  235. }
  236.  
  237. el.style.left = x + 'px';
  238. el.style.top = y + 'px';
  239. el.style.color = color;
  240. el.data = {
  241. x: x,
  242. y: y,
  243. dx: Math.cos(rot) * speed + dx,
  244. dy: Math.sin(rot) * speed + dy,
  245. color: color,
  246. age: Math.random() * .25
  247. };
  248.  
  249. if (!foundEl)
  250. {
  251. document.getElementsByTagName('body')[0].appendChild(el);
  252. this._particles.push(el);
  253. }
  254. },
  255.  
  256.  
  257. // move existing particles
  258. _move: function()
  259. {
  260. requestAnimFrame(this._animFn);
  261.  
  262. // calculate movement factor
  263. var dif = this._time() - this._lastInterval;
  264. this._lastInterval = this._time();
  265.  
  266. var delta = dif / 20,
  267. el,
  268. d,
  269. p = this._particles,
  270. r = Math.pow(this._resistance, delta),
  271. g = this._gravity * delta,
  272. a = dif / this._maxAge;
  273.  
  274. for (var i = 0, l = p.length; i < l; i++)
  275. {
  276. el = p[i];
  277. d = el.data;
  278.  
  279. if (d.age > 1)
  280. continue;
  281.  
  282. d.age += a;
  283. d.dy += g;
  284. d.dx *= r;
  285. d.dy *= r;
  286. d.x += d.dx * delta;
  287. d.y += d.dy * delta;
  288.  
  289. if (d.x < 0)
  290. {
  291. d.dx *= -1;
  292. d.x = 0;
  293. }
  294. else if (d.x > this._bodyWidth)
  295. {
  296. d.dx *= -1;
  297. d.x = this._bodyWidth;
  298. }
  299. if (d.y < 0)
  300. {
  301. d.dy *= -1;
  302. d.y = 0;
  303. }
  304. else if (d.y > this._bodyHeight)
  305. {
  306. d.dy *= -1;
  307. d.y = this._bodyHeight;
  308. }
  309.  
  310. if (d.age > 1)
  311. d.x = d.y = 0;
  312.  
  313. el.style.left = d.x + 'px';
  314. el.style.top = d.y + 'px';
  315. el.style.color = (Math.random() * .5 + d.age >= 1) ? 'transparent' : d.color;
  316. el.style.fontSize = Math.max(this._minSize, (1 - d.age) * this._maxSize) + 'px';
  317. }
  318. },
  319.  
  320.  
  321. // calculate new positions for all particles
  322. resize: function()
  323. {
  324. // get new width and height
  325. this._bodyWidth = this._getWindowWidth() - this._maxSize;
  326. this._bodyHeight = this._getWindowHeight() - this._maxSize - 10;
  327. },
  328.  
  329.  
  330. // get window width
  331. _getWindowWidth: function()
  332. {
  333. return document.getElementsByTagName('body')[0].offsetWidth;
  334. },
  335.  
  336.  
  337. // get window height
  338. _getWindowHeight: function()
  339. {
  340. var h = Math.max(self.innerHeight || 0, window.innerHeight || 0);
  341.  
  342. if (document.documentElement)
  343. h = Math.max(h, document.documentElement.clientHeight || 0);
  344. if (document.body)
  345. {
  346. h = Math.max(h, document.body.clientHeight || 0);
  347. h = Math.max(h, document.body.scrollHeight || 0);
  348. h = Math.max(h, document.body.offsetHeight || 0);
  349. }
  350.  
  351. return h;
  352. }
  353.  
  354. };
  355.  
  356. // shim layer with setTimeout fallback
  357. window.requestAnimFrame = (function(){
  358. return window.requestAnimationFrame ||
  359. window.webkitRequestAnimationFrame ||
  360. window.mozRequestAnimationFrame ||
  361. window.oRequestAnimationFrame ||
  362. window.msRequestAnimationFrame ||
  363. function (cb){
  364. window.setTimeout(cb, 1000 / 60);
  365. };
  366. })();
  367.  
  368.  
  369.  
  370. function highScore(score) {
  371. var saved = 0;
  372.  
  373. saved = parseFloat(localStorage.getItem('highScore')) || 0; //nimmt zeichenketten Argument (highscore) und gibt Float. highscore oder 0
  374. score = parseFloat(score) || 0;
  375. document.getElementById("highscore").innerHTML = saved;
  376.  
  377. if (score > saved) {
  378. saved = score; //speichert neuen Highscore, wenn der score größer ist als die gespeicherte variable
  379. localStorage.setItem('highScore', score);//speichert highscore im browser
  380. fireworks.start();
  381. }
  382. return saved;
  383.  
  384. }
  385.  
  386. $.when($("span").fadeIn()).done(function() {
  387. $.when($("span").fadeOut()).done(function() {
  388. alert("done!")
  389. });
  390. });
Add Comment
Please, Sign In to add comment