@@ -13,6 +13,7 @@ import (
13
13
var (
14
14
screenWidth , screenHeight int
15
15
grid [][]int
16
+ styleGrid [][]int
16
17
lastMouseX , lastMouseY int
17
18
mouseMoved bool
18
19
)
@@ -49,41 +50,45 @@ func HSVtoRGB(hue int) (int32, int32, int32) {
49
50
return int32 (r ), int32 (g ), int32 (b )
50
51
}
51
52
52
- func render (s tcell.Screen , updates [][2 ]int ) {
53
- for _ , update := range updates {
54
- x , y := update [0 ], update [1 ]
55
- ch := grid [y ][x ]
56
- if ch > 0 {
57
- blockstyle := tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (ch )))
58
- s .SetContent (x , y , ' ' , nil , blockstyle )
59
- } else {
60
- s .SetContent (x , y , ' ' , nil , tcell .StyleDefault )
61
- }
62
- }
63
- }
53
+ func render (s tcell.Screen ) {
64
54
65
- func updateGrid () [][2 ]int {
66
- updates := make ([][2 ]int , 0 )
67
55
for y := screenHeight - 2 ; y >= 0 ; y -- {
68
56
for x := 0 ; x < screenWidth ; x ++ {
57
+ blockstyle := tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y ][x ])))
69
58
if grid [y ][x ] > 0 {
70
59
if grid [y + 1 ][x ] == 0 {
71
60
grid [y + 1 ][x ] = grid [y ][x ]
72
61
grid [y ][x ] = 0
73
- updates = append (updates , [2 ]int {x , y }, [2 ]int {x , y + 1 })
62
+ s .SetContent (x , y + 1 , ' ' , nil , blockstyle )
63
+ styleGrid [y + 1 ][x ] = 1
74
64
} else if x > 0 && grid [y + 1 ][x - 1 ] == 0 {
75
65
grid [y + 1 ][x - 1 ] = grid [y ][x ]
76
66
grid [y ][x ] = 0
77
- updates = append (updates , [2 ]int {x , y }, [2 ]int {x - 1 , y + 1 })
67
+ s .SetContent (x - 1 , y + 1 , ' ' , nil , blockstyle )
68
+ styleGrid [y + 1 ][x - 1 ] = 1
78
69
} else if x < screenWidth - 1 && grid [y + 1 ][x + 1 ] == 0 {
79
70
grid [y + 1 ][x + 1 ] = grid [y ][x ]
80
71
grid [y ][x ] = 0
81
- updates = append (updates , [2 ]int {x , y }, [2 ]int {x + 1 , y + 1 })
72
+ s .SetContent (x + 1 , y + 1 , ' ' , nil , blockstyle )
73
+ styleGrid [y + 1 ][x + 1 ] = 1
82
74
}
75
+ } else {
76
+ style := tcell .StyleDefault
77
+ if y != 0 && grid [y - 1 ][x ] != 0 {
78
+ style = tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y - 1 ][x ])))
79
+ grid [y ][x ] = grid [y - 1 ][x ]
80
+ grid [y - 1 ][x ] = 0
81
+ styleGrid [y ][x ] = 1
82
+ } else {
83
+ styleGrid [y ][x ] = 0
84
+ }
85
+ s .SetContent (x , y , ' ' , nil , style )
86
+ }
87
+ if grid [y ][x ] != 0 && styleGrid [y ][x ] == 0 {
88
+ s .SetContent (x , y , ' ' , nil , tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y ][x ]))))
83
89
}
84
90
}
85
91
}
86
- return updates
87
92
}
88
93
89
94
func main () {
@@ -101,9 +106,11 @@ func main() {
101
106
s .EnableMouse ()
102
107
screenWidth , screenHeight = s .Size ()
103
108
grid = make ([][]int , screenHeight )
109
+ styleGrid = make ([][]int , screenHeight )
104
110
105
111
for i := 0 ; i < screenHeight ; i ++ {
106
112
grid [i ] = make ([]int , screenWidth )
113
+ styleGrid [i ] = make ([]int , screenWidth )
107
114
}
108
115
109
116
s .Clear ()
@@ -142,7 +149,7 @@ func main() {
142
149
}
143
150
case <- ticker .C :
144
151
// Add sand at the last known mouse position if the mouse has moved
145
- if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth {
152
+ if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid [ lastMouseY ][ lastMouseX ] == 0 {
146
153
grid [lastMouseY ][lastMouseX ] = colorNum
147
154
rand1 := rand .Intn (4 )
148
155
rand2 := rand .Intn (4 )
@@ -161,8 +168,7 @@ func main() {
161
168
}
162
169
}
163
170
164
- updates := updateGrid ()
165
- render (s , updates )
171
+ render (s )
166
172
s .Show ()
167
173
colorNum ++
168
174
if colorNum == 360 {
0 commit comments