February 25, 2026

Code a Town Economy Game


Code a Mini Economy Town: A Development Guide

This guide breaks down the creation of Dice City Tycoon, a browser-based economic simulator. We developed this game in five distinct phases, moving from a simple text-based engine to a polished, multi-level city builder.

This page is all about building the game in HTML for a Web Browser. This is where I like to prototype my games before building a polished version in a game engine.

A Scratch Version of this Guide is available.


Phase 1: The Core Engine (Randomized Income)

Objective: Establish the "Game Loop." We needed a way for the player to spend money (Input) and earn it back through luck (Output).

  • How it was accomplished:
    • Data Structure: We used a JavaScript object (state) to track coins and buildings, and an array (buildingData) to define what each building does.
    • Randomness: We used Math.floor(Math.random() * 6) + 1 to simulate a 6-sided die.[1][2]
    • Logic: Every time the player rolls, the code loops through their buildings. If the roll matches a building's "trigger" number (e.g., a Wheat Field triggers on a 1), it adds coins to the total.

Phase 2: Visual Feedback (The Emoji City)

Objective: Make the game feel like a "City Builder" rather than just a spreadsheet. We wanted the player to see their progress physically.

  • How it was accomplished:
    • The City Window: We created a dedicated CSS div (the "green field") using Flexbox.
    • Dynamic DOM Injection: In the buyBuilding function, we added a line that creates a new <span> containing an emoji (🌾, ☕, ⛏️) and appends it to the city window.
    • CSS Animations: We used a @keyframes "pop-in" effect to make buildings bounce into existence, giving a satisfying sense of "impact" when buying.

Phase 3: Strategic Depth (Synergy Buildings)

Objective: Move beyond simple payouts and introduce strategy. We wanted buildings that "talk" to each other.

  • How it was accomplished:
    • Conditional Payouts: We added "Synergy Buildings" like the Flower Shop and Furniture Factory.
    • Dynamic Calculation: Instead of a flat payout number, we wrote a function that checks the current state. For example, the Flower Shop calculates: (Count of Shops) * (3 * Count of Flower Gardens).
    • Strategy Shift: This forced players to decide between "Reliable" income (Wheat Fields) or "High-Ceiling" combos (Flower Shop + Gardens).

Phase 4: The Progression System (Levels & Unlocks)

Objective: Prevent the player from being overwhelmed and create long-term goals.

  • How it was accomplished:
    • Level Gatekeeping: We added a minLvl property to each building. The "Market" code was modified to only show buildings if game.level >= building.minLvl.
    • The Level-Up Trigger: After every roll, a checkLevel() function compares the player's coins to a target goal (e.g., Level 1 goal = 12 coins).
    • The "Second Die" Mechanic: We locked the ability to roll two dice until Level 4. This created a major gameplay milestone that felt like a reward for mastering the early game.

Phase 5: Polish & "Juice" (Animations & Resets)

Objective: Make the game feel professional and implement a "Prestige" loop where the game gets harder but more rewarding.

  • How it was accomplished:
    • Coordinate Math: For the sliding coins, we used getBoundingClientRect(). This allowed JavaScript to find the exact pixel location of the dice and the pixel location of the coin counter on your screen.
    • Coin Flying: We created temporary "flying coin" elements and used CSS transitions to move them between those two coordinates.
    • The Great Reset: To keep the game challenging, we added a reset logic on level-up. When a goal is hit, the code clears the buildingsOwned object, empties the City Window HTML, and sets coins back to 5. This forces the player to use their newly unlocked buildings to reach the next, higher goal.

Final Project Architecture Summary

  1. HTML: Provides the skeleton (the city field and the control sidebar).
  2. CSS: Handles the "Vibe" (grass colors, dirt borders, and button styling).
  3. JavaScript: Acts as the brain, managing the level logic, coin math, and animations.

Key takeaway: By starting with a simple dice roll and layering on visuals, strategy, and then progression, you can build a complex simulation starting from just a few lines of code.

Sprattronics Learning Lab

Maximize your education!
Arctic Animal Science: Polar Bears

1. Introduction and Build Meet an Apex Predator Polar bears are the largest land predators on Earth. They live in the Arctic and the Northern Hemisphere, where they spend much of their time on sea ice and in the surrounding coastal areas. Polar bears are apex predators, which means they are at the top of […]

Read More
Arctic Animal Science: Reindeer Adaptations

1. Introduction & Build Welcome to the Arctic Tundra! The Arctic tundra is a challenging place to live. Temperatures can become extremely cold, snow can cover the ground, and finding food can become difficult. Yet reindeer survive here year after year. How do they do it? Reindeer have special adaptations that help them survive in […]

Read More
Arctic Animal Science : Walruses

Welcome to the Arctic Ocean! The Arctic Ocean is the smallest and shallowest of Earth's five oceans. It surrounds the North Pole and is one of the coldest ocean environments on Earth. Much of the Arctic Ocean is covered by sea ice, especially during the coldest parts of the year. Even though the environment is […]

Read More
Arctic Animals: Engineering in a Frozen World

Welcome to the Arctic! The Arctic is the far northern region of Earth. It surrounds the North Pole and includes everything north of the Arctic Circle, an imaginary line located at about 66.5° north latitude. The Arctic is one of the coldest places on Earth. Much of the land is treeless, and the region is […]

Read More
Get the Gold

Get the Gold! LEGO® Education Computer Science & AI Grade 3 - 5 Unit 1 • Lesson 1 Student Sheet Mission A treasure of gold is waiting! Your job is to build a horse and carriage, then write a program that helps your robot travel from the castle to the gold and safely back home […]

Read More
LEGO Education Computer Science & AI Kit: The Ultimate Getting Started Guide

LEGO Education Computer Science & AI Kit: The Ultimate Getting Started Guide You Placed Your Order and the Kit is on the Way... Now What? Congratulations! You've ordered the LEGO Education Computer Science & AI Set. Whether you're a classroom teacher, homeschool parent, after-school educator, or robotics enthusiast, you're about to unlock dozens of engaging […]

Read More
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram