Skip to content

Commit 61df676

Browse files
fixed bug and build
1 parent 1bf7369 commit 61df676

File tree

4 files changed

+37
-24
lines changed

4 files changed

+37
-24
lines changed

TermiSand.exe

-1.5 KB
Binary file not shown.

go.mod

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ module TermiSand
22

33
go 1.21.6
44

5+
require (
6+
github.com/gdamore/tcell/v2 v2.7.4
7+
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130
8+
)
9+
510
require (
611
github.com/gdamore/encoding v1.0.0 // indirect
7-
github.com/gdamore/tcell/v2 v2.7.4 // indirect
812
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
913
github.com/mattn/go-runewidth v0.0.15 // indirect
10-
github.com/rivo/uniseg v0.4.3 // indirect
14+
github.com/rivo/uniseg v0.4.7 // indirect
1115
golang.org/x/sys v0.17.0 // indirect
1216
golang.org/x/term v0.17.0 // indirect
1317
golang.org/x/text v0.14.0 // indirect

go.sum

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
66
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
77
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
88
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
9+
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130 h1:o1CYtoFOm6xJK3DvDAEG5wDJPLj+SoxUtUDFaQgt1iY=
10+
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
911
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
10-
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
1112
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
13+
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
14+
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
1215
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
1316
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1417
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

main.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
var (
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

8994
func 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

Comments
 (0)