Golang Sprites
Go Sprites is for simulating actor model sprites in a 2D world
Golang Sprites is a Reimagining of Scratch created by MIT Media Lab. Every entity on the screen is a sprite that can be programmed independently.
The goals are many:
- Remove the complexity of the game loop handling
- A super easy game engine
- See the falling turtles example
- A package for visualizing a simulation
- See the double pendulum example
Install
Go 1.21 or later is required.
Ebitengine is the main dependency. Check here for the system specific instructions.
Example
Random Walk
A really simple example is to create turtles that each perform a random walk.
main(...)
start the sim with the window size parameters. Ebiten eats the main thread, so the function to run once Ebiten starts is passed as an additional parameter.
simStartFunc(...)
Adds one sprite image and then starts go routines for each sprite. A new sprite will be started every half second.
turtleRandomWalk(...)
is the main function for each sprite. A new sprite is created and setup. Then it enters an infinite loop where a random velocity is added in the x and y directions. That velocity is integrated into position. The position and angle are updated. And finally, the sprite sleeps. If the sprite gets too far off screen then it is deleted.
package main
import (
"math"
"math/rand"
"time"
"github.com/gary23b/sprites"
"github.com/gary23b/sprites/models"
"github.com/gary23b/sprites/sprite"
)
func main() {
params := sprites.ScratchParams{Width: 1000, Height: 1000}
sprites.Start(params, simStartFunc)
}
func simStartFunc(sim models.Scratch) {
sim.AddCostume(sprite.DecodeCodedSprite(sprite.TurtleImage), "t")
for {
go turtleRandomWalk(sim)
time.Sleep(time.Millisecond * 500)
}
}
func turtleRandomWalk(sim models.Scratch) {
s := sim.AddSprite("")
s.Costume("t")
s.Visible(true)
x, y := 0.0, 0.0
velX, velY := 0.0, 0.0
for {
velX += (rand.Float64()*2 - 1) * .05
velY += (rand.Float64()*2 - 1) * .05
x += velX
y += velY
s.Pos(x, y)
s.Angle(180.0 / math.Pi * math.Atan2(velY, velX))
if max(math.Abs(x), math.Abs(y)) > 1000 {
s.DeleteSprite()
return
}
time.Sleep(time.Millisecond * 10)
}
}
Double Pendulum
Here is a simulation of a double pendulum.
go run github.com/gary23b/sprites/examples/DoublePendulum@latest
Falling Turtles
Here is a really simple game where you have to click on each turtle before it reaches the bottom of the screen. At the end, your final score is displayed.
go run github.com/gary23b/sprites/examples/fallingturtles@latest
Build Executable for Window
GOOS=windows go build ./examples/fallingturtles/
Things to Research