forked from suryanshsk/Python-Voice-Assistant-Suryanshsk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInteractive Storytelling
323 lines (306 loc) · 8.96 KB
/
Interactive Storytelling
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#####html###
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Story Game</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1 id="storyTitle">Welcome to the Interactive Story Game</h1>
<div id="storyText"></div>
<div id="choices"></div>
<script src="script.js"></script>
</body>
</html>
####CSS##########
body {
font-family: 'Arial', sans-serif;
margin: 20px;
padding: 20px;
background-color: #f0f0f0;
color: #333;
}
h1 {
text-align: center;
color: #4CAF50;
margin-bottom: 20px;
}
#storyText {
margin-bottom: 20px;
padding: 15px;
background-color: #ffffff;
border-radius: 5px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
font-size: 1.2em;
line-height: 1.5em;
}
#choices {
text-align: center;
}
#choices button {
margin: 5px;
padding: 10px 15px;
cursor: pointer;
border: none;
background-color: #008cba;
color: white;
font-size: 1em;
border-radius: 5px;
transition: background-color 0.3s, transform 0.2s;
}
#choices button:hover {
background-color: #005f75;
transform: scale(1.05);
}
#choices button:active {
transform: scale(0.95);
}
@media (max-width: 600px) {
body {
padding: 10px;
}
#storyText {
font-size: 1em;
}
#choices button {
padding: 8px 12px;
font-size: 0.9em;
}
}
####JS#########
const storyText = document.getElementById('storyText');
const choicesDiv = document.getElementById('choices');
// Array of stories
const stories = [
{
title: "The Mysterious Forest",
start: {
text: "You find yourself in a mysterious forest. There are two paths ahead of you.",
choices: {
left: "Take the left path.",
right: "Take the right path."
},
next: {
left: 'leftPath',
right: 'rightPath'
}
},
leftPath: {
text: "You take the left path and stumble upon a beautiful clearing with a magical pond.",
choices: {
look: "Look into the pond.",
walk: "Walk around the clearing."
},
next: {
look: 'lookIntoPond',
walk: 'walkAround'
}
},
rightPath: {
text: "You take the right path and meet a wise old man sitting under a tree.",
choices: {
ask: "Ask him for advice.",
ignore: "Ignore him and keep walking."
},
next: {
ask: 'askOldMan',
ignore: 'ignoreOldMan'
}
},
lookIntoPond: {
text: "You look into the pond and see a reflection that shows your greatest fears.",
choices: {
face: "Face your fears.",
run: "Run away."
},
next: {
face: 'end',
run: 'end'
}
},
walkAround: {
text: "While walking around, you discover a hidden treasure!",
choices: {},
next: {}
},
askOldMan: {
text: "The old man shares his wisdom and guides you through the forest.",
choices: {},
next: {}
},
ignoreOldMan: {
text: "Ignoring him, you eventually get lost in the forest.",
choices: {},
next: {}
},
end: {
text: "Your journey ends here, but the lessons learned will stay with you.",
choices: {},
next: {}
}
},
{
title: "The Haunted Castle",
start: {
text: "You stand before an ancient castle, rumors say it's haunted. What do you do?",
choices: {
enter: "Enter the castle.",
leave: "Leave the castle."
},
next: {
enter: 'enterCastle',
leave: 'leaveCastle'
}
},
enterCastle: {
text: "Inside, you find yourself in a grand hall filled with cobwebs and eerie portraits.",
choices: {
explore: "Explore the hall.",
investigate: "Investigate a noise."
},
next: {
explore: 'exploreHall',
investigate: 'investigateNoise'
}
},
leaveCastle: {
text: "You decide to leave the castle but hear whispers behind you.",
choices: {},
next: {}
},
exploreHall: {
text: "You find a hidden door behind a tapestry! Do you go inside?",
choices: {
yes: "Yes, go inside.",
no: "No, explore more."
},
next: {
yes: 'hiddenRoom',
no: 'exploreMore'
}
},
investigateNoise: {
text: "You find a ghost trying to play the piano. It looks confused.",
choices: {
help: "Help the ghost.",
flee: "Flee in terror."
},
next: {
help: 'end',
flee: 'end'
}
},
hiddenRoom: {
text: "Inside, you discover valuable treasures and artifacts long forgotten!",
choices: {},
next: {}
},
exploreMore: {
text: "You stumble upon more hidden secrets of the castle.",
choices: {},
next: {}
}
},
{
title: "Space Odyssey",
start: {
text: "Your spaceship has just landed on a mysterious alien planet. What do you do?",
choices: {
explore: "Explore the surface.",
scan: "Scan for life forms."
},
next: {
explore: 'exploreSurface',
scan: 'scanLife'
}
},
exploreSurface: {
text: "You step out and find strange plants and creatures. One seems friendly!",
choices: {
approach: "Approach the creature.",
takePhoto: "Take photos."
},
next: {
approach: 'friendlyCreature',
takePhoto: 'captureMoment'
}
},
scanLife: {
text: "Your scanner detects intelligent life nearby. Do you investigate?",
choices: {
yes: "Yes, investigate.",
no: "No, stay in the ship."
},
next: {
yes: 'intelligentLife',
no: 'staySafe'
}
},
friendlyCreature: {
text: "The creature offers you a glowing fruit. Do you eat it?",
choices: {
eat: "Eat the fruit.",
refuse: "Refuse the gift."
},
next: {
eat: 'magicalFruit',
refuse: 'gratefulCreature'
}
},
captureMoment: {
text: "Your camera malfunctions and attracts the creature's attention!",
choices: {},
next: {}
},
intelligentLife: {
text: "You meet the aliens, who communicate through telepathy. They are curious about you!",
choices: {},
next: {}
},
staySafe: {
text: "You decide to stay put and scan your surroundings. Something seems off.",
choices: {},
next: {}
},
magicalFruit: {
text: "You gain new abilities and communicate with the creature!",
choices: {},
next: {}
},
gratefulCreature: {
text: "The creature seems understanding, and you both form a bond!",
choices: {},
next: {}
}
}
];
// Function to start a random story
function startStory() {
const randomIndex = Math.floor(Math.random() * stories.length);
const selectedStory = stories[randomIndex];
document.getElementById('storyTitle').innerText = selectedStory.title;
changeStory(selectedStory.start);
}
// Function to change the story part based on user choices
function changeStory(part) {
storyText.innerHTML = part.text;
choicesDiv.innerHTML = '';
for (const [key, value] of Object.entries(part.choices)) {
const button = document.createElement('button');
button.innerHTML = value;
button.onclick = () => {
const nextPart = part.next[key];
if (nextPart) {
changeStory(stories.find(story => story[part.next[key]])[part.next[key]]);
} else {
storyText.innerHTML += "<br><strong>The story ends here...</strong>";
choicesDiv.innerHTML = '';
}
};
choicesDiv.appendChild(button);
}
}
// Start a random story on page load
startStory();