Skip to content

Commit f6777a2

Browse files
resize bug fix and also reset with ctrl+r
1 parent 85b773a commit f6777a2

6 files changed

Lines changed: 83 additions & 52 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ TermiSand is a falling sand simulator, but in your terminal!
2525
<img src="./.github/example.png">
2626

2727
## Usage
28-
```bash
29-
termisand.exe
30-
```
31-
and then move your cursor around and enjoy the show!
28+
Run the "TermiSand" executable
29+
30+
and then move your cursor around and enjoy the show!
31+
to clear the screen press ctrl+r and watch as the sand falls off the screen ready for you to start filling it up again!
3232

3333
## Built with
3434
![](https://img.shields.io/badge/tcell-4298B8.svg?style=for-the-badge&logoColor=white)

TermiSand

3.58 MB
Binary file not shown.

TermiSand.exe

100644100755
280 KB
Binary file not shown.

go.mod

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ module github.com/BobdaProgrammer/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-
)
5+
require github.com/gdamore/tcell/v2 v2.7.4
96

107
require (
118
github.com/gdamore/encoding v1.0.0 // indirect

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ 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=
119
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
1210
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
1311
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=

main.go

Lines changed: 78 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
styleGrid [][]int
1717
lastMouseX, lastMouseY int
1818
mouseMoved bool
19+
hasFloor bool = true
1920
)
2021

2122
func HSVtoRGB(hue int) (int32, int32, int32) {
@@ -50,40 +51,77 @@ func HSVtoRGB(hue int) (int32, int32, int32) {
5051
return int32(r), int32(g), int32(b)
5152
}
5253

54+
func BottomRowClear() bool {
55+
for x:=range grid[len(grid)-1]{
56+
if grid[len(grid)-1][x] != 0{
57+
return false
58+
}
59+
}
60+
return true
61+
}
62+
63+
func resetGrid(s tcell.Screen){
64+
grid = make([][]int, screenHeight)
65+
styleGrid = make([][]int, screenHeight)
66+
67+
for i := 0; i < screenHeight; i++ {
68+
grid[i] = make([]int, screenWidth)
69+
styleGrid[i] = make([]int, screenWidth)
70+
for x := 0; x < screenWidth; x++{
71+
s.SetContent(x, i, ' ', nil, tcell.StyleDefault)
72+
}
73+
}
74+
}
75+
5376
func render(s tcell.Screen) {
5477

78+
if !hasFloor{
79+
isclear:=true
80+
for x := range grid[len(grid)-1]{
81+
if grid[len(grid)-1][x] != 0{
82+
isclear=false;
83+
}
84+
grid[len(grid)-1][x]=0
85+
s.SetContent(x, screenHeight-1, ' ', nil, tcell.StyleDefault)
86+
}
87+
if isclear{
88+
hasFloor=true
89+
resetGrid(s)
90+
return
91+
}
92+
}
5593
for y := screenHeight - 2; y >= 0; y-- {
5694
for x := 0; x < screenWidth; x++ {
5795
blockstyle := tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x])))
58-
if grid[y][x] > 0 {
59-
if grid[y+1][x] == 0 {
60-
grid[y+1][x] = grid[y][x]
61-
grid[y][x] = 0
62-
s.SetContent(x, y+1, ' ', nil, blockstyle)
63-
styleGrid[y+1][x] = 1
64-
} else if x > 0 && grid[y+1][x-1] == 0 {
65-
grid[y+1][x-1] = grid[y][x]
66-
grid[y][x] = 0
67-
s.SetContent(x-1, y+1, ' ', nil, blockstyle)
68-
styleGrid[y+1][x-1] = 1
69-
} else if x < screenWidth-1 && grid[y+1][x+1] == 0 {
70-
grid[y+1][x+1] = grid[y][x]
71-
grid[y][x] = 0
72-
s.SetContent(x+1, y+1, ' ', nil, blockstyle)
73-
styleGrid[y+1][x+1] = 1
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-
}
96+
if grid[y][x] > 0 {
97+
if grid[y+1][x] == 0 {
98+
grid[y+1][x] = grid[y][x]
99+
grid[y][x] = 0
100+
s.SetContent(x, y+1, ' ', nil, blockstyle)
101+
styleGrid[y+1][x] = 1
102+
} else if x > 0 && grid[y+1][x-1] == 0 {
103+
grid[y+1][x-1] = grid[y][x]
104+
grid[y][x] = 0
105+
s.SetContent(x-1, y+1, ' ', nil, blockstyle)
106+
styleGrid[y+1][x-1] = 1
107+
} else if x < screenWidth-1 && grid[y+1][x+1] == 0 {
108+
grid[y+1][x+1] = grid[y][x]
109+
grid[y][x] = 0
110+
s.SetContent(x+1, y+1, ' ', nil, blockstyle)
111+
styleGrid[y+1][x+1] = 1
112+
}
113+
} else {
114+
style := tcell.StyleDefault
115+
if y != 0 && grid[y-1][x] != 0 {
116+
style = tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y-1][x])))
117+
grid[y][x] = grid[y-1][x]
118+
grid[y-1][x] = 0
119+
styleGrid[y][x] = 1
120+
} else {
121+
styleGrid[y][x] = 0
122+
}
123+
s.SetContent(x, y, ' ', nil, style)
124+
}
87125
if grid[y][x] != 0 && styleGrid[y][x] == 0 {
88126
s.SetContent(x, y, ' ', nil, tcell.StyleDefault.Background(tcell.NewRGBColor(HSVtoRGB(grid[y][x]))))
89127
}
@@ -105,13 +143,7 @@ func main() {
105143
s.EnableFocus()
106144
s.EnableMouse()
107145
screenWidth, screenHeight = s.Size()
108-
grid = make([][]int, screenHeight)
109-
styleGrid = make([][]int, screenHeight)
110-
111-
for i := 0; i < screenHeight; i++ {
112-
grid[i] = make([]int, screenWidth)
113-
styleGrid[i] = make([]int, screenWidth)
114-
}
146+
resetGrid(s)
115147

116148
s.Clear()
117149
colorNum := 0
@@ -136,20 +168,23 @@ func main() {
136168
switch ev := ev.(type) {
137169
case *tcell.EventKey:
138170
switch ev.Key() {
139-
case tcell.KeyCtrlQ:
171+
case tcell.KeyCtrlQ, tcell.KeyCtrlC:
140172
s.Fini()
141173
os.Exit(0)
174+
case tcell.KeyCtrlR:
175+
hasFloor = false
142176
}
143177
case *tcell.EventResize:
144178
screenWidth, screenHeight = s.Size()
179+
resetGrid(s)
145180
s.Sync()
146181
case *tcell.EventMouse:
147182
lastMouseX, lastMouseY = ev.Position()
148183
mouseMoved = true
149184
}
150185
case <-ticker.C:
151186
// Add sand at the last known mouse position if the mouse has moved
152-
if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid[lastMouseY][lastMouseX] == 0 {
187+
if hasFloor && mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth && grid[lastMouseY][lastMouseX] == 0 {
153188
grid[lastMouseY][lastMouseX] = colorNum
154189
rand1 := rand.Intn(4)
155190
rand2 := rand.Intn(4)
@@ -166,14 +201,15 @@ func main() {
166201
if (rand1 == 3 || rand2 == 3) && lastMouseX+1 < screenWidth && grid[lastMouseY][lastMouseX+1] == 0 {
167202
grid[lastMouseY][lastMouseX+1] = colorNum
168203
}
204+
colorNum++
205+
if colorNum == 360 {
206+
colorNum = 1
207+
}
169208
}
170209

171210
render(s)
172211
s.Show()
173-
colorNum++
174-
if colorNum == 360 {
175-
colorNum = 1
176-
}
212+
177213
}
178214
}
179215
}

0 commit comments

Comments
 (0)