39 lines
737 B
Go
39 lines
737 B
Go
package screensaver
|
|
|
|
import (
|
|
"image"
|
|
"math/rand"
|
|
|
|
"go.maze.io/harmon/hardware"
|
|
"go.maze.io/harmon/internal/pixel"
|
|
)
|
|
|
|
type Stars struct {
|
|
display hardware.Display
|
|
points []image.Point
|
|
}
|
|
|
|
func NewStars(display hardware.Display, n int) *Stars {
|
|
s := &Stars{
|
|
display: display,
|
|
}
|
|
for i := 0; i < n; i++ {
|
|
s.points = append(s.points, s.randomPoint(display.Size()))
|
|
}
|
|
return s
|
|
}
|
|
|
|
func (s *Stars) randomPoint(p image.Point) image.Point {
|
|
return image.Point{X: rand.Intn(p.X), Y: rand.Intn(p.Y)}
|
|
}
|
|
|
|
func (s *Stars) Refresh() error {
|
|
s.display.Clear()
|
|
for _, p := range s.points {
|
|
s.display.Set(p.X, p.Y, pixel.On)
|
|
}
|
|
copy(s.points[1:], s.points)
|
|
s.points[0] = s.randomPoint(s.display.Size())
|
|
return s.display.Refresh()
|
|
}
|