-
Notifications
You must be signed in to change notification settings - Fork 1
/
peli.html
171 lines (143 loc) · 3.9 KB
/
peli.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Adventure game</title>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
$(document).ready(function () {
var items = {};
var day = 0;
var hp = 100;
var room = 0;
var lastmessage = "";
var lastpotionused = 0;
var score = 0;
var gameover = 0;
var treasurefound = 0;
$("#forward").click(function () {
update();
random = Math.round(Math.random()*10);
hp -= random;
day += 1;
room += 1;
lastmessage = "You go to the next room. A monster attacks you. You lose " + random + " HP."
newitems = [];
if (Math.random() >= 0.80) {
newitems.push("Potion");
}
if ( room >= 15 && Math.random() >= 0.95) {
newitems.push("Treasure");
}
if (newitems.length > 0) {additems(newitems);}
update();
});
$("#rest").click(function () {
lastmessage = "You rest for a day and restore 5 HP";
day += 1;
hp += 5;
update();
});
function additems(list) {
//Adds objects from the list to the item dictionary and gives a nice message
lastmessage += "<br>You found new items:"
for (var i = list.length - 1; i >= 0; i--) {
if (!(list[i] in items)) {items[list[i]] = 0;}
items[list[i]] += 1;
lastmessage += " " + list[i];
}
}
function update() {
if (gameover == 1) {}
else {
$("#item_list").html("");
for (var key in items) {
if (items[key] > 0) {
strtoappend = "<li>" + key + " : "+items[key]+ "</li>";
if (key == "Potion") {
strtoappend += "<button id='usepotion' title='Heals 50 HP'>Use</button>";
}
if (key == "Treasure") {
strtoappend += "<button id='usetreasure' title='Use this to win the game'>Use</button>";
}
$("#item_list").append(strtoappend);
potionbutton();
treasurebutton();
}
}
$("#health").text(hp);
$("#day").text(day);
$("#msg").html("");
$("#msg").append(lastmessage)
if (hp <= 0) {
//Game over, call game over function
$("#msg").append("Game over")
gameover = 1;
}
score = hp*treasurefound - day + treasurefound*400;
$("#score").text(score);
}
}
function potionbutton () {
$("#usepotion").click(function () {
if (day != lastpotionused) {
items["Potion"] -= 1;
if (hp > 50) {
lastmessage = "You used an potion to restore " + + " HP";
hp=100;
}
hp += 50;
lastmessage = "You used an potion to restore 50 HP";
lastpotionused = day;
update();
}
});
}
function treasurebutton () {
$("#usetreasure").click(function () {
lastmessage = "You win";
treasurefound = 1;
update();
//call game over function
});
}
function gameover() {
//game over function
gameover = 1;
}
///Game functionalities end here ///
///Utility functionalities start here///
function submit() {
var msg = {
"messageType":"SCORE",
"score": score};
window.parent.postMessage(msg,"*");
}
$("submit_score").click(function () { submit(); });
var message = {
messageType: "SETTING",
options: {
"width": 800, //Integer
"height": 600 //Integer
}
};
window.parent.postMessage(message, "*");
});
</script>
</head>
<body>
<h5>Insturctions: find the treasure as fast as possible, without dying.</h5>
<h5>Rumors say the treasure lies somewhere behind the 15th room.</h5>
<h5>Better hurry, you lose points every day!</h5>
<h5>Tip: you can only use one potion per day..</h5>
<button id="forward">Go forth</button>
<button id="rest">Rest</button>
<h3>Items</h3>
<ul id="item_list"></ul>
<div><span id="health">100</span> HP left</div>
<div>DAY <span id="day">0</span></div>
<div>Score: <span id="score">0</span></div>
<div><span id="msg"></span></div>
<button id="submit_score">Submit score</button>
</body>
</html>