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!
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
Control a LEGO Spike Robot with Motion: Using the Gyroscope

Control LEGO Spike Essential with Motion: Using a Gyroscope Introduce Today we are going to learn how to control a robot in a completely different way. Instead of pressing buttons or using a controller, we will use motion. The LEGO SPIKE hub has a built-in gyroscope that can detect how it is tilted, and we […]

Read More
Digestion Dash: The Ultimate Body Adventure!

Digestion Dash: The Ultimate Body Adventure! Get ready to race through the human body in Digestion Dash! In this game, you’ll follow food on an epic journey—from the first bite all the way to the end. Along the way, you’ll play mini-games and learn how your digestive system turns food into nutrients your body can […]

Read More
Build a LEGO Elevator!

Architecture Week Challenge: Build a LEGO Elevator! Can you design and build a working elevator using LEGO Robotics? This week at Sprattronics Learning Lab, your mission is to take your minifigures from low to high—just like real engineers do in skyscrapers around the world! Watch the Challenge Video First Before you start building, watch this […]

Read More
Can You Design a Video Game About Animal Senses?

Can You Design a Video Game About Animal Senses? Your Challenge Create a video game where the main character survives using its senses. You’ll need to: Your mission is to design a game that shows how animals sense, process information, and respond in order to survive. Why Animal Senses Make Great Video Games Animals experience […]

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