-
Notifications
You must be signed in to change notification settings - Fork 6
/
border.go
55 lines (45 loc) · 1.17 KB
/
border.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
package main
import tl "github.com/JoelOtter/termloop"
// Border is the edge of the playing area. If the Snake collides with it,
// it dies.
type Border struct {
*tl.Entity
width, height int
coords map[Coord]int
}
// NewBorder creates a Border with the given dimensions.
func NewBorder(width, height int) *Border {
b := new(Border)
b.Entity = tl.NewEntity(1, 1, 1, 1)
// Subtract one to account for bottom and right border
b.width, b.height = width-1, height-1
b.coords = make(map[Coord]int)
// Top and bottom
for x := 0; x < b.width; x++ {
b.coords[Coord{x, 0}] = 1
b.coords[Coord{x, b.height}] = 1
}
// Left and right
for y := 0; y < b.height+1; y++ {
b.coords[Coord{0, y}] = 1
b.coords[Coord{b.width, y}] = 1
}
return b
}
// Contains returns true if a Coord is part of the border, else false.
// Used for collision detection.
func (b *Border) Contains(coord Coord) bool {
_, exists := b.coords[coord]
return exists
}
// Draw draws the border on the screen. A default color is used.
func (b *Border) Draw(screen *tl.Screen) {
if b == nil {
return
}
for c := range b.coords {
screen.RenderCell(c.x, c.y, &tl.Cell{
Bg: tl.ColorBlue,
})
}
}