-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d44ebff
commit eea40d2
Showing
4 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#Termisand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module TermiSand | ||
|
||
go 1.21.6 | ||
|
||
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 | ||
golang.org/x/sys v0.17.0 // indirect | ||
golang.org/x/term v0.17.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko= | ||
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg= | ||
github.com/gdamore/tcell/v2 v2.7.4 h1:sg6/UnTM9jGpZU+oFYAsDahfchWAFW8Xx2yFinNSAYU= | ||
github.com/gdamore/tcell/v2 v2.7.4/go.mod h1:dSXtXTSK0VsW1biw65DZLZ2NKr7j0qP/0J7ONmsraWg= | ||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= | ||
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/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/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= | ||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= | ||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= | ||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= | ||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= | ||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= | ||
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= | ||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= | ||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= | ||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= | ||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= | ||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"math" | ||
"math/rand" | ||
"os" | ||
"time" | ||
|
||
"github.com/gdamore/tcell/v2" | ||
) | ||
|
||
var ( | ||
screenWidth, screenHeight int | ||
grid [][]int | ||
lastMouseX, lastMouseY int | ||
mouseMoved bool | ||
) | ||
|
||
func HSVtoRGB(hue int) (int32, int32, int32) { | ||
saturation := 1.0 | ||
value := 1.0 | ||
|
||
c := value * saturation | ||
x := c * (1 - math.Abs(math.Mod(float64(hue)/60.0, 2)-1)) | ||
m := value - c | ||
|
||
var r, g, b float64 | ||
|
||
switch { | ||
case hue >= 0 && hue < 60: | ||
r, g, b = c, x, 0 | ||
case hue >= 60 && hue < 120: | ||
r, g, b = x, c, 0 | ||
case hue >= 120 && hue < 180: | ||
r, g, b = 0, c, x | ||
case hue >= 180 && hue < 240: | ||
r, g, b = 0, x, c | ||
case hue >= 240 && hue < 300: | ||
r, g, b = x, 0, c | ||
case hue >= 300 && hue < 360: | ||
r, g, b = c, 0, x | ||
} | ||
|
||
r = (r + m) * 255 | ||
g = (g + m) * 255 | ||
b = (b + m) * 255 | ||
|
||
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 updateGrid() [][2]int { | ||
updates := make([][2]int, 0) | ||
for y := screenHeight - 2; y >= 0; y-- { | ||
for x := 0; x < screenWidth; 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}) | ||
} 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}) | ||
} 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}) | ||
} | ||
} | ||
} | ||
} | ||
return updates | ||
} | ||
|
||
func main() { | ||
s, err := tcell.NewScreen() | ||
if err != nil { | ||
log.Fatalf("TermISand couldn't initialise the screen :(") | ||
} | ||
defer s.Fini() | ||
|
||
if err := s.Init(); err != nil { | ||
log.Fatalf("TermISand couldn't initialise the screen :(") | ||
} | ||
|
||
s.EnableFocus() | ||
s.EnableMouse() | ||
screenWidth, screenHeight = s.Size() | ||
grid = make([][]int, screenHeight) | ||
|
||
for i := 0; i < screenHeight; i++ { | ||
grid[i] = make([]int, screenWidth) | ||
} | ||
|
||
s.Clear() | ||
colorNum := 0 | ||
|
||
eventCh := make(chan tcell.Event, 1) | ||
|
||
go func() { | ||
for { | ||
ev := s.PollEvent() | ||
if ev != nil { | ||
eventCh <- ev | ||
} | ||
} | ||
}() | ||
|
||
ticker := time.NewTicker(20 * time.Millisecond) | ||
defer ticker.Stop() | ||
|
||
for { | ||
select { | ||
case ev := <-eventCh: | ||
switch ev := ev.(type) { | ||
case *tcell.EventKey: | ||
switch ev.Key() { | ||
case tcell.KeyCtrlQ: | ||
s.Fini() | ||
os.Exit(0) | ||
} | ||
case *tcell.EventResize: | ||
screenWidth, screenHeight = s.Size() | ||
s.Sync() | ||
case *tcell.EventMouse: | ||
lastMouseX, lastMouseY = ev.Position() | ||
mouseMoved = true | ||
} | ||
case <-ticker.C: | ||
// Add sand at the last known mouse position if the mouse has moved | ||
if mouseMoved && lastMouseY < screenHeight && lastMouseX < screenWidth { | ||
grid[lastMouseY][lastMouseX] = colorNum | ||
rand1 := rand.Intn(4) | ||
rand2 := rand.Intn(4) | ||
|
||
if (rand1 == 0 || rand2 == 0) && lastMouseY-1 >= 0 && grid[lastMouseY-1][lastMouseX] == 0 { | ||
grid[lastMouseY-1][lastMouseX] = colorNum | ||
} | ||
if (rand1 == 1 || rand2 == 1) && lastMouseY+1 < screenHeight && grid[lastMouseY+1][lastMouseX] == 0 { | ||
grid[lastMouseY+1][lastMouseX] = colorNum | ||
} | ||
if (rand1 == 2 || rand2 == 2) && lastMouseX-1 >= 0 && grid[lastMouseY][lastMouseX-1] == 0 { | ||
grid[lastMouseY][lastMouseX-1] = colorNum | ||
} | ||
if (rand1 == 3 || rand2 == 3) && lastMouseX+1 < screenWidth && grid[lastMouseY][lastMouseX+1] == 0 { | ||
grid[lastMouseY][lastMouseX+1] = colorNum | ||
} | ||
} | ||
|
||
updates := updateGrid() | ||
render(s, updates) | ||
s.Show() | ||
colorNum++ | ||
if colorNum == 360 { | ||
colorNum = 1 | ||
} | ||
} | ||
} | ||
} |