-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreasure.html
93 lines (81 loc) · 2.19 KB
/
treasure.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
<!DocType html>
<html>
<head>
<title>
Treasure Hunt
</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<img id="map" width="800" height="800" src="http://nostarch.com/images/treasuremap.png">
<p id="distance"> </p>
<h1 id="win">Treasure</h1>
<script>
var getRandomNumber=function(size)
{
return Math.floor(Math.random()*size);
}
//event is predefined object which has built-in information about the player's click
var getDistance=function(event,target){
console.log(target.x);
console.log(target.y);
var diffX=event.offsetX -target.x;
var diffY=event.offsetY-target.y;
console.log(diffX,diffY);
return Math.sqrt(diffX * diffX)+(diffY * diffY);
}
var remainingDistanceHint =function (distance){
if(distance <10)
return "BoilingHot";
else if(distance <20)
return "Really Hot";
else if(distance < 40 )
return "Hot";
else if(distance<80)
return "Warm";
else if(distance <160)
return "Cold";
else if(distance <320)
return "Really Cold";
else
return "Freezing!!";
}
var offSet=10;
function move()
{
$("#win").offset({left : offSet});
offSet++;
if (offSet > 800)
offSet=0;
//$("#alert").offset({top: topOffSet});
}
setInterval(move,10);
var width=400;
var height=400;
var target={
x: getRandomNumber(width),
y:getRandomNumber(height)
};
var count=0;
var remianingClicks=5;
setInterval(move,30);
$("#map").click(function (event){
count++;
remianingClicks--;
if(count >5){
alert("Game Over");
return;
}
console.log("Reamianing Clicks "+remianingClicks);
distance=getDistance(event,target);
console.log(distance);
$("#distance").text(remainingDistanceHint(distance));
if(distance < 8)
{
alert("You took "+count+ "clicks to find the treasure");
alert( "You Won!!");
}
});
</script>
</body>
</html>