Oto przykładowy kod źródłowy gry Tetris napisany w języku Go:
package main
import (
"fmt"
"time"
"github.com/nsf/termbox-go"
)
const (
width = 10
height = 22
)
var (
playfield [height][width]bool
ticker *time.Ticker
quitChan chan struct{}
piece pieceData
)
type pieceData struct {
x, y int
shape [4][4]bool
color termbox.Attribute
active bool
}
func main() {
err := termbox.Init()
if err != nil {
panic(err)
}
defer termbox.Close()
termbox.SetInputMode(termbox.InputEsc)
quitChan = make(chan struct{})
ticker = time.NewTicker(time.Millisecond * 500)
piece = generatePiece()
go gameLoop()
loop:
for {
select {
case <-quitChan:
break loop
case ev := <-termbox.PollEvent():
if ev.Type == termbox.EventKey && ev.Key == termbox.KeyEsc {
close(quitChan)
} else if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowLeft {
movePiece(-1, 0)
} else if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowRight {
movePiece(1, 0)
} else if ev.Type == termbox.EventKey && ev.Key == termbox.KeyArrowDown {
movePiece(0, 1)
} else if ev.Type == termbox.EventKey && ev.Key == termbox.KeySpace {
rotatePiece()
}
}
}
fmt.Println("Game over!")
}
func gameLoop() {
for {
select {
case <-ticker.C:
if !movePiece(0, 1) {
mergePiece()
linesCleared := clearLines()
fmt.Println("Cleared", linesCleared, "lines")
piece = generatePiece()
if !canMove(piece.x, piece.y) {
close(quitChan)
}
}
case <-quitChan:
return
}
drawGame()
}
}
func movePiece(dx, dy int) bool {
if canMove(piece.x+dx, piece.y+dy) {
piece.x += dx
piece.y += dy
return true
}
return false
}
func rotatePiece() {
newShape := [4][4]bool{}
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
newShape[i][j] = piece.shape[3-j][i]
}
}
if canMove(piece.x, piece.y) {
piece.shape = newShape
}
}
func canMove(newX, newY int) bool {
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if piece.shape[i][j] {
x := newX + j
y := newY + i
if x < 0 || x >= width || y >= height || (y >= 0 && playfield[y][x]) {
return false
}
}
}
}
return true
}
func mergePiece() {
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if piece.shape[i][j] {
x := piece.x + j
y := piece.y + i
if y >= 0 {
playfield[y][x] = true
}
}
}
}
}
func clearLines() int {
linesCleared := 0
for i := height - 1; i >= 0; i-- {
full := true
for j := 0; j < width; j++ {
if !playfield[i][j] {
full = false
break
}
}
if full {
linesCleared++
for k := i; k > 0; k-- {
copy(playfield[k][:], playfield[k-1][:])
}
}
}
return linesCleared
}
func drawGame() {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
if playfield[i][j] {
termbox.SetCell(j*2, i, ' ', termbox.ColorDefault, termbox.ColorGreen)
termbox.SetCell(j*2+1, i, ' ', termbox.ColorDefault, termbox.ColorGreen)
} else {
termbox.SetCell(j*2, i, ' ', termbox.ColorDefault, termbox.ColorDefault)
termbox.SetCell(j*2+1, i, ' ', termbox.ColorDefault, termbox.ColorDefault)
}
}
}
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if piece.shape[i][j] {
termbox.SetCell((piece.x+j)*2, piece.y+i, ' ', termbox.ColorDefault, piece.color)
termbox.SetCell((piece.x+j)*2+1, piece.y+i, ' ', termbox.ColorDefault, piece.color)
}
}
}
termbox.Flush()
}
func generatePiece() pieceData {
shapes := [][][]bool{
{
{false, true, false, false},
{false, true, false, false},
{false, true, false, false},
{false, true, false, false},
},
{
{false, false, false, false},
{false, true, true, false},
{false, true, true, false},
{false, false, false, false},
},
{
{false, false, false, false},
{false, true, false, false},
{false, true, true, true},
{false, false, false, false},
},
{
{false, false, false, false},
{false, false, true, false},
{false, true, true, true},
{false, false, false, false},
},
{
{false, false, false, false},
{false, true, true, true},
{false, false, true, false},
{false, false, false, false},
},
{
{false, false, false, false},
{false, false, true, false},
{false, true, true, true},
{false, false, false, false},
},
{
{false, false, false, false},
{false, true, true, false},
{false, true, true, false},
{false, false, false, false},
},
}
colors := []termbox.Attribute{
termbox.ColorRed,
termbox.ColorBlue,
termbox.ColorMagenta,
termbox.ColorGreen,
termbox.ColorYellow,
}
shapeIndex := rand.Intn(len(shapes))
colorIndex := rand.Intn(len(colors))
return pieceData{
x: width / 2,
y: 0,
shape: shapes[shapeIndex],
color: colors[colorIndex],
active: true,
}
}
Powyższy kod używa biblioteki termbox-go, która dostarcza funkcje do tworzenia prostych konsolowych interfejsów użytkownika. Aby uruchomić ten kod, musisz zainstalować tę bibliotekę używając polecenia:
go get -u github.com/nsf/termbox-go
Następnie możesz skompilować i uruchomić kod przy użyciu polecenia:
go run tetris.go
Gra zostanie uruchomiona w konsoli, a sterowanie jest możliwe za pomocą strzałek i spacji.