Skip to content

About

A complete Tetris for Windows written in 32-bit x86 assembly

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tetris in x86 assembly

A complete Tetris for Windows written in 32-bit x86 assembly (FASM). No C runtime, no frameworks, no external libraries — just kernel32, user32, gdi32 and winmm. One self-contained tetris.exe, no installer, no DLLs.

Tetris

The user interface is available in 16 languages, chosen when you assemble the program rather than at runtime — no language switcher, no extra bytes in the binary.

Gameplay

Board 10 × 20 cells
Randomiser 7-bag (Fisher–Yates shuffle, xorshift32 PRNG)
Rotation 4 states per piece with a 6-entry wall-kick table
Gravity NES frame table, 48 down to 1 frame per step at 60 fps
Lock delay 500 ms, up to 15 resets
Scoring [0, 40, 100, 300, 1200] × (level + 1), level = lines / 10
Soft / hard drop +1 and +2 points per row
Autorepeat DAS 150 ms, ARR 45 ms
Extras ghost piece, line-clear flash, next-piece preview, sound

Controls

Key Action
← → move
↑ or X rotate clockwise
Z rotate counter-clockwise
↓ soft drop
Space hard drop
P / Esc pause
G toggle ghost piece
M toggle sound
Enter / Space start / restart

The overlay button can also be clicked with the mouse.

High scores

Reaching the top 10 prompts for a name (up to 12 characters). The table is shown on the start and game-over screens, with the new entry highlighted.

Scores live in a plain text file, tetris.top, next to the executable — exactly ten lines, numbered 1 to 10:

 1. John                208
 2. Denis               208
 3. Smoke               200
 4. -                    0

Edit it by hand or delete it and a fresh one is written. Names are stored as UTF-8, so non-ASCII names survive a round trip.

Building

Requirements: Windows. That is all. FASM 1.73.32 is bundled in fasm\, so nothing has to be installed and nothing outside the project folder is used. Clone or copy the directory anywhere and run:

compile.bat

compile.bat

Pick a language by number or by code. Enter builds English, Q quits. A wrong entry says so and asks again. The output is always tetris.exe.

To skip the menu:

compile.bat uk

Or invoke the assembler yourself, from the project directory:

set INCLUDE=%CD%\fasm\INCLUDE;%CD%\lang\uk
fasm\FASM.EXE tetris.asm tetris.exe

If the assembler reports write failed, close the running tetris.exe.

How the language selection works

tetris.asm contains no user-visible text at all. Every string lives in lang\<code>\lang.inc, and the source pulls one in with a single line:

include 'lang.inc'

compile.bat points the INCLUDE environment variable at the chosen lang\<code> directory, so FASM resolves that one line to the translation you picked. Nothing else in the source changes between languages.

Included languages

en English · zh Chinese (Simplified) · es Spanish · fr French · pt Portuguese · ru Russian · id Indonesian · de German · ja Japanese · tr Turkish · lt Lithuanian · lv Latvian · et Estonian · pl Polish · uk Ukrainian · be Belarusian

CJK text renders through GDI font linking — Consolas has no CJK glyphs, so Windows substitutes a fallback font automatically.

Adding a language

Copy lang\en\lang.inc to lang\<code>\lang.inc and translate the strings. It appears in the menu immediately; compile.bat scans the lang\ directory and skips empty files. Nothing in the build script needs editing.

Rules for a translation file:

  • Keep the labels and their order (s_score, s_level, …) — the source indexes them by position.
  • The first line, ; NAME=…, is the name shown in the build menu. Use ASCII so the console does not mangle it.
  • Write in UTF-8. Strings are converted to UTF-16 at startup, so any alphabet works regardless of the system code page.
  • Key-help lines fit about 20 characters. The pause title is drawn at 34 px (≤ 13 characters); the game-over and new-record titles at 26 px (≤ 18 characters).

Project layout

tetris.asm            the whole program, no user-visible text
lang\<code>\lang.inc  UI strings, one directory per language
tetris.ico            application icon, compiled into the exe
fasm\                 bundled FASM 1.73.32 (FASM.EXE, INCLUDE, LICENSE.TXT)
compile.bat           build script with the language menu
img\                  screenshots for this file

tetris.top is created next to the executable on the first high score.

Implementation notes

  • Rendering. Everything is drawn into an off-screen bitmap (CreateCompatibleDC + CreateCompatibleBitmap) and blitted to the window in a single BitBlt, so there is no flicker and no partial frames.
  • Pieces. Each rotation state is a 16-bit mask over a 4×4 grid, bit r*4+c. All seven pieces in all four rotations take 56 bytes.
  • Text. The window is Unicode (RegisterClassExW / DefWindowProcW). Source strings are UTF-8, converted once at startup with MultiByteToWideChar and drawn with DrawTextW. Name entry reads WM_CHAR as UTF-16, so any keyboard layout works.
  • Timing. timeBeginPeriod(1) plus timeGetTime(); the frame is capped at roughly 60 fps and the delta is clamped to 100 ms so nothing falls through the board after a pause or a drag.
  • Sound. Beep on a worker thread fed by a ring buffer, so the game loop never blocks on audio.
  • Sections. Uninitialised data sits in its own .bss section and is not stored in the file. The icon is a proper .rsrc resource, so Explorer and the title bar both use it.
  • Placement. The window opens centred on the work area — the screen minus the taskbar — via SystemParametersInfo(SPI_GETWORKAREA).

Code and data come to under 13 KB; everything above that in tetris.exe is the embedded icon. tetris.ico is a single 256×256 PNG frame, so Windows scales it down for the title bar and the taskbar — an icon that also carries 16, 32 and 48 px frames would be both smaller and sharper.

Runs on Windows XP or later. The icon is PNG-compressed, which Windows Vista and later render; on XP the window falls back to the default icon and everything else works normally.

License

The bundled assembler in fasm\ is flat assembler by Tomasz Grysztar, distributed under its own license — see fasm\LICENSE.TXT, which must be kept with it. It covers the assembler only, not the programs you build with it.

tetris.asm code is The Unlicense / CC0

About

A complete Tetris for Windows written in 32-bit x86 assembly

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Contributors

Languages