-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
214 lines (192 loc) · 5.56 KB
/
script.js
File metadata and controls
214 lines (192 loc) · 5.56 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// DOM elements
const gameText = document.getElementById("game-text");
const choices = document.getElementById("choices");
const gameImage = document.getElementById("game-image");
const restartButton = document.getElementById("restart");
// Game state
let name = "";
let isFirstTime = true;
// Utility function to update the game
function updateGame(text, options, imageUrl = "") {
gameText.textContent = text;
choices.innerHTML = "";
options.forEach((option) => {
const button = document.createElement("button");
button.textContent = option.text;
button.addEventListener("click", option.action);
choices.appendChild(button);
});
// Update image
if (imageUrl) {
gameImage.src = imageUrl;
gameImage.classList.remove("hidden");
} else {
gameImage.classList.add("hidden");
}
}
// Start the game
function startGame() {
if (isFirstTime) {
name = prompt("What is your name?");
isFirstTime = false;
}
updateGame(
`Hello, ${name}. Great! Let the adventure begin...`,
[
{ text: "Go Left", action: goLeft },
{ text: "Go Right", action: goRight },
],
"images/LeftorRight.jpg"
);
restartButton.classList.add("hidden");
}
// Left path
function goLeft() {
updateGame(
"You go left and see a bridge. Do you want to cross it?",
[
{ text: "Cross", action: crossBridge },
{ text: "Don't Cross", action: findCave },
],
"images/Bridge.jpg"
);
}
// Right path
function goRight() {
updateGame(
"You go right and find a steep cliff. Do you want to climb down?",
[
{ text: "Climb Down", action: climbCliff },
{ text: "Don't Climb", action: loseGame },
],
"images/Cliff.jpg"
);
}
// Cross bridge path
function crossBridge() {
updateGame(
"You cross the bridge and it creaks under your weight. Do you want to inspect the bridge for safety first?",
[
{ text: "Inspect", action: inspectBridge },
{ text: "Don't Inspect", action: bridgeCollapses },
],
"images/Creaky-Bridge.jpg"
);
}
// Inspect bridge
function inspectBridge() {
updateGame(
"Good choice! You find a loose plank and fix it. The bridge holds, and you safely cross. You find a treasure chest on the other side! Do you want to open it?",
[
{ text: "Open Chest", action: openChest },
{ text: "Don't Open", action: surviveWithoutTreasure },
],
"images/Chest.jpg"
);
}
// Bridge collapses
function bridgeCollapses() {
updateGame(
"You cross the bridge without inspecting it, and it collapses! You fall into the river and lose.",
[{ text: "Restart", action: restartGame }],
"images/BridgeBreak.jpg"
);
}
// Open chest
function openChest() {
updateGame(
"The chest contains gold and jewels! You win!",
[{ text: "Play Again", action: restartGame }],
"images/Treasure.jpg"
);
}
// Survive without treasure
function surviveWithoutTreasure() {
updateGame(
"You walk away from the chest, never knowing what was inside... You survive, but miss out on treasure.",
[{ text: "Restart", action: restartGame }],
"images/Survived.png"
);
}
// Find cave path
function findCave() {
updateGame(
"You see a dark cave. Do you want to enter it?",
[
{ text: "Enter", action: enterCave },
{ text: "Avoid", action: surviveForest },
],
"images/Cave.jpg"
);
}
// Enter cave
function enterCave() {
updateGame(
"You enter the cave and find a sleeping dragon! Do you want to sneak past it?",
[
{ text: "Sneak", action: sneakPastDragon },
{ text: "Don't Sneak", action: dragonWakesUp },
],
"images/Dragon.jpg"
);
}
// Sneak past dragon
function sneakPastDragon() {
updateGame(
"You successfully sneak past the dragon and find an exit. You win!",
[{ text: "Play Again", action: restartGame }],
"images/CaveExit.jpg"
);
}
// Dragon wakes up
function dragonWakesUp() {
updateGame(
"The dragon wakes up and sees you. You try to escape, but it breathes fire. You lose!",
[{ text: "Restart", action: restartGame }],
"images/DragonFire.jpg"
);
}
// Climb cliff path
function climbCliff() {
updateGame(
"You carefully climb down the cliff and find a hidden path. Do you want to explore it?",
[
{ text: "Explore", action: explorePath },
{ text: "Don't Explore", action: surviveForest },
],
"images/Path.jpg"
);
}
// Explore path
function explorePath() {
updateGame(
"The path leads you to a hidden village where you are welcomed as a hero. You win!",
[{ text: "Play Again", action: restartGame }],
"images/Village.jpg"
);
}
// Survive forest
function surviveForest() {
updateGame(
"You survive but miss out on the adventure. The game ends.",
[{ text: "Restart", action: restartGame }],
"images/Survived.png"
);
}
// Lose game
function loseGame() {
updateGame(
"You trip and fall down the cliff anyways! Better luck next time.",
[{ text: "Restart", action: restartGame }],
"images/GameOver.jpg"
);
}
// Restart the game
function restartGame() {
isFirstTime = false;
startGame();
}
// Attach event listeners
restartButton.addEventListener("click", startGame);
// Initialize game
startGame();