forked from jjinux/gotetris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
98 lines (89 loc) · 2.42 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
package main
import (
"fmt"
"math"
"strings"
"github.com/nsf/termbox-go"
)
// Colors
const backgroundColor = termbox.ColorBlue
const boardColor = termbox.ColorBlack
const 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
const defaultMarginHeight = 1
const titleStartX = defaultMarginWidth
const titleStartY = defaultMarginHeight
const titleHeight = 1
const titleEndY = titleStartY + titleHeight
const boardStartX = defaultMarginWidth
const boardStartY = titleEndY + defaultMarginHeight
const boardWidth = 10
const boardHeight = 16
const cellWidth = 2
const boardEndX = boardStartX + boardWidth*cellWidth
const boardEndY = boardStartY + boardHeight
const instructionsStartX = boardEndX + defaultMarginWidth
const 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)
}
}
}
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++
}
}