/
view.go
126 lines (114 loc) · 3.05 KB
/
view.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"math"
"strings"
"github.com/nsf/termbox-go"
)
// Colors
const (
backgroundColor = termbox.ColorBlue
boardColor = termbox.ColorBlack
instructionsColor = termbox.ColorYellow
)
var pieceColors = []termbox.Attribute{
termbox.ColorBlack,
termbox.ColorRed,
termbox.ColorGreen,
termbox.ColorYellow,
termbox.ColorBlue,
termbox.ColorMagenta,
termbox.ColorCyan,
termbox.ColorWhite,
}
// Layout
const (
defaultMarginWidth = 2
defaultMarginHeight = 1
titleStartX = defaultMarginWidth
titleStartY = defaultMarginHeight
titleHeight = 1
titleEndY = titleStartY + titleHeight
boardStartX = defaultMarginWidth
boardStartY = titleEndY + defaultMarginHeight
boardWidth = 10
boardHeight = 16
cellWidth = 2
boardEndX = boardStartX + boardWidth*cellWidth
boardEndY = boardStartY + boardHeight
instructionsStartX = boardEndX + defaultMarginWidth
instructionsStartY = boardStartY
)
// Text in the UI
const title = "TETRIS WRITTEN IN GO"
var instructions = []string{
"Goal: Fill in 5 lines!",
"",
"left Left",
"right Right",
"up Rotate",
"down Down",
"space Fall",
"s Start",
"p Pause",
"esc,q Exit",
"",
"Level: %v",
"Lines: %v",
"",
"GAME OVER!",
}
// This takes care of rendering everything.
func render(g *Game) {
termbox.Clear(backgroundColor, backgroundColor)
tbprint(titleStartX, titleStartY, instructionsColor, backgroundColor, title)
for y := 0; y < boardHeight; y++ {
for x := 0; x < boardWidth; x++ {
cellValue := g.board[y][x]
absCellValue := int(math.Abs(float64(cellValue)))
cellColor := pieceColors[absCellValue]
for i := 0; i < cellWidth; i++ {
termbox.SetCell(boardStartX+cellWidth*x+i, boardStartY+y, ' ', cellColor, cellColor)
}
}
}
tx := g.x
ty := g.y
for g.pieceFits(tx, ty) {
ty += 1
}
ty -= 1
for k := 0; k < numSquares; k++ {
x := tx + g.dx[k]
y := ty + g.dy[k]
origin_y := g.y + g.dy[k]
if 0 <= origin_y && origin_y < boardHeight {
if 0 <= y && y < boardHeight && 0 <= x && x < boardWidth && g.board[y][x] != -g.piece {
cellValue := g.board[origin_y][x]
absCellValue := int(math.Abs(float64(cellValue)))
cellColor := pieceColors[absCellValue]
for i := 0; i < cellWidth; i++ {
termbox.SetCell(boardStartX+cellWidth*x+i, boardStartY+y, '*', termbox.ColorBlack, cellColor)
}
}
}
}
for y, instruction := range instructions {
if strings.HasPrefix(instruction, "Level:") {
instruction = fmt.Sprintf(instruction, g.level)
} else if strings.HasPrefix(instruction, "Lines:") {
instruction = fmt.Sprintf(instruction, g.numLines)
} else if strings.HasPrefix(instruction, "GAME OVER") && g.state != gameOver {
instruction = ""
}
tbprint(instructionsStartX, instructionsStartY+y, instructionsColor, backgroundColor, instruction)
}
termbox.Flush()
}
// Function tbprint draws a string.
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}