tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/microbitmatrix/main.go (about)

     1  package main
     2  
     3  import (
     4  	"image/color"
     5  	"math/rand"
     6  	"time"
     7  
     8  	"tinygo.org/x/drivers/microbitmatrix"
     9  )
    10  
    11  var display microbitmatrix.Device
    12  
    13  func main() {
    14  	display = microbitmatrix.New()
    15  	display.Configure(microbitmatrix.Config{})
    16  
    17  	display.ClearDisplay()
    18  
    19  	x := int16(1)
    20  	y := int16(2)
    21  	deltaX := int16(1)
    22  	deltaY := int16(1)
    23  	then := time.Now()
    24  	c := color.RGBA{255, 255, 255, 255}
    25  
    26  	for {
    27  		if time.Since(then).Nanoseconds() > 80000000 {
    28  			then = time.Now()
    29  
    30  			pixel := display.GetPixel(x, y)
    31  			if pixel {
    32  				display.ClearDisplay()
    33  				x = 1 + int16(rand.Int31n(3))
    34  				y = 1 + int16(rand.Int31n(3))
    35  				deltaX = 1
    36  				deltaY = 1
    37  				if rand.Int31n(2) == 0 {
    38  					deltaX = -1
    39  				}
    40  				if rand.Int31n(2) == 0 {
    41  					deltaY = -1
    42  				}
    43  			}
    44  			display.SetPixel(x, y, c)
    45  
    46  			x += deltaX
    47  			y += deltaY
    48  
    49  			if x == 0 || x == 4 {
    50  				deltaX = -deltaX
    51  			}
    52  
    53  			if y == 0 || y == 4 {
    54  				deltaY = -deltaY
    55  			}
    56  		}
    57  		display.Display()
    58  	}
    59  }