Welcome to Pet Simulator!
- Hunger:
- Happiness:
Built with lots of love by your local JavaScript developer.
以下がペット育成ゲームのコード例です。セキュリティに関する考慮が行われています。
```html
<!DOCTYPE html>
<html>
<head>
<title>Pet Simulator</title>
<script type="text/javascript">
let pet = {
name: "Fluffy",
hunger: 0,
happiness: 100,
tricks: []
};
function feed() {
pet.hunger -= 10;
if (pet.hunger < 0) pet.hunger = 0;
updateStats();
}
function walk() {
pet.happiness += 20;
pet.hunger += 10;
updateStats();
}
function teachTrick() {
let trick = prompt('What trick do you want to teach your pet?');
pet.tricks.push(trick);
pet.happiness += 10;
updateStats();
}
function doTrick() {
let trickIndex = Math.floor(Math.random() * pet.tricks.length);
alert(pet.name + " does " + pet.tricks[trickIndex] + "!");
pet.happiness += 5;
updateStats();
}
function updateStats() {
document.getElementById('name').innerHTML = pet.name;
document.getElementById('hunger').innerHTML = pet.hunger;
document.getElementById('happiness').innerHTML = pet.happiness;
}
window.onload = function() {
updateStats();
};
</script>
</head>
<body>
<h1>Welcome to Pet Simulator!</h1>
<div>
<h2 id="name"></h2>
<ul>
<li>Hunger: <span id="hunger"></span></li>
<li>Happiness: <span id="happiness"></span></li>
</ul>
</div>
<div>
<button onclick="feed()">Feed</button>
<button onclick="walk()">Walk</button>
<button onclick="teachTrick()">Teach Trick</button>
<button onclick="doTrick()">Do Trick</button>
</div>
<p>Built with lots of love by your local JavaScript developer.</p>
</body>
</html>
```
以上のコードは、ユーザーに対してペットに名前をつけ、ペットの飢餓度と幸福度を計算し、餌を与えたり散歩させたり芸を教えたりすることができます。芸の種類が増えるたびに幸福度が上がります。