~rockorager/snowflake-term

119555cff76528457a5d6220de316a1a88eb29c2 — Tim Culverhouse 5 months ago 28d1fcc
make animations slightly smoother

The previous drawing would advance some flakes by 3 rows per draw cycle.
Smooth this out by advancing 1 row at a time, but at different
frequencies
1 files changed, 22 insertions(+), 6 deletions(-)

M main.go
M main.go => main.go +22 -6
@@ 37,9 37,13 @@ func main() {
		newSnowflake(width),
		newSnowflake(width),
		newSnowflake(width),
		newSnowflake(width),
		newSnowflake(width),
		newSnowflake(width),
		newSnowflake(width),
	}

	ticker := time.NewTicker(200 * time.Millisecond)
	ticker := time.NewTicker(50 * time.Millisecond)
	events := vx.Events()
	for {
		w, h := vx.Window().Size()


@@ 55,7 59,7 @@ func main() {
		case <-ticker.C:
			// Update our snowflake positions
			for i, s := range snowflakes {
				s.row += s.speed
				s.update()
				if s.row > h {
					snowflakes[i] = newSnowflake(w)
				}


@@ 79,9 83,13 @@ var cell = vaxis.Cell{
}

type snowflake struct {
	row   int
	col   int
	speed int
	row int
	col int
	// delay is how many update cycles we wait before advancing to the next
	// row. This can be thought of as the inverse of speed
	delay int
	// acc is the accumulation of delays
	acc int
}

// generates a new snowflake which will enter the screen at a random column and


@@ 90,12 98,20 @@ func newSnowflake(w int) *snowflake {
	flake := &snowflake{
		row:   -1,
		col:   rand.Intn(w),
		speed: rand.Intn(3) + 1,
		delay: rand.Intn(5) + 1,
	}

	return flake
}

func (s *snowflake) update() {
	s.acc += 1
	if s.acc > s.delay {
		s.row += 1
		s.acc = 0
	}
}

// Draw draws the snowflake.
func (s *snowflake) Draw(win vaxis.Window) {
	// W