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!
Interactive Weather Game

Interactive Weather Game: Learn the Water Cycle by Controlling a Cloud If you’re looking for a hands-on way to teach the water cycle and basic weather science, this browser-based game offers an engaging, intuitive experience. Players control a cloud, collect water vapor, release rain, and watch plants grow as they explore how water moves through […]

Read More
LEGO Baseplate Builder

Free 16x16 LEGO® Plate Builder: Create Custom LEGO Challenge Cards Unlock your creativity and kickstart your next building session with the Sprattronics LEGO Plate Builder. This free digital tool is designed specifically for educators, parents, and LEGO fans to design 16x16 LEGO Challenge Cards in seconds. Whether you are looking for a quick STEM warmup or a way […]

Read More
Level 1 Scratch - Make a Flappy Bird Game

Scratch Coding Challenge: Build Flappy Bird In this challenge, you will build your own version of Flappy Bird in Scratch. Flappy Bird was one of the most downloaded mobile games in the early days of touchscreen devices. The idea is simple: This isn't meant to be cookbook coding. Great programmers learn by breaking problems into […]

Read More
Code a Mini Economy Town in Scratch

Development Guide: Code a Mini Economy Town in Scratch This guide explains how to build a dice-powered city simulator in Scratch. You will learn how to manage a virtual economy, use cloning to build a visual city, and create a multi-level progression system. You can check out this project on Scratch. Dicy City Scratch We […]

Read More
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. […]

Read More
Create a Downhill Skier Game

DOWNHILL SKIER GAME CODING CHALLENGE The Challenge Your mission is to create a downhill skiing game.The skier stays near the top of the screen while the world moves toward them. Goal:Avoid obstacles for as long as possible. This game uses a classic trick in game design:If the environment moves, it feels like the player is […]

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