Game Design Studio โ€บ Procedural Generation
๐Ÿ—ก๏ธ New ยท Intermediate

Procedural Generation

What if you could teach a computer to build endless worlds, create infinite treasure, and design a dungeon that goes on forever? Let's do it! ๐Ÿš€

Endless Possibilities ๐ŸŽฒ

Have you ever noticed how every world in Minecraft is different? Or how dungeons in Hades change every time you play? Thatโ€™s Procedural Generation (or ProcGen)! ๐Ÿ—บ๏ธ

Instead of a human drawing every tree, we write rules and let the computer build the world for us. Itโ€™s like teaching a robot to be creative! Let's try it.

Let's create a brand new adventure!

How Does It Work? ๐Ÿค”

The simplest way to understand ProcGen is to imagine the computer has "magic boxes" full of words. It just grabs one from each box and sticks them together! You just played with a treasure generator right now.

Click the button to generate a new item!

Why Bother with ProcGen?

Imagine trying to build a world as big as Minecraft by hand. It would take a huge team of artists decades! With ProcGen, a small team can write a set of smart rules to generate a massive, unique, and replayable world automatically. It's working smarter, not harder, and it's what makes "infinite" games possible.

  • โœ๏ธ By Hand: One artist, one map. Takes forever! ๐Ÿ˜ฉ
  • ๐Ÿค– With ProcGen: One coder, INFINITE maps! โœจ๐Ÿคฏ
๐ŸŒฑ Curious how Minecraft worlds are shared?

Pro-Tip: The Magic of "Seeds"

Ever wonder how you can share a specific Minecraft world with a friend using just a few numbers? That's a 'seed'! It's a starting number for the random generator. If two computers use the same rules AND the same seed, they'll create the exact same 'random' world. This is how game developers can share, test, and replay their generated worlds.

๐Ÿ›ก๏ธ Safety Check: Using Your Powers for Good

Procedural generation, especially with AI, is like letting a "chaos monster" build your levels. It's awesome, but you're the boss! Always check what it creates to make sure it's fair, fun, and doesn't have any weird or unkind stuff. When you use AI to generate art or text, itโ€™s also important to be a thoughtful creator. Ask yourself: "Is this art really mine?" and "Why does the AI draw things this way?" Thinking about these questions helps you use your powers for good!

And remember the #1 rule for any AI tool: Never share personal information. Don't type your real name, your school, or your address. Keep your prompts about your game world, not your real world. Stay safe, explorer!

The 'Magic Boxes' used simple lists we gave it. But what if a computer could come up with its own ideas for the lists? That's AI! Let's talk to an Imagination Machine. ๐Ÿค–

๐Ÿ› ๏ธ Let's Build a Loot Generator!

Now let's use a more powerful tool. An AI can mix and match ideas that are way more creative than just picking words from a list. Tell our AI Assistant what kind of game you're making, and it will procedurally generate a loot table for you!

Need an idea? Try one of these!

Your procedurally generated loot will appear here...
๐Ÿ”Ž For Curious Coders: Look Inside the Machine!
๐Ÿคซ Curious about our AI's secret instructions?

"You are an RPG game designer. Generate a fun, kid-friendly JSON array of 5 procedurally generated items based on the user's game idea. Include the item name, a silly description, and a wacky drop rate percentage. Format it nicely with emojis."

This is called a 'System Prompt.' It's a hidden instruction that guides the AI. Learning to write good system prompts is a super-powerful skill called 'Prompt Engineering'!

๐Ÿ”ฌ Peek Behind the Curtain: How does our site talk to the AI?

When you click the button, our website sends a message to a server over the internet. This is called an API call. The message includes your prompt and our secret instructions. The server asks the AI model for a response, gets it back, and sends it to your browser to be displayed! It looks something like this in code:

async function getAiLoot(userPrompt) {
  const response = await fetch("https://api.tomorrowhub.ai/generate", {
    method: "POST",
    body: JSON.stringify({
      prompt: userPrompt
    })
  });
  const lootData = await response.json();
  return lootData;
}
                            

๐Ÿš€ Go Pro: Talk to a Real-World API

You just saw how our site uses an API. Real developers use tools like Hoppscotch to test APIs without writing any code. It's like a playground for talking to servers! The Pokรฉmon Company has a free API for all 1000+ Pokรฉmon.

Request
GET
https://pokeapi.co/api/v2/pokemon/charizard
Send

Your Mission: Visit Hoppscotch, paste in the URL https://pokeapi.co/api/v2/pokemon/charizard, and hit "Send" to see the real JSON data for Charizard. This is a real skill developers use every single day.

๐Ÿ“œ Quest Alert: The Clumsy Alchemist!

Grizelda the Alchemist just spilled all her potions! ๐Ÿ˜ฑ She needs your help to invent three new ones for her shop. Your mission is to write one perfect prompt for the Loot Generator above. Make sure your prompt asks the AI to create potions that have:

  • โœ… A funny name
  • โœ… A weird side-effect
  • โœ… A super gross ingredient

Use the Loot Generator to help Grizelda, then copy your best prompt below!

๐Ÿš€ Level Up: Explore No-Code Generators

See how the AI mixed and matched ideas? You can build your OWN generator that does the exact same thing, no AI needed! The Perchance.org tool below uses the same 'magic box' idea we saw earlier. Let's try it.

The code on the left is a recipe. The diagram shows how it picks one word from each list to make a new item. Simple, right?

adjective
+
material
+
item
=
[adjective] [material] [item]

๐Ÿ› ๏ธ Your Turn: Click "Try This Code Live" and add a new item to the 'material' list. How about 'starlight' or 'jelly'? See how it instantly becomes part of the generator!

// This is our recipe's name!
loot
  [adjective.titleCase] [material.titleCase] [item.titleCase]

// These are our ingredient boxes!
adjective
  flaming
  spooky
  gigantic

material
  iron
  slime
  diamond

item
  sword
  helmet
  banana
Try This Code Live on Perchance! โ†—๏ธ

Pro-Tip: You're Learning Real Code!

The Perchance syntax is a simplified version of a real programming concept called a 'template literal' or 'f-string'. The code [adjective] [material] [item] is a simple way of writing what a pro coder might write in JavaScript as `${adjective} ${material} ${item}`. You're learning real programming concepts without even realizing it!

๐Ÿš€ Advanced Mission: Code Your Own Generator in JavaScript

Ready to go beyond other tools? Your mission is to build the "magic boxes" generator yourself using JavaScript! Programmers build small 'helper' scripts like this all the time to quickly generate placeholder data or test ideas without needing a complex tool. It's a fundamental coding skill.

Your Mission: Below is the empty shell of a function. Copy it, fill in the blanks, and then update the button's `onclick` to call your new function: `runMyGenerator()`.

function runMyGenerator() {
  // TODO: Get the text from the 'js-box1' textarea and split it into a list
  const list1 = document.getElementById('js-box1').value.trim().split('\n');
  // TODO: Get the text from the other two boxes
  const list2 = document.getElementById('js-box2').value.trim().split('\n');
  const list3 = document.getElementById('js-box3').value.trim().split('\n');

  // TODO: Pick one random item from each list
  const word1 = list1[Math.floor(Math.random() * list1.length)];
  const word2 = list2[Math.floor(Math.random() * list2.length)];
  const word3 = list3[Math.floor(Math.random() * list3.length)];

  // TODO: Display the result in the 'js-output' paragraph
  document.getElementById('js-output').textContent = `${word1} ${word2} ${word3}`;
}
        
Hint: How to update the button?

In your browser's dev tools, you can select the button element and change its `onclick` attribute from the alert to `runMyGenerator()`. Or you could write a line of JavaScript in the console: `document.getElementById('my-generator-button').onclick = runMyGenerator;` (after giving the button that ID, of course!)

Your creation will appear here...

๐Ÿ”ฅ Stretch Goal: Add Item Rarity!

Making an "Epic" item appear as often as a "Common" one is boring. Real games use weighted rarity. Can you modify your code to make "Epic" items much rarer than "Common" ones?

Hint: Instead of picking from `['Common', 'Rare', 'Epic']`, what if you picked from a list that looks like this?

const rarityPool = [
  'Common', 'Common', 'Common', 'Common', 'Common', // 5 chances
  'Rare', 'Rare', // 2 chances
  'Epic' // 1 chance
];
const chosenRarity = rarityPool[Math.floor(Math.random() * rarityPool.length)];
// Now use chosenRarity to set the color!
            

Your Turn

Modify your `runMyGenerator` function to use a rarity pool. Based on the rarity, change the color of the output text. (e.g., `output.style.color = 'grey'` for Common, `'#60a5fa'` for Rare, `'#c084fc'` for Epic).

๐Ÿš€ Boss Level: Summon a Pokรฉmon with JavaScript

You've seen how to use an API with Hoppscotch. Now let's do it with real code! Your final mission is to use the `fetch()` command to get data from the PokeAPI right here in your browser.

Open your browser's developer console (usually by pressing F12 or Right-Click โ†’ Inspect), paste this code in, and press Enter. What happens?

// Your Mission: Get the name of a Pokรฉmon and log it to the console!
async function getPokemon(name) {
  const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`);
  const data = await response.json();
  console.log("You summoned:", data.name);
  // Now, can you figure out how to log its primary type?
  // Hint: Look at the data object you get back in the console!
}

getPokemon('pikachu');
        

This is a direct bridge from our lesson to a fundamental, real-world web development skill. It's the perfect final challenge that lets you build something you can actually show off.

Beyond Gaming ๐ŸŽฌ

This idea of using rules to generate content isn't just for games! It's a superpower used everywhere:

  • Movies & CGI: To create realistic forests or huge crowds. See how it was used to build armies in The Lord of the Rings!
  • Art: "Generative artists" write code that creates beautiful images. Explore some amazing examples in the p5.js examples gallery.
  • Data Science: To create safe, "synthetic" data for testing new programs without using anyone's real information.

๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ง Parent Corner

This isn't just about video games; it's a huge idea about how creativity, rules, and randomness work together. Exploring it with your child is a great way to build worlds and make connections, both on-screen and off.

๐ŸŽฒ Parent & Child Challenge: The Dice Maze!

Here's a fun, no-screen activity! Grab a piece of graph paper and a six-sided die.

  1. Draw a start and end point on the paper.
  2. For each square, roll the die. If you roll a 1 or 2, draw a wall in that square. If you roll anything else, leave it empty.
  3. Keep going until you've built a whole maze. Can you solve it together?

This is exactly how simple procedural generation works: rules + randomness = a unique creation! For a digital next step, you could try building your paper maze in a simple game engine like GDevelop.