Skip to content

Commit

Permalink
fixed bug and build
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdaProgrammer committed Jul 28, 2024
1 parent 1bf7369 commit 61df676
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
Binary file modified TermiSand.exe
Binary file not shown.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ module TermiSand

go 1.21.6

require (
github.com/gdamore/tcell/v2 v2.7.4
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/gdamore/tcell/v2 v2.7.4 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/rivo/uniseg v0.4.3 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
5 changes: 4 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130 h1:o1CYtoFOm6xJK3DvDAEG5wDJPLj+SoxUtUDFaQgt1iY=
github.com/rivo/tview v0.0.0-20240625185742-b0a7293b8130/go.mod h1:02iFIz7K/A9jGCvrizLPvoqr4cEIx7q54RH5Qudkrss=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
Expand Down
48 changes: 27 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
screenWidth, screenHeight int
grid [][]int
styleGrid [][]int
lastMouseX, lastMouseY int
mouseMoved bool
)
Expand Down Expand Up @@ -49,41 +50,45 @@ func HSVtoRGB(hue int) (int32, int32, int32) {
return int32(r), int32(g), int32(b)
}

func render(s tcell.Screen, updates [][2]int) {
for _, update := range updates {
x, y := update[0], update[1]
ch := grid[y][x]
if ch > 0 {
blockstyle := tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(ch)))
s.SetContent(x, y, ' ', nil, blockstyle)
} else {
s.SetContent(x, y, ' ', nil, tcell.StyleDefault)
}
}
}
func render(s tcell.Screen) {

func updateGrid() [][2]int {
updates := make([][2]int, 0)
for y := screenHeight - 2; y >= 0; y-- {
for x := 0; x < screenWidth; x++ {
blockstyle := tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x])))
if grid[y][x] > 0 {
if grid[y+1][x] == 0 {
grid[y+1][x] = grid[y][x]
grid[y][x] = 0
updates = append(updates, [2]int{x, y}, [2]int{x, y + 1})
s.SetContent(x, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x] = 1
} else if x > 0 && grid[y+1][x-1] == 0 {
grid[y+1][x-1] = grid[y][x]
grid[y][x] = 0
updates = append(updates, [2]int{x, y}, [2]int{x - 1, y + 1})
s.SetContent(x-1, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x-1] = 1
} else if x < screenWidth-1 && grid[y+1][x+1] == 0 {
grid[y+1][x+1] = grid[y][x]
grid[y][x] = 0
updates = append(updates, [2]int{x, y}, [2]int{x + 1, y + 1})
s.SetContent(x+1, y+1, ' ', nil, blockstyle)
styleGrid[y+1][x+1] = 1
}
} else {
style := tcell.StyleDefault
if y != 0 && grid[y-1][x] != 0 {
style = tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y-1][x])))
grid[y][x] = grid[y-1][x]
grid[y-1][x] = 0
styleGrid[y][x] = 1
} else {
styleGrid[y][x] = 0
}
s.SetContent(x, y, ' ', nil, style)
}
if grid[y][x] != 0 && styleGrid[y][x] == 0 {
s.SetContent(x, y, ' ', nil, tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x]))))
}
}
}
return updates
}

func main() {
Expand All @@ -101,9 +106,11 @@ func main() {
s.EnableMouse()
screenWidth, screenHeight = s.Size()
grid = make([][]int, screenHeight)
styleGrid = make([][]int, screenHeight)

for i := 0; i < screenHeight; i++ {
grid[i] = make([]int, screenWidth)
styleGrid[i] = make([]int, screenWidth)
}

s.Clear()
Expand Down Expand Up @@ -142,7 +149,7 @@ func main() {
}
case <-ticker.C:
// Add sand at the last known mouse position if the mouse has moved
if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth {
if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid[lastMouseY][lastMouseX] == 0 {
grid[lastMouseY][lastMouseX] = colorNum
rand1 := rand.Intn(4)
rand2 := rand.Intn(4)
Expand All @@ -161,8 +168,7 @@ func main() {
}
}

updates := updateGrid()
render(s, updates)
render(s)
s.Show()
colorNum++
if colorNum == 360 {
Expand Down

0 comments on commit 61df676

Please sign in to comment.