-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from 23wh1a0513/patch-8
Create Interactive Storytelling
- Loading branch information
Showing
1 changed file
with
323 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); |