-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reaction_Game.html
130 lines (105 loc) · 3.51 KB
/
Reaction_Game.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html>
<head>
<title>Reaction Game</title>
<style type="text/css">
#shape
{
position: relative;
margin-top: 50px;
top=50px;
left: 20px;
height: 100px;
width: 100px;
border-radius: 50%;
background-color: red;
display: none;
}
#container
{
height: 400px;
width: 1000px;
border-color:brown;
border-style:dotted;
margin-left: 150px;
border-width: 2px;
background-color: aliceblue;
}
#text
{
text-align: center;
}
#first_p
{
color: blue;
font-size: 30px:
}
#sp
{
font-size: 30px;
}
#score
{
color: green;
}
</style>
</head>
<body>
<div id="text">
<p>If your reaction time is greater than 1.3 sec then game over</p>
<p id="first_p">your response time:<span id="sp"> </span> </p>
<p> <span id="score">playing</span></p>
</div>
<div id="container">
<div id="shape">
</div>
</div>
<script type="text/javascript">
//start=new Date().getTime();
var start=0; var end= 0;var c=0;
function blocker()
{
var top=Math.random()*50+50;
var side=Math.random()*400+50;
var rad=Math.random()*40+20;
document.getElementById("shape").style.top=top+"px";
document.getElementById("shape").style.left=side+"px";
document.getElementById("shape").style.backgroundColor=getRandomColor();
document.getElementById("shape").style.borderRadius=rad+"%";
document.getElementById("shape").style.display="block";
start=new Date().getTime();
}
setTimeout(blocker,1000);
document.getElementById("shape").onclick=function()
{
document.getElementById("container").style.backgroundColor=getRandomColor();
document.getElementById("shape").style.display = "none";
end = new Date().getTime();
var timeTaken=0;
timeTaken=(end-start)/1000;
document.getElementById("sp").innerHTML=timeTaken+"s";
if(timeTaken>1.3)
{
document.getElementById("score").innerHTML="Game Over Your score:"+c;
document.getElementById("score").style.color="red";
return;
}
else
{
c++;
}
setTimeout(blocker,Math.random()*2000);
}
function getRandomColor()
{
var letters = '0123456789ABCDEF';
var color='#';
for(var i=0;i<6;i++)
{
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
</body>
</html>