The world of gaming has always been a space where creativity and imagination come together to create captivating experiences. One such experience is the Pokemon-inspired game we’re about to delve into, complete with graphics, animations, and a dynamic battle system. In this blog post, we’ll take a closer look at the code that powers this game and explore how it all comes together to deliver an engaging gaming experience.
Introduction
The Pokemon franchise has captured the hearts of millions with its charming creatures and exciting battles. Our game draws inspiration from this beloved series and allows players to engage in thrilling creature battles with graphics and animations. Let’s break down the different components that make up this game.
HTML and CSS: Setting the Stage
The foundation of any web-based game is the HTML and CSS structure. The game interface is designed with a focus on the battle area, where two creatures face off. The use of CSS Flexbox ensures proper alignment and spacing, creating an appealing visual layout.
<!DOCTYPE html>
<html>
<head>
<title>Pokemon-like Game with Graphics and Animations</title>
<style>
/* CSS styles */
</style>
</head>
<body>
<!-- Game interface -->
</body>
</html>
Meet the Creatures
Two creatures take center stage in our game: Pikachu and Charmander. Each creature is represented by an image and accompanied by a display of their current level and health points (HP). The game mechanics include the ability for creatures to level up as they battle and take damage from each other.
<!-- Inside the body tag -->
<div id="battle-container">
<div class="creature" id="pikachu">
<img src="pikachu.png" alt="Pikachu">
<div id="pikachu-info">Pikachu: Level 1, HP: 50</div>
</div>
<!-- Battle log will be displayed here -->
<div class="creature" id="charmander">
<img src="charmander.png" alt="Charmander">
<div id="charmander-info">Charmander: Level 1, HP: 40</div>
</div>
</div>
<button id="start-button">Start Battle</button>
<button id="replay-button" style="display: none;">Replay</button>
Abilities for Strategy
Abilities add an element of strategy to the battles. Each creature possesses a unique ability that can influence the outcome of the battle. These abilities are defined in the JavaScript code and can modify the damage dealt to the opponent.
class Ability {
constructor(name, damageModifier) {
this.name = name;
this.damageModifier = damageModifier;
}
}
The JavaScript Engine
The heart of our game lies in the JavaScript code that governs the battle system and animations. Here’s a breakdown of the key functionalities:
- Creature and Ability Classes: The
Creatureclass is responsible for creating creature instances with attributes such as name, health, attack, and abilities. Abilities are defined as a separate class, allowing for expandable and customizable abilities in the future. - Battle Logic: The
battlefunction simulates a battle between two creatures. It incorporates animations to create a dynamic and engaging experience. Creatures take turns attacking each other, either using their abilities or a standard attack. Damage calculations and health adjustments are handled here. - Leveling Up: When a creature defeats its opponent, it gains experience points and levels up. This not only enhances the creature’s attributes but also displays a level-up message in the battle log.
- Animation: Animations play a crucial role in making battles visually appealing. The
animateAttackfunction adds movement to the attacking creature, creating a sense of action and impact. - Battle Log: The battle log displays a running commentary of the battle’s progression. It provides real-time updates on actions taken by each creature, making the battle feel immersive.
- Game Loop: The
gameLoopfunction orchestrates the battles between Pikachu and Charmander. It continues until one of the creatures is defeated, and a victory message is displayed.
class Creature {
// Creature class definition
}
// Abilities definition
const abilities = [
new Ability("Electric Shock", 1.2),
new Ability("Fire Blast", 1.5),
new Ability("Water Jet", 0.8)
];
// Pikachu and Charmander instances
const pikachu = new Creature("Pikachu", 50, 15, [abilities[0]]);
const charmander = new Creature("Charmander", 40, 20, [abilities[1]]);
// Battle function
function battle(attacker, defender) {
// Battle logic
}
// Game loop
function gameLoop() {
// Game loop logic
}
// Event listeners for buttons
const startButton = document.getElementById("start-button");
const replayButton = document.getElementById("replay-button");
startButton.addEventListener("click", () => {
gameLoop();
startButton.disabled = true;
});
replayButton.addEventListener("click", () => {
// Replay logic
});
Bringing It All Together
With HTML, CSS, and JavaScript working in harmony, our Pokemon-inspired game comes to life. Players can click the « Start Battle » button to initiate the game loop, which simulates battles between Pikachu and Charmander. The battle log keeps players informed about the ongoing action, and animations add a dynamic touch to the experience.
<!DOCTYPE html>
<html>
<head>
<title>Pokemon-like Game with Graphics and Animations</title>
<style>
#battle-container {
display: flex;
justify-content: space-between;
align-items: center;
margin: 20px;
}
.creature {
display: flex;
flex-direction: column;
align-items: center;
}
.creature img {
width: 100px;
height: 100px;
}
#battle-log {
width: 450px;
height: 350px;
border: 1px solid black;
overflow: auto;
margin-top: 20px;
}
</style>
</head>
<body>
<div id="battle-container">
<div class="creature" id="pikachu">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICA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AxuyA7MrG4/VwxDogC7ohG6Hds9/s1WJe9u+GsOBJK6rS7J0Q9vE5Nmw3zRR7x/uodz8Mo2gM0BAQxu0RR/0hQ7t6w7SSbqDPfS8rZ/NYRMS7yVudCkpwjHc+fW+fo+7evh2YHCPRFXwG8+8L1CiD1VyR6EDukrrI65f5DtB3PFP5qQOuDlOU/g1GsnZdl5aTiQ/g+A3nuFvaIO2Vt8F3P14+q4Doif/AxRP6f0XyJkRAAAAAElFTkSuQmCC" alt="Pikachu">
<div id="pikachu-info">Pikachu: Level 1, HP: 50</div>
</div>
<div id="battle-log"></div>
<div class="creature" id="charmander">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAG8QAABvEBXlFFbAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAABB6SURBVHja3VsLWNRVFh/XrMQ0y0frEx+IDxBhZv7veYGCgDwEBBNEEEREVFBE1BQt09SybLW1LHtnVu63u9r2LnV3ybVat3Xd3XZdH9lmqZumiIozzNlz7swgw8zAzDCKX3zf+YZ53XvP757zO497RwUAqkCImpOLOFH6WsNL2YEa82ZIQAbR8vIiDS9DtIm34iPJXMd7JpPptiheN0YjKNE/SQDUvKInpXMzIuD83k6QlhJl1gqyRc3r0tAatuB7dQQOiZqXkn5SANDuipL4zbgYDk7vvh3gCxV8/fGdoDcIVk6QmNL5UyIsv/1lb9DpBKual1/+SQGgFZUUUnLflnsBfq9iAMBhFby9tRdTftfTvdlz+FQFOWlj6LVvAsI3gk6Df0HtDgAS305BkuDqxz8D2IUvLUX5XAXn9ndiADxYEQJwUMXAWVocClpRvtqW+TIzMztqBHkrjY2881lUlL5XuwIgydIJ8n14D59WoZTYH79UQVqyGnLjIqBufUfYue7ncH/qGEBuYAtHBd5GRVYROfpkcYL8Cim/bPYwQBdrwDH+GWYy3dUuAOBfBwx75kcXDAZYjS8tsQOQg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ENbHNDBEnrbSKjA9ndvZq80SqKu7Cw6RcYlGEiv1AC5snHPckHk3vaUmBO3hiQjpBGY+pJvpRuiIKT0zv7tBgn68DdI1CIsWk3SbkL5feyRwph9DqdN56e4f8cR/K6QByGPlzvEboiE7BbYmpeGk+oLh0fCt/5ubgbLXRhoxiJDzNYi0aSRgf8mhwSygp2Tx/rg29xsltJ+eMY82fEhDO/Vwvy9Bt2T9Bx6DgLkT6Z3/mWUP7faPZTjJGU8jbQRcobflFSK0gFhPRknLQm+552Vf59JLwknQYoX9FqlYSb9osRNSfFy6J4lmLtQwkhreYJgZavpnUBKtRYESZLRzlO5m76j6bowEQjyE+SNRDzbpvYH47dYCD+hYpvSg4GoyTYyI5OgP28HR6wu8JarcRLkvQZa4dh1vhAfGjAXeOjyT1gQewIVkjZC6r3eV4Xekv9dJYuQtMvt3Bx12iRKXoNLMGw+WpaXzg4tZvXylKYpQLsebQoMnNqa9nL7CuU3EQKwrBb9sfTjiJKyytzqCePGeQlR2kbg6V1FhInhSvazZXIHesnDAEqtujWaUE0VomYbJF5O76jUDHDK2+izKBb37f8r8fd3RKNEvSjMDbP1HLyM6jUb3AnD+gl6RsCh0IXZm211LbiRakGI8yvkFeeola2RiMPvZm/Hf4/Wzb6sYd8VT8AAAAASUVORK5CYII=" alt="Charmander">
<div id="charmander-info">Charmander: Level 1, HP: 40</div>
</div>
</div>
<button id="start-button">Start Battle</button>
<button id="replay-button" style="display: none;">Replay</button>
<script>
class Ability {
constructor(name, damageModifier) {
this.name = name;
this.damageModifier = damageModifier;
}
}
class Creature {
constructor(name, health, attack, abilities = []) {
this.name = name;
this.health = health;
this.attack = attack;
this.level = 1;
this.abilities = abilities;
}
levelUp() {
this.level++;
this.attack += 5;
this.health += 10;
}
takeDamage(damage) {
this.health -= damage;
}
isDefeated() {
return this.health <= 0;
}
// Reset creature's health
resetHealth() {
this.health = this.maxHealth;
updateCreatureInfo(this);
}
useAbility(target) {
if (this.abilities.length > 0) {
const randomAbility = this.abilities[Math.floor(Math.random() * this.abilities.length)];
const modifiedDamage = this.attack * randomAbility.damageModifier;
target.takeDamage(modifiedDamage);
addToBattleLog(`${this.name} uses ${randomAbility.name} on ${target.name}!`);
}
}
animateAttack() {
addToBattleLog(`${this.name} is attacking...`);
const attackerElement = document.getElementById(this.name.toLowerCase());
attackerElement.style.transform = 'translateX(100px)';
setTimeout(() => {
attackerElement.style.transform = 'translateX(0)';
addToBattleLog(`${this.name} attack animation finished.`);
}, 300);
}
}
const abilities = [
new Ability("Electric Shock", 1.2),
new Ability("Fire Blast", 1.5),
new Ability("Water Jet", 0.8)
];
const pikachu = new Creature("Pikachu", 50, 15, [abilities[0]]);
const charmander = new Creature("Charmander", 40, 20, [abilities[1]]);
function battle(attacker, defender) {
addToBattleLog(`--- ${attacker.name} is attacking ${defender.name} ---`);
attacker.animateAttack();
if (Math.random() < 0.8) {
attacker.useAbility(defender);
} else {
defender.takeDamage(attacker.attack);
addToBattleLog(`${defender.name} takes ${attacker.attack} damage!`);
}
updateCreatureInfo(attacker);
updateCreatureInfo(defender);
if (defender.isDefeated()) {
addToBattleLog(`${defender.name} is defeated!`);
attacker.levelUp();
addToBattleLog(`${attacker.name} leveled up to level ${attacker.level}!`);
} else {
addToBattleLog(`${defender.name} has ${defender.health} HP left.`);
}
checkEndOfBattle();
}
function checkEndOfBattle() {
if (pikachu.isDefeated() || charmander.isDefeated()) {
addToBattleLog("The battle has ended.");
document.getElementById("replay-button").style.display = "block";
}
}
function gameLoop() {
addToBattleLog("Starting the battle...");
while (!pikachu.isDefeated() && !charmander.isDefeated()) {
const randomCreature = Math.random() < 0.5 ? pikachu : charmander;
const otherCreature = randomCreature === pikachu ? charmander : pikachu;
battle(randomCreature, otherCreature);
}
if (pikachu.isDefeated()) {
addToBattleLog(`${pikachu.name} has been defeated.`);
} else {
addToBattleLog(`${charmander.name} has been defeated.`);
}
}
function updateCreatureInfo(creature) {
const creatureInfo = document.getElementById(`${creature.name.toLowerCase()}-info`);
creatureInfo.textContent = `${creature.name}: Level ${creature.level}, HP: ${creature.health}`;
}
function addToBattleLog(message) {
console.log(message);
const battleLog = document.getElementById("battle-log");
battleLog.innerHTML += `<p>${message}</p>`;
battleLog.scrollTop = battleLog.scrollHeight;
}
const startButton = document.getElementById("start-button");
const replayButton = document.getElementById("replay-button");
startButton.addEventListener("click", () => {
gameLoop();
startButton.disabled = true;
});
replayButton.addEventListener("click", () => {
document.getElementById("battle-log").innerHTML = "";
startButton.disabled = false;
replayButton.style.display = "none";
pikachu.resetHealth(); // Reset Pikachu's health
charmander.resetHealth(); // Reset Charmander's health
});
</script>
</body>
</html>
Conclusion
Creating a Pokemon-inspired game with graphics and animations demonstrates the power of web technologies in crafting interactive and entertaining experiences. By combining HTML, CSS, and JavaScript, we’ve built a game that captures the essence of creature battles while adding an extra layer of visual engagement.
Whether you’re a gaming enthusiast or a developer looking to explore the world of interactive web applications, this project serves as a great starting point. You can further expand this game by adding more creatures, abilities, and features to create your own unique gaming experience. Happy coding and gaming!

Laisser un commentaire
Vous devez vous connecter pour publier un commentaire.