create a fun rogue-like game with a procedurally generated map with a TON of features…
- User
create a fun rogue-like game with a procedurally generated map with a TON of features that you can add! using HTML5
- Assistant
create a fun rogue-like game with a procedurally generated map with a TON of features that you can add! using HTML5
Sure! Let's create a roguelike game with a procedurally generated map using HTML5. We'll start with a basic framework and include plenty of features that you can expand upon. This game will feature:
Let's get started!
Create an index.html file with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Roguelike Game</title>
<style>
body {
background-color: #000;
margin: 0;
}
canvas {
display: block;
margin: 0 auto;
background-color: #111;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="640" height="480"></canvas>
<script src="game.js"></script>
</body>
</html>
This sets up the basic HTML structure with a canvas where we'll render the game.
Create a game.js file and include the following code:
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game settings
const TILE_SIZE = 32;
const MAP_WIDTH = Math.floor(canvas.width / TILE_SIZE);
const MAP_HEIGHT = Math.floor(canvas.height / TILE_SIZE);
const PLAYER_COLOR = '#0f0';
const WALL_COLOR = '#555';
const FLOOR_COLOR = '#222';
// Map tiles
const TILE_WALL = 1;
const TILE_FLOOR = 0;
// Game variables
let map = [];
let rooms = [];
let player = { x: 0, y: 0 };
// Initialize the game
function init() {
generateMap();
placePlayer();
requestAnimationFrame(gameLoop);
}
// Map generation
function generateMap() {
// Create a 2D array filled with walls
map = Array.from({ length: MAP_HEIGHT }, () => Array(MAP_WIDTH).fill(TILE_WALL));
// Generate rooms
let roomCount = 10;
let roomMinSize = 3;
let roomMaxSize = 7;
for (let i = 0; i < roomCount; i++) {
let room = {
x: getRandomInt(1, MAP_WIDTH - roomMaxSize - 1),
y: getRandomInt(1, MAP_HEIGHT - roomMaxSize - 1),
width: getRandomInt(roomMinSize, roomMaxSize),
height: getRandomInt(roomMinSize, roomMaxSize)
};
if (!doesRoomOverlap(room)) {
createRoom(room);
if (rooms.length > 0) {
// Connect rooms
let prevRoom = rooms[rooms.length - 1];
connectRooms(prevRoom, room);
}
rooms.push(room);
} else {
i--; // Try again if the room overlaps
}
}
}
function doesRoomOverlap(newRoom) {
return rooms.some(room => (
newRoom.x < room.x + room.width &&
newRoom.x + newRoom.width > room.x &&
newRoom.y < room.y + room.height &&
newRoom.y + newRoom.height > room.y
));
}
function createRoom(room) {
for (let y = room.y; y < room.y + room.height; y++) {
for (let x = room.x; x < room.x + room.width; x++) {
map[y][x] = TILE_FLOOR;
}
}
}
function connectRooms(roomA, roomB) {
let pointA = {
x: getRandomInt(roomA.x, roomA.x + roomA.width),
y: getRandomInt(roomA.y, roomA.y + roomA.height)
};
let pointB = {
x: getRandomInt(roomB.x, roomB.x + roomB.width),
y: getRandomInt(roomB.y, roomB.y + roomB.height)
};
if (Math.random() < 0.5) {
carveHorizontalTunnel(pointA.x, pointB.x, pointA.y);
carveVerticalTunnel(pointA.y, pointB.y, pointB.x);
} else {
carveVerticalTunnel(pointA.y, pointB.y, pointA.x);
carveHorizontalTunnel(pointA.x, pointB.x, pointB.y);
}
}
function carveHorizontalTunnel(x1, x2, y) {
for (let x = Math.min(x1, x2); x <= Math.max(x1, x2); x++) {
map[y][x] = TILE_FLOOR;
}
}
function carveVerticalTunnel(y1, y2, x) {
for (let y = Math.min(y1, y2); y <= Math.max(y1, y2); y++) {
map[y][x] = TILE_FLOOR;
}
}
function placePlayer() {
let firstRoom = rooms[0];
player.x = firstRoom.x + Math.floor(firstRoom.width / 2);
player.y = firstRoom.y + Math.floor(firstRoom.height / 2);
}
// Game loop
function gameLoop() {
drawMap();
drawPlayer();
requestAnimationFrame(gameLoop);
}
// Drawing functions
function drawMap() {
for (let y = 0; y < MAP_HEIGHT; y++) {
for (let x = 0; x < MAP_WIDTH; x++) {
if (map[y][x] === TILE_WALL) {
ctx.fillStyle = WALL_COLOR;
} else {
ctx.fillStyle = FLOOR_COLOR;
}
ctx.fillRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
}
}
function drawPlayer() {
ctx.fillStyle = PLAYER_COLOR;
ctx.fillRect(player.x * TILE_SIZE, player.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
}
// Input handling
document.addEventListener('keydown', handleInput);
function handleInput(event) {
let newX = player.x;
let newY = player.y;
if (event.key === 'ArrowUp' || event.key === 'w') {
newY--;
} else if (event.key === 'ArrowDown' || event.key === 's') {
newY++;
} else if (event.key === 'ArrowLeft' || event.key === 'a') {
newX--;
} else if (event.key === 'ArrowRight' || event.key === 'd') {
newX++;
}
if (isWalkable(newX, newY)) {
player.x = newX;
player.y = newY;
}
}
function isWalkable(x, y) {
return map[y] && map[y][x] === TILE_FLOOR;
}
// Utility functions
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
// Start the game
init();
Map Generation
map represents the dungeon grid, initialized with walls (TILE_WALL).doesRoomOverlap checks if a new room overlaps with any existing room.connectRooms, carveHorizontalTunnel, and carveVerticalTunnel functions to ensure the dungeon is fully navigable.Player Setup
Game Loop
gameLoop function continuously redraws the map and the player position.handleInput function moves the player in response to arrow key or WASD inputs, checking for collision with walls.Here are some ideas for features that you can implement to expand the game:
Enemies: Create enemy characters that roam the dungeon. Implement AI for movement and interaction with the player.
Combat System: Develop a combat system where the player can attack enemies and vice versa, including health points and damage calculation.
Items and Inventory: Add items that the player can pick up, such as weapons, armor, potions, and treasure. Implement an inventory system to manage these items.
Fog of War: Introduce a visibility mechanic where areas of the dungeon are hidden until the player explores them.
Level Progression: Create multiple dungeon levels, increasing difficulty as the player descends. Implement stairs or portals to move between levels.
Character Stats: Implement RPG elements like experience points, leveling up, and stat improvements.
Traps and Obstacles: Place traps or puzzles in the dungeon that the player must navigate or deactivate.
NPC Interaction: Include non-hostile NPCs that the player can talk to for quests or information.
Save and Load: Implement a system to save and load game progress.
Sound and Music: Add background music and sound effects to enhance the atmosphere.
Here's how you might start adding enemies to the game:
Step 1: Define Enemy Objects
let enemies = [];
function createEnemies(count) {
for (let i = 0; i < count; i++) {
let room = rooms[getRandomInt(1, rooms.length)]; // Avoid starting room
let enemy = {
x: getRandomInt(room.x, room.x + room.width),
y: getRandomInt(room.y, room.y + room.height),
hp: 10
};
enemies.push(enemy);
}
}
Step 2: Render Enemies
Modify the drawMap function to include enemies:
function drawMap() {
// Existing code...
// Draw enemies
enemies.forEach(enemy => {
ctx.fillStyle = '#f00';
ctx.fillRect(enemy.x * TILE_SIZE, enemy.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
});
}
Step 3: Enemy Movement and Interaction
Implement simple AI for enemy movement and collision with the player.
This code provides a solid foundation for a roguelike game. From here, you can implement additional features to make the game more engaging and complex. Remember to incrementally test each new feature as you add it to keep your code manageable and to catch bugs early.
Have fun developing your game!
Not yet included in a published release.