GoTris
Introduction
A language a year keeps the doc away!
The famous block game written in Go compiled to WASM. This is significant because this implies that you can run Go programs in the browser.
It is a quick port from TypeTris.
Play it! Main source code Download ZIP
Controls
Use cursor keys ▲ ▶ ▼ ◀ SPACE or I J K L M SPACE to control the game.
You need a recent version of Chrome, Firefox, (non-legacy) Edge or Safari to play it.
Build
Use build.sh to build it.
Go version 1.14 is needed because
of WASM support. Although syscall/js
is available from 1.11 on.
However the backward compatibility promise does not applies to this package
(fortunately, because there was some major simplification between 1.11 and 1.14).
This implies that the code may be broken in versions other then 1.14. I try
to keep this up-to-date.
What did I learn?
I learned to love channels again!
Debugging was - due to the absence of tools - not a breeze, but the good thing is that the Golang logger logs to the browser console and in case of errors full stack traces are available in the console too.
WASM
Also this was my first real WASM project. To make this portable I abstracted the renderer (browser.go) and it is desired to do this for the keyboard handler too.
Golang package size
The resulting WASM binary is ~1.5 MB (gzipped: 460 KB). I used these packages:
package | size |
---|---|
log | ~400 KB |
math/rand | ~20 KB |
time | ? KB (to be measured) |
I omitted the log file from the production version so it saves some bytes. Omitting the math/rand
packages was not worth it. If you really want you can use something like date.Nanoseconds % len(blocks)
to get a random block. Maybe give TinyGo a go for a smaller WASM file?
Source code
Name | Type | Last modified | Size | |
---|---|---|---|---|
blocks.go | Golang | Mar ’20 | 1.6 kB | Preview Download |
browser.go | Golang | Mar ’20 | 2.6 kB | Preview Download |
build.sh | Shell script | Mar ’20 | 178 bytes | Preview Download |
go.mod | Go module | Aug ’23 | 23 bytes | Preview Download |
gotris-big.png | PNG image | Mar ’20 | 12.4 kB | Preview Download |
gotris-small.png | PNG image | Mar ’20 | 6.1 kB | Preview Download |
gotris.css | Stylesheet | Mar ’20 | 103 bytes | Preview Download |
gotris.go | Golang | Mar ’20 | 4.7 kB | Preview Download |
gotris.html | HTML | Mar ’20 | 468 bytes | Preview Download |
gotris.js | JavaScript | Mar ’20 | 817 bytes | Preview Download |
gotris.png | PNG image | Mar ’20 | 12.4 kB | Preview Download |
gotris.wasm | WASM binary | Aug ’23 | 1.6 MB | Preview Download |
gotris.zip | Zip archive | Mar ’20 | 485.8 kB | Preview Download |
render.go | Golang | Mar ’20 | 180 bytes | Preview Download |
serve.sh | Shell script | Aug ’23 | 22 bytes | Preview Download |
tetris.css | Stylesheet | Oct ’17 | 2.9 kB | Preview Download |
wasm_exec.js | JavaScript | Aug ’23 | 16.6 kB | Preview Download |
17 files | 2.1 MB |