master
Trevor Slocum 2 years ago
parent
commit
2 changed files with 98 additions and 9 deletions
1. 62
content/post/tetris-1.md
2. 45
content/post/tetris-2.md

62 content/post/tetris-1.md Unescape Escape View File

 @ -9,6 +9,13 @@ aliases: @@ -9,6 +9,13 @@ aliases: This is the first part of a series of tutorials on creating a [terminal-based](https://en.wikipedia.org/wiki/Text-based_user_interface) [Tetris](https://en.wikipedia.org/wiki/Tetris) clone with [Go](https://golang.org). Code for this tutorial is available [here](https://gitlab.com/tslocum/terminal-tetris-tutorial/-/tree/master/part-1). ```bash go get gitlab.com/tslocum/terminal-tetris-tutorial/part-1 # Download and install ~/go/bin/part-1 # Run ``` For a complete implementation of a Tetris clone in Go, see [netris](https://gitlab.com/tslocum/netris). # Disclaimer @ -38,7 +45,7 @@ Tetris is played on an [X-Y grid](https://en.wikipedia.org/wiki/Cartesian_coordi @@ -38,7 +45,7 @@ Tetris is played on an [X-Y grid](https://en.wikipedia.org/wiki/Cartesian_coordi so we will store minos as slices of points. ``` @ -47,7 +54,7 @@ so we will store minos as slices of points. @@ -47,7 +54,7 @@ so we will store minos as slices of points. \/\/\/\/\/ ``` @ -108,6 +115,35 @@ func (m Mino) String() string { @@ -108,6 +115,35 @@ func (m Mino) String() string { return b.String() } func (m Mino) Render() string { var ( w, h = m.Size() c = Point{0, h - 1} b strings.Builder ) for y := h - 1; y >= 0; y-- { c.X = 0 c.Y = y for x := 0; x < w; x++ { if !m.HasPoint(Point{x, y}) { continue } for i := x - c.X; i > 0; i-- { b.WriteRune(' ') } b.WriteRune('X') c.X = x + 1 } b.WriteRune('\n') } return b.String() } {{< / highlight >}} Origin returns a translated mino located at `0,0` and with positive coordinates @ -169,6 +205,20 @@ XXX X @@ -169,6 +205,20 @@ XXX X Flatten calculates the flattest side of a mino and returns a flattened mino. {{< highlight go >}} func (m Mino) Size() (int, int) { var x, y int for _, p := range m { if p.X > x { x = p.X } if p.Y > y { y = p.Y } } return x + 1, y + 1 } func (m Mino) Flatten() Mino { var ( w, h = m.Size() @ -351,6 +401,10 @@ We generate minos for the rank below the requested rank and iterate over the @@ -351,6 +401,10 @@ We generate minos for the rank below the requested rank and iterate over the variations of each mino, saving and returning all unique variations. {{< highlight go >}} func monomino() Mino { return Mino{{0, 0}} } func Generate(rank int) ([]Mino, error) { switch { case rank < 0: @ -385,10 +439,6 @@ func Generate(rank int) ([]Mino, error) { @@ -385,10 +439,6 @@ func Generate(rank int) ([]Mino, error) { return minos, nil } } func monomino() Mino { return Mino{{0, 0}} } {{< / highlight >}} # Stay tuned...

45 content/post/tetris-2.md Unescape Escape View File

 @ -9,6 +9,13 @@ aliases: @@ -9,6 +9,13 @@ aliases: This is the second part of a series of tutorials on creating a [terminal-based](https://en.wikipedia.org/wiki/Text-based_user_interface) [Tetris](https://en.wikipedia.org/wiki/Tetris) clone with [Go](https://golang.org). Code for this tutorial is available [here](https://gitlab.com/tslocum/terminal-tetris-tutorial/-/tree/master/part-2). ```bash go get gitlab.com/tslocum/terminal-tetris-tutorial/part-2 # Download and install ~/go/bin/part-2 # Run ``` For a complete implementation of a Tetris clone in Go, see [netris](https://gitlab.com/tslocum/netris). # Disclaimer @ -19,11 +26,43 @@ Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC. @@ -19,11 +26,43 @@ Rocket Nine Labs is in no way affiliated with Tetris Holding, LLC. ## Matrix The matrix is typically 10 blocks wide and 20 blocks high. In this part of the series, we will learn what the matrix is and how we could implement it in Go. The matrix is a playfield which holds tetrominos. It is typically 10 blocks wide and 20 blocks high. Initially empty, tetrominos will appear just outside of view at the top of the matrix, and will fall into place one by one. ``` \/\/\/\/\/ \/\/\/\/\/ ``` *Example coordinate positions and blocks in 10x20 playfield* ### Matrix data model A block is an integer representing the contents of a single X-Y coordinate on the matrix. A block is an integer representing the contents of a single X-Y Point (see part 1) on the matrix. We will use these blocks later to build tetrominos. {{< highlight go >}} type Block int @ -49,7 +88,7 @@ The buffer is additional space above the visible playfield. @@ -49,7 +88,7 @@ The buffer is additional space above the visible playfield. {{< highlight go >}} type Matrix struct { W int // Width H in // Height H int // Height B int // Buffer height M []Block // Contents