|
1 | | -var bestTime = 0; |
2 | | -var start = new Date().getTime(); |
3 | | - |
4 | | -function getRandomColor() { |
5 | | - |
6 | | - var x = Math.floor(Math.random()* 256); |
7 | | - var y = Math.floor(Math.random()* 256); |
8 | | - var z = Math.floor(Math.random()* 256); |
| 1 | +window.onload = function () { |
9 | 2 |
|
10 | | - var randomColorMaker = "rgb(" + x + "," + y + "," + z + ")"; |
| 3 | + var bestTime = 10; |
| 4 | + var start = new Date().getTime(); |
| 5 | + document.getElementById("rules").onclick = function () { |
| 6 | + alert("Try to click the shapes as soon as possible to increase your score"); |
| 7 | + } |
11 | 8 |
|
12 | | - document.getElementById("shape").style.backgroundColor = randomColorMaker; |
13 | | -} |
| 9 | + function getRandomColor() { |
| 10 | + |
| 11 | + var x = Math.floor(Math.random()* 256); |
| 12 | + var y = Math.floor(Math.random()* 256); |
| 13 | + var z = Math.floor(Math.random()* 256); |
| 14 | + |
| 15 | + var randomColorMaker = "rgb(" + x + "," + y + "," + z + ")"; |
14 | 16 |
|
15 | | -function makeShapeAppear() { |
16 | | - |
17 | | - var top = Math.random()* 400; |
18 | | - |
19 | | - var left =Math.random()* 1150; |
20 | | - |
21 | | - var width = (Math.random()* 400) + 100; |
22 | | - |
23 | | - if(Math.random() < 0.5) { |
| 17 | + document.getElementById("shape").style.backgroundColor = randomColorMaker; |
| 18 | + } |
| 19 | + |
| 20 | + function makeShapeAppear() { |
| 21 | + |
| 22 | + var top = Math.random()* 200; |
| 23 | + |
| 24 | + var left =Math.random()* 1150; |
24 | 25 |
|
25 | | - document.getElementById("shape").style.borderRadius = "50%"; |
26 | | - } else { |
| 26 | + var width = (Math.random()* 400) + 100; |
27 | 27 |
|
28 | | - document.getElementById("shape").style.borderRadius = "0"; |
| 28 | + if(Math.random() > 0.5) { |
| 29 | + |
| 30 | + document.getElementById("shape").style.borderRadius = "50%"; |
| 31 | + } else { |
| 32 | + |
| 33 | + document.getElementById("shape").style.borderRadius = "0"; |
| 34 | + } |
| 35 | + |
| 36 | + document.getElementById("shape").style.top = top + "px"; |
| 37 | + document.getElementById("shape").style.left = left + "px"; |
| 38 | + document.getElementById("shape").style.width = width + "px"; |
| 39 | + document.getElementById("shape").style.height = width + "px"; |
| 40 | + |
| 41 | + document.getElementById("shape").style.backgroundColor = getRandomColor(); |
| 42 | + |
| 43 | + document.getElementById("shape").style.display ="block"; |
| 44 | + start = new Date().getTime(); |
29 | 45 | } |
30 | | - |
31 | | - document.getElementById("shape").style.top = top + "px"; |
32 | | - document.getElementById("shape").style.left = left + "px"; |
33 | | - document.getElementById("shape").style.width = width + "px"; |
34 | | - document.getElementById("shape").style.height = width + "px"; |
35 | | - |
36 | | - document.getElementById("shape").style.backgroundColor = getRandomColor(); |
37 | | - |
38 | | - document.getElementById("shape").style.display ="block"; |
39 | | - start = new Date().getTime(); |
40 | | -} |
41 | 46 |
|
42 | | -function Timeout() { |
43 | | - |
44 | | - setTimeout(makeShapeAppear, Math.random()* 2000); |
45 | | - |
46 | | -} |
47 | | -Timeout(); |
| 47 | + function Timeout() { |
| 48 | + |
| 49 | + setTimeout(makeShapeAppear, Math.random()* 2000); |
| 50 | + |
| 51 | + } |
| 52 | + Timeout(); |
48 | 53 |
|
49 | 54 |
|
50 | | -document.getElementById("shape").onclick = function() { |
51 | | - |
52 | | - |
53 | | - document.getElementById("shape").style.display = "none"; |
54 | | - |
55 | | - var end = new Date().getTime(); |
56 | | - |
57 | | - var timeTaken = (end - start) / 1000 + " sec"; |
58 | | - |
59 | | - document.getElementById("timeTaken").innerHTML = timeTaken; |
60 | | - |
| 55 | + document.getElementById("shape").onclick = function() { |
61 | 56 |
|
62 | | - |
63 | | - if (timeTaken < bestTime || bestTime == 0) { |
| 57 | + |
| 58 | + document.getElementById("shape").style.display = "none"; |
| 59 | + |
| 60 | + var end = new Date().getTime(); |
| 61 | + |
| 62 | + var timeTaken = (end - start) / 1000; |
| 63 | + |
| 64 | + document.getElementById("timeTaken").innerHTML = timeTaken + " sec"; |
| 65 | + |
| 66 | + if(timeTaken < bestTime) { |
64 | 67 | bestTime = timeTaken; |
65 | | - document.getElementById("best").innerHTML = bestTime + "s" |
66 | | - } else { |
| 68 | + document.getElementById("best").innerHTML = bestTime + " sec"; |
67 | 69 |
|
68 | | - document.getElementById("best").innerHTML = timeTaken + " sec"; |
69 | | - } |
70 | | - |
71 | | - |
72 | | - Timeout(); |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + Timeout(); |
| 77 | + } |
| 78 | + // exit function |
| 79 | + exit.onclick = function(e) { |
| 80 | + score = bestTime*1000; |
| 81 | + alert("Great, You Best Score till now is: " + score); |
| 82 | + } |
73 | 83 | } |
0 commit comments