Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>Document</title>
  9. <style>
  10. #obwoluta{
  11. position:relative;
  12. width:100%;
  13. }
  14. h1 {
  15. text-align: center;
  16. }
  17.  
  18. #timer {
  19. position: absolute;
  20. top:50px;
  21. left:35%;
  22. margin: 0 auto;
  23. width: 400px;
  24. height: 30px;
  25. text-align: center;
  26. font-size: 25px;
  27. background-color: gray;
  28. }
  29.  
  30. #pasek {
  31. background-color: red;
  32. z-index: 0;
  33. position: absolute;
  34. left: 0;
  35. top: 0;
  36. width: 400px;
  37. height: 30px;
  38. }
  39.  
  40. #time {
  41. position: relative;
  42. color: black;
  43. z-index: 100;
  44. }
  45.  
  46. #playArea>img {
  47. position: absolute;
  48. width: 100px;
  49. height: 100px;
  50. }
  51.  
  52. #playArea {
  53. margin: 0 auto;
  54. position: absolute;
  55. left:35%;
  56. top: 100px;
  57. height: 500px;
  58. width: 500px;
  59. }
  60.  
  61. #intro {
  62. margin: 0 auto;
  63. position: absolute;
  64. left:35%;
  65. top:100px;
  66. height: 500px;
  67. width: 500px;
  68. display: block;
  69. }
  70. </style>
  71. <script>
  72. window.onload = function () {
  73. document.getElementById("firstOption").addEventListener("click", function () {
  74. czas = 30000;
  75. document.getElementById("intro").style.display = "none"
  76. game()
  77. })
  78.  
  79. document.getElementById("secondOption").addEventListener("click", function () {
  80. czas = 60000;
  81. document.getElementById("intro").style.display = "none"
  82. game()
  83. })
  84.  
  85. document.getElementById("thirdOption").addEventListener("click", function () {
  86. czas = 90000;
  87. document.getElementById("intro").style.display = "none"
  88. game()
  89. })
  90.  
  91. wybor();
  92. function wybor() {
  93. document.getElementById("intro").style.display = "block"
  94. document.getElementById("playArea").style.display = "none"
  95. document.getElementById("timer").style.display = "none"
  96. }
  97.  
  98. function game() {
  99. node = document.getElementById("playArea")
  100. while (node.hasChildNodes()) {
  101. node.removeChild(node.lastChild);
  102. }
  103. let height = 4;
  104. let width = 4;
  105. let count = 0
  106. let objs = []
  107. let actualClicked = []
  108. let swap = false
  109. let goingOn = false
  110. let sources = []
  111. for (let i = 1; i <= 8; i++) { sources.push("img/" + i + ".jpg"); sources.push("img/" + i + ".jpg") }
  112. shuffle(sources)
  113. document.getElementById("playArea").style.display = "block"
  114. for (let i = 0; i < height; i++) {
  115. for (let j = 0; j < width; j++) {
  116. let left = (j * 101).toString()
  117. let top = (i * 101).toString()
  118. let img = document.createElement('img')
  119. //tablica reprezentujaca id src i zmienna clicked kazdego obrazka
  120. objs.push({
  121. id: 'id' + count,
  122. src: sources[count],
  123. clicked: false
  124. })
  125. img.src = 'img/0.jpg'
  126. document.getElementById("playArea").appendChild(img).setAttribute('style', 'left:' + left + "px; top:" + top + 'px;')
  127. img.id = 'id' + count
  128. img.addEventListener("mousedown", listener)
  129. count++
  130. }
  131. }
  132.  
  133.  
  134. function listener(e) {
  135. if (!goingOn) {
  136. goingOn = true
  137. timer(new Date().getTime())
  138. }
  139. let target = (e.target.id).slice(2)
  140. //swap - czy dzieje sie jakies obrocenie obrazkow
  141. if (swap == false) {
  142. e.target.src = objs[target].src
  143. //decyzja o dodaniu do arraya actualClicked
  144. if (objs[target].clicked == false) {
  145. actualClicked.push(objs[target])
  146. objs[target].clicked = true
  147. }
  148. //porownanie przy rozmiarze arraya = 2
  149. if (actualClicked.length == 2) {
  150. console.log(actualClicked)
  151. if (compare()) deleteimg()
  152. else reswaping()
  153. }
  154. }
  155. }
  156. //funkcja obslugi obrocenia roznych obrazkow
  157. async function reswaping() {
  158. swap = true
  159. await sleep(300)
  160. actualClicked.forEach(function (element) {
  161. document.getElementById(element.id).src = 'img/0.jpg'
  162. objs[(element.id).slice(2)].clicked = false
  163. }, this);
  164. actualClicked = []
  165. swap = false
  166. }
  167. //funkcja obslugi obrocenia tych samych obrazkow
  168. async function deleteimg() {
  169. swap = true
  170. await sleep(300);
  171. actualClicked.forEach(function (element) {
  172. document.getElementById(element.id).removeEventListener("mousedown", listener);
  173. objs[(element.id).slice(2)].clicked = false
  174. }, this);
  175. actualClicked = []
  176. swap = false
  177. count -= 2
  178. if (count == 0) end('wygrana')
  179. }
  180. //zakonczenie gry
  181. function end(result) {
  182. goingOn = false;
  183. if (result == 'przegrana') {
  184. window.alert('Przegrales xD')
  185. }
  186. else {
  187. window.alert("Wygrales! czas:" + czas - (czas - currTime) / 1000)
  188. }
  189. wybor()
  190. }
  191. //porownanie pola src z actualClicka
  192. function compare() {
  193. if (actualClicked[0].src == actualClicked[1].src) return true;
  194. else return false;
  195. }
  196.  
  197. //funkcja mieszajaca araya , credits : knuth - shuffle https://bost.ocks.org/mike/shuffle/
  198. function shuffle(srcs) {
  199. var m = srcs.length, t, i;
  200. // While there remain elements to shuffle…
  201. while (m) {
  202. // Pick a remaining element…
  203. i = Math.floor(Math.random() * m--);
  204. // And swap it with the current element.
  205. t = srcs[m];
  206. srcs[m] = srcs[i];
  207. srcs[i] = t;
  208. }
  209. return srcs;
  210. }
  211.  
  212. function sleep(ms) {
  213. return new Promise(resolve => setTimeout(resolve, ms));
  214. }
  215.  
  216. function timer(startTime) {
  217. document.getElementById("timer").style.display = "block"
  218. timeit()
  219. function timeit() {
  220. if (goingOn) {
  221. currTime = new Date().getTime() - startTime
  222. let timeleft = czas - currTime
  223. if (timeleft < 0) end('przegrana')
  224. var minutes = Math.floor((timeleft % (1000 * 60 * 60)) / (1000 * 60))
  225. var seconds = Math.floor((timeleft % (1000 * 60)) / 1000);
  226. var miliseconds = timeleft%1000
  227. document.getElementById("time").innerText = minutes + ":" + seconds + ":" +miliseconds
  228. document.getElementById("pasek").style.width = (czas - currTime) / czas * 100 + '%'
  229. requestAnimationFrame(timeit)
  230. }
  231. }
  232. }
  233. }
  234. }
  235. </script>
  236. </head>
  237.  
  238. <body>
  239. <div id="obwoluta">
  240. <h1>Memory</h1>
  241. <div id="timer">
  242. <span id="time"></span>
  243. <div id="pasek"></div>
  244. </div>
  245. <div id="playArea">
  246.  
  247. </div>
  248. <div id="intro">
  249. <img src="img/intro.png" alt="" usemap="#Map" />
  250.  
  251. <map name="Map" id="Map">
  252. <area alt="" title="" href="#" shape="poly" coords="99,50,173,190,48,145" id="firstOption" />
  253. <area alt="" title="" href="#" shape="poly" coords="305,141,357,236,232,282" id="secondOption" />
  254. <area alt="" title="" href="#" shape="poly" coords="99,238,173,378,48,334" id="thirdOption" />
  255. </map>
  256. </div>
  257. </div>
  258. </body>
  259.  
  260. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement