@@ -13,6 +13,7 @@ import (
1313var (
1414 screenWidth , screenHeight int
1515 grid [][]int
16+ styleGrid [][]int
1617 lastMouseX , lastMouseY int
1718 mouseMoved bool
1819)
@@ -49,41 +50,45 @@ func HSVtoRGB(hue int) (int32, int32, int32) {
4950 return int32 (r ), int32 (g ), int32 (b )
5051}
5152
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 ) {
6454
65- func updateGrid () [][2 ]int {
66- updates := make ([][2 ]int , 0 )
6755 for y := screenHeight - 2 ; y >= 0 ; y -- {
6856 for x := 0 ; x < screenWidth ; x ++ {
57+ blockstyle := tcell .StyleDefault .Background (tcell .NewRGBColor (HSVtoRGB (grid [y ][x ])))
6958 if grid [y ][x ] > 0 {
7059 if grid [y + 1 ][x ] == 0 {
7160 grid [y + 1 ][x ] = grid [y ][x ]
7261 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
7464 } else if x > 0 && grid [y + 1 ][x - 1 ] == 0 {
7565 grid [y + 1 ][x - 1 ] = grid [y ][x ]
7666 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
7869 } else if x < screenWidth - 1 && grid [y + 1 ][x + 1 ] == 0 {
7970 grid [y + 1 ][x + 1 ] = grid [y ][x ]
8071 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
8274 }
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 ]))))
8389 }
8490 }
8591 }
86- return updates
8792}
8893
8994func main () {
@@ -101,9 +106,11 @@ func main() {
101106 s .EnableMouse ()
102107 screenWidth , screenHeight = s .Size ()
103108 grid = make ([][]int , screenHeight )
109+ styleGrid = make ([][]int , screenHeight )
104110
105111 for i := 0 ; i < screenHeight ; i ++ {
106112 grid [i ] = make ([]int , screenWidth )
113+ styleGrid [i ] = make ([]int , screenWidth )
107114 }
108115
109116 s .Clear ()
@@ -142,7 +149,7 @@ func main() {
142149 }
143150 case <- ticker .C :
144151 // 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 {
146153 grid [lastMouseY ][lastMouseX ] = colorNum
147154 rand1 := rand .Intn (4 )
148155 rand2 := rand .Intn (4 )
@@ -161,8 +168,7 @@ func main() {
161168 }
162169 }
163170
164- updates := updateGrid ()
165- render (s , updates )
171+ render (s )
166172 s .Show ()
167173 colorNum ++
168174 if colorNum == 360 {
0 commit comments