Certainly! Let's create a game called "Servant of Evil Goblins" where the player controls a servant tasked with cleaning up dirt left by goblins in a castle. The game has two possible endings:
Peaceful Ending: The servant successfully cleans the castle without reaching maximum rage by avoiding the goblins.
Combat Ending: The servant's rage reaches 100, and he retaliates by defeating the goblins with fireballs.
We'll develop this game using HTML5 Canvas and JavaScript. This approach allows the game to run in a web browser without the need for additional plugins.
Table of Contents
Game Overview
Technologies Used
Game Features
Development Steps
Set Up the HTML Structure
Style the Game with CSS
Game Assets
Initialize the Canvas and Game Loop
Create Game Objects
Servant (Player)
Goblins (Enemies)
Dirt (Mess to Clean)
Fireballs (Projectiles)
Implement Game Mechanics
Movement and Controls
Goblins' Movement
Rage Meter
Collision Detection
Cleaning Mechanism
Combat Mode
Game States and Endings
Main Game Loop
Putting It All Together
Final Notes
Running the Game
Possible Enhancements
Game Overview
The player controls the Servant, who must clean up dirt left behind by 10 Goblins in a castle. The Goblins move around randomly, leaving trails of dirt. When the Servant encounters a Goblin, his Rage Meter increases. If it reaches 100, the Servant enters Combat Mode, shooting fireballs at Goblins until they are all defeated.
Technologies Used
HTML5 Canvas: For rendering the game visuals.
JavaScript: For game logic and interactivity.
CSS: For styling the game elements.
Game Features
Player Movement: Move the servant using keyboard controls (arrow keys or WASD).
Rage Meter: Increases when the servant encounters goblins.
Goblins: Move around the map, leaving dirt trails.
Cleaning Mechanism: Clean dirt by moving over it.
Combat Mode: Activate when rage reaches 100, allowing the servant to fire projectiles at goblins.
Two Endings:
Peaceful: Clean all dirt without rage reaching 100.
Combat: Defeat all goblins after rage reaches 100.
Development Steps
Set Up the HTML Structure
Create an index.html file with a canvas element and a container for the rage meter.
body {
margin: 0;
overflow: hidden;
background-color: #2c3e50;
}
#rageMeter {
width: 0%;
height: 100%;
background-color: #e74c3c;
}
3. Game Assets
For simplicity, we'll represent the servant, goblins, dirt, and fireballs using basic shapes:
Servant: Blue rectangle.
Goblins: Green rectangles.
Dirt: Brown circles.
Fireballs: Orange circles.
You can replace these with images later if desired.
Assistant
Initialize the Canvas and Game Loop
Create a game.js file to initialize the game.
for (let i = 0; i < goblinCount; i++) {
goblins.push({
x: Math.random() * canvasWidth,
y: Math.random() * canvasHeight,
width: 20,
height: 20,
speed: 1,
color: 'green',
dirX: Math.random() < 0.5 ? 1 : -1,
dirY: Math.random() < 0.5 ? 1 : -1,
});
}
Dirt (Mess to Clean)
We'll store dirt in an array. Each dirt spot has an x and y.
const dirts = [];
Fireballs (Projectiles)
const fireballs = [];
6. Implement Game Mechanics
Movement and Controls
Add event listeners for keyboard input.
if (distX > (rect.width / 2 + circle.radius)) return false;
if (distY > (rect.height / 2 + circle.radius)) return false;
if (distX <= (rect.width / 2)) return true;
if (distY <= (rect.height / 2)) return true;
const dx = distX - rect.width / 2;
const dy = distY - rect.height / 2;
return dx * dx + dy * dy <= (circle.radius * circle.radius);
}
7. Game States and Endings
Add logic to check for game ending conditions.
function checkGameEnd() {
if (!combatMode && dirts.length === 0) {
// Peaceful ending
gameRunning = false;
alert('You have successfully cleaned the castle! Peaceful Ending Achieved.');
} else if (combatMode && goblins.length === 0) {
// Combat ending
gameRunning = false;
alert('You have defeated all the goblins! Combat Ending Achieved.');
}
}
8. Main Game Loop
Implement the update and draw functions.
function gameLoop() {
if (gameRunning) {
update();
draw();
requestAnimationFrame(gameLoop);
}
}
gameLoop();
Final Notes
Game Balance: Adjust the speeds, rates, and thresholds to balance the game's difficulty.
Graphics: Replace the basic shapes with images or sprites to enhance visual appeal.
Sound Effects: Add sounds for cleaning, goblin taunts, fireballs, and game endings.
User Interface: Improve the rage meter and add additional HUD elements if needed.
Collision Optimization: For better performance, especially with more objects, consider spatial partitioning techniques.
Running the Game
Create the files: index.html, styles.css, and game.js as shown above.
Open index.html in a modern web browser.
Play the game:
Move the servant using the arrow keys or WASD.
Avoid goblins to keep rage low.
Clean dirt by moving over it.
Engage in combat after rage reaches 100 by pressing spacebar to shoot fireballs.
Achieve one of the endings based on your actions.
Possible Enhancements
Levels: Introduce multiple levels with increasing difficulty.
Power-ups: Add items that reduce rage or enhance cleaning speed.
Goblin Behavior: Make goblins react to the servant or have different movement patterns.
Obstacles: Include obstacles in the castle that the servant and goblins must navigate around.
Mobile Support: Add touch controls for playing on mobile devices.
Storyline: Incorporate a narrative explaining why the servant is cleaning and why goblins mock him.
By following these steps, you've created a simple yet engaging game that meets the requested features. Feel free to expand and customize the game to make it more fun and challenging!
Happy coding! If you need further assistance or have questions about specific parts of the code, feel free to ask.
There is an error. When the game starts, it immediately says "You have successfully cleaned the castle! Peaceful Ending Achieved." Fix it!
I apologize for the issue you're experiencing. The problem occurs because, at the start of the game, there is no dirt (dirts.length === 0), and the combatMode is false. As a result, the condition for the peaceful ending is immediately met, causing the game to end right away.
To fix this, we need to ensure that the game doesn't check for the peaceful ending until at least one piece of dirt has been generated. Here's how you can modify your code to resolve this issue:
1. Add a Variable to Track Dirt Generation
At the beginning of your game.js file, add a new variable called dirtGenerated and initialize it to false. This variable will help us determine whether any dirt has been generated yet.
let dirtGenerated = false;
2. Update the Goblins' Dirt Generation Logic
In the moveGoblins() function, set dirtGenerated to true whenever a goblin leaves dirt. This change ensures that the game knows when dirt has been generated.
function moveGoblins() {
goblins.forEach(goblin => {
goblin.x += goblin.speed * goblin.dirX;
goblin.y += goblin.speed * goblin.dirY;
// Change direction on collision with walls
if (goblin.x <= 0 || goblin.x >= canvasWidth - goblin.width)
goblin.dirX *= -1;
if (goblin.y <= 0 || goblin.y >= canvasHeight - goblin.height)
goblin.dirY *= -1;
// Leave dirt
if (Math.random() < 0.02) {
dirts.push({ x: goblin.x + goblin.width / 2, y: goblin.y + goblin.height / 2 });
dirtGenerated = true; // Set to true when dirt is generated
}
});
}
3. Modify the Game Ending Condition
In the checkGameEnd() function, update the condition for the peaceful ending to include a check for dirtGenerated. This modification ensures that the peaceful ending only triggers after at least one piece of dirt has been generated and subsequently cleaned.
function checkGameEnd() {
if (!combatMode && dirts.length === 0 && dirtGenerated) {
// Peaceful ending
gameRunning = false;
alert('You have successfully cleaned the castle! Peaceful Ending Achieved.');
} else if (combatMode && goblins.length === 0) {
// Combat ending
gameRunning = false;
alert('You have defeated all the goblins! Combat Ending Achieved.');
}
}
By making these changes, the game will no longer end immediately upon starting. Instead, it will wait until the goblins have generated dirt, and you've cleaned it up before triggering the peaceful ending.
Here's the Updated game.js File with the Fix Applied:
Introduced dirtGenerated Variable: We added dirtGenerated to track whether any dirt has been created by the goblins.
Updated Dirt Generation Logic: When a goblin leaves dirt in moveGoblins(), we set dirtGenerated to true.
Modified Peaceful Ending Condition: In checkGameEnd(), we now check if dirtGenerated is true before triggering the peaceful ending. This change ensures that the game doesn't end prematurely.
Additional Notes
Testing the Game: After applying these changes, test the game by moving the servant around. Wait for the goblins to generate dirt, clean it up, and ensure that the peaceful ending triggers appropriately.
Game Balance: If you find that dirt is generated too slowly or too quickly, you can adjust the dirt generation probability in moveGoblins(). For example, changing Math.random() < 0.02 to a higher value like Math.random() < 0.05 will increase the frequency of dirt generation.
Starting with Initial Dirt (Optional): Alternatively, if you'd like the game to start with some dirt already present, you can initialize the dirts array with some values.
// Example: Starting with 5 pieces of dirt
for (let i = 0; i < 5; i++) {
dirts.push({
x: Math.random() * (canvasWidth - 10) + 5,
y: Math.random() * (canvasHeight - 10) + 5,
});
}
dirtGenerated = true; // Set to true since dirt now exists
Now, the game should run as intended, allowing you to experience the gameplay without it ending immediately. If you encounter any more issues or have additional questions, please feel free to ask!
Happy coding and enjoy your game development journey!