Description

Godot 2048 PC — Classic Puzzle Game Source (Desktop Edition)

A classic 2048 game clone built on Godot 4.7+, designed for PC desktop in landscape (1280×720). Merge matching numbers until you reach 2048 — simple rules, addictive gameplay, and an elegantly structured Godot learning template with thorough comments.


🎯 Core Highlights

Designed for AI-Assisted Development

The entire project is built purely in code: main.tscn is only 6 lines (root node + script reference). Theme styles are generated in pure code by ThemeBuilder (replacing .tres files), and UI nodes are constructed at runtime by ViewBuilder — no .tscn / .tres resource black boxes. Reading the .gd files tells you everything.

  • No scene black boxes — Scene tree is built in code, ThemeBuilder replaces .tres, ViewConfig is the single source of truth
  • View handles input — GridView listens directly to keyboard/touch and emits move_requested signals, no Controller mediation needed
  • Object pool pattern — GridView reuses TileView instances via _dead/_live arrays, avoiding frequent node creation/destruction
  • Four universal principles — Everything is text, single-responsibility files, constant-driven layout, unidirectional dependencies

LV (Logic-View) Layered Architecture

Following the same Logic-View separation as Block Fall, Block Blast, and the mobile 2048 — replacing the common MVC pattern. main.gd is a thin 95-line coordinator:

Layer Responsibility Key Files
logic/ Pure game logic. Game / GameGrid / GameTile all extend RefCounted, no scene-tree dependency, unit-testable. SaveData handles high-score JSON persistence. game.gd game_grid.gd game_tile.gd save_data.gd
view/ Pure rendering + input handling. GridView listens to keyboard/touch → emits move_requested. Three animation states (spawn/move/merge) via Tween. grid_view.gd tile_view.gd view_config.gd theme_builder.gd view_builder.gd board_sizer.gd
main.gd Thin coordinator. ViewBuilder.build() constructs the UI → wires Logic ↔ View signals → Game.start() kicks off the game. —

Key insight: The entire Logic layer is RefCounted, so you can instantiate and test it directly without a scene tree — Game.new() + a fixed random seed = a reproducible test board.

Complete Test Coverage

The project ships with a lightweight test framework (no plugin dependencies), matching the mobile edition’s coverage:

  • Unit tests (tests/test_*.gd) — covers Game (movement/merging/scoring/win/lose/dead board), GameGrid (grid access), GameTile (data constraints), SaveData (score formatting)
  • Smoke test (tests/smoke_test.gd) — instantiates the real main scene and verifies the full flow
  • Regression tests — includes cases like the once-fixed bug where a full dead board must emit the over signal

Run them with:

godot --headless --path . --script res://tests/test_runner.gd   # Unit tests
godot --headless --path . --script res://tests/smoke_test.gd    # Smoke test

PC Landscape Adaptation

  • Landscape 1280×720, classic 2048 cream/brown color palette
  • Left-panel layout: title + scoreboard + new game button + how-to-play instructions
  • board_sizer.gd auto-fit board: keeps the board square and centered within available space
  • Keyboard arrow keys + mouse/touch swipes — both input methods supported
  • Three animation states (spawn/move/merge) with smooth 0.15s Tween transitions
  • Auto score formatting (999 / 1.0k / 1.0m), high score persisted as JSON

One-Click Recording Demo

The project includes tools/auto_demo.gd: starts a game with a fixed random seed, plays 80 moves using a simple greedy strategy, then exits — so you can record a 60fps clip in one command (fully reproducible).

godot --path . --write-movie frames/frame.png --fixed-fps 60 res://tools/auto_demo.tscn
ffmpeg -y -framerate 60 -i frames/frame%08d.png -c:v libx264 -preset slow \
       -crf 18 -pix_fmt yuv420p -movflags +faststart -an demo.mp4

🎮 Game Features

  • Classic 2048 gameplay — 4×4 grid, matching numbers merge and double, goal is 2048
  • Object-pool tile rendering — reuses tile nodes, avoiding frequent create/destroy overhead
  • Three animation states — spawn (center scale), move (smooth slide), merge (dual-source combine)
  • Auto font sizing — text shrinks automatically when numbers get large, always fully visible
  • Bit-operation color lookup — tile values map to an 11-level color array via bit operations, clever and efficient
  • Continue after winning — after reaching 2048, choose to keep playing for a higher score
  • JSON high-score save — auto-saves on window close

How to Play

  1. Use arrow keys or mouse/touch swipes to move all tiles
  2. Tiles with the same number merge into one when they touch
  3. After each move, a new tile (2 or 4) spawns at random
  4. Game ends when the board is full and no adjacent tiles share a value

📂 Project Structure

godot-2048-pc/
├── resources/                 # Static assets
│   └── fonts/                 #   JetBrainsMono font (regular + bold)
├── logic/                     # Game logic layer (Logic)
│   ├── game.gd                #   Game: movement/merging/scoring/win-lose (RefCounted pure logic)
│   ├── game_grid.gd           #   4×4 grid data structure
│   ├── game_tile.gd           #   Single tile data (value, position, merge sources)
│   └── save_data.gd           #   High-score JSON persistence (format_score formatting)
├── view/                      # Rendering layer (View)
│   ├── grid_view.gd           #   UI grid + object pool + Tween animations + input handling
│   ├── tile_view.gd           #   Tile drawing (bit-operation colors + auto font sizing)
│   ├── view_config.gd         #   View config constants (sizes, colors, fonts)
│   ├── theme_builder.gd       #   Theme factory (builds Theme in pure code)
│   ├── view_builder.gd        #   Scene builder (builds node tree in pure code)
│   └── board_sizer.gd         #   Board square auto-fit + centering (PC-exclusive)
├── tests/                     # Tests (no plugin dependencies, covers full Logic layer)
│   ├── test_runner.gd         #   Unit test runner
│   ├── smoke_test.gd          #   Smoke test
│   ├── test_base.gd           #   Assertion base class
│   ├── test_game.gd           #   Game core logic tests
│   ├── test_game_grid.gd      #   Grid data tests
│   ├── test_game_tile.gd      #   Tile data tests
│   └── test_save_data.gd      #   Score formatting tests
├── tools/                     # Helper tools
│   └── auto_demo.gd           #   Auto-demo script (fixed seed + greedy 80 moves)
├── docs/                      # Project documentation
│   ├── intro.md               #   This document (Chinese)
│   └── intro_en.md            #   English introduction
├── main.gd                    # Main coordinator (LV thin layer: 95 lines of signal wiring)
├── main.tscn                  # Main scene (only 6 lines: root node + script reference)
├── project.godot              # Engine configuration
├── export_presets.cfg         # Export preset configuration
└── LICENSE.md                 # Paid license (see LICENSE_en.md for English version)

🚀 Quick Start

Requirements

  • Godot 4.7+

Run

  1. Open the project directory with Godot
  2. Press F5 to run (or godot --path .)
  3. Use arrow keys or swipes to play

Build

  1. Adjust target platform presets in export_presets.cfg
  2. Use Godot’s export dialog to build for Windows / macOS / Linux / Web (HTML5 PWA)

👤 Who Is This For

  • Godot beginners — manageable code size (~700 lines of Logic + View), thorough comments, single-responsibility files
  • Architecture learners — LV separation + object pool + Tween animations + code-driven ThemeBuilder — multiple engineering patterns in one project
  • AI-assisted developers — pure code architecture aligns perfectly with AI tooling; changing colors/animations/layout is find-and-replace friendly
  • Indie game developers — classic 2048 gameplay, ready to ship or use as a starting point for derivatives (5×5 grid, new merge rules, etc.)
  • Compare with mobile 2048 / Block Fall / Block Blast — same architecture across three targets (mobile portrait + PC landscape) and four game types, from simple to complex

📄 License

This project uses a paid custom license. After purchase you may modify the code and ship derivative games, but you may not redistribute the source code or sell it commercially. See LICENSE.md (or LICENSE_en.md for English) for the full terms.

2048 PC Source Code

$9.99