JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<script src="myjs.js"> </script>
<link rel="stylesheet" type="text/css" href="style.css">
<!-- Font Link Below Google fonts -->
<meta charset="utf-8">
<title>DigitalPet2</title>
</head>
<body>
<section class="dark">
<p class="dark" id="demo"><button onclick="entername()">Click to Enter Name & see your Fortune</button></p>
<p id="tamoVoice" class="dark"></p>
</section>
<section id="tamoHome"></section>
</body>
</html>
CSS
p{
text-align: left;
}
.dark{
margin: auto;
width: 50%;
text-align: center;
color: pink;
background-color: black;
border-radius: 8em;
}
.hidden{
visibility:hidden;
}
#tamoVoice, #petStats{
border: 1px solid black;
width: 200px;
height: 50px;
}
.talking{
animation: mouthMotion 1s ease-in-out 1s infinite;
}
@keyframes mouthMotion{
0%{
transform: scaleY(1);
transform-origin: 20px 320px;
}
50%{
transform: scaleY(0.5);
transform-origin: 20px 320px;
}
100%{
transform: scaleY(1);
transform-origin: 20px 320px;
}
}
JavaScript
console.log("Checking log...And it Works!");
function Tamogotchi(tamoName) {
this.Name;
this.init = () => {
this.Name = tamoName;
this.fetchData();
this.showTamogotchi(document.querySelector("#tamoHome"));
this.talkBox = document.querySelector("#tamoVoice");
this.statsCounter = document.querySelector("#petStats");
}
this.init();
}
// Fetches Data from Json File
Tamogotchi.prototype.fetchData = function(){
fetch('data.json')
.then(data => data.json())
.then( data =>{
this.sayings = data.fortunes;
this.moods = data.moods;
this.hatch();
})
.catch(error =>{
console.log(error);
});
}
function entername() {
var person = prompt("Please enter your name", "Harry Potter");
if (person != null) {
document.getElementById("demo").innerHTML =
"Hello " + person + "! Check Your Fortune";
}
}
// Hatch Function to start start digital pet
Tamogotchi.prototype.hatch = function(){
this.initialFace();
}
// Function for random words
Tamogotchi.prototype.words = function(){
var say = this.moods[Math.floor(Math.random()*this.moods.length)];
this.talk(say);
}
// a command that takes in a mood and returns a phrase also changes pet face depending on mood entered uses check command
Tamogotchi.prototype.talk = function(mood){
let endFace = this.neutralFace;
switch (mood){
case "good":
endFace = this.happyface;
break;
case "bad":
endFace = this.sadFace;
break;
}
const moodPhrases = this.sayings.filter(saying => saying.mood == mood);
if(moodPhrases.length>0){
const moodPhrase = moodPhrases[Math.floor(Math.random()*moodPhrases.length)];
this.useMouth(moodPhrase.saying, endFace);
}else{
this.useMouth("derp derp!", endFace);
}
}
Tamogotchi.prototype.useMouth = function(words, mouthType){
this.startTalking(mouthType);
this.talkBox.innerText =...