scratch

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

README

Golang Scratch

Go Reference Go CI Go Report Card Coverage Badge

A Scratch Game Engine for Golang

Golang Scratch 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/GaryBrownEEngr/scratch"
	"github.com/GaryBrownEEngr/scratch/models"
	"github.com/GaryBrownEEngr/scratch/sprite"
)

func main() {
	params := scratch.ScratchParams{Width: 1000, Height: 1000}
	scratch.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/GaryBrownEEngr/scratch/examples/DoublePendulum@latest

Example Picture

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/GaryBrownEEngr/scratch/examples/fallingturtles@latest

Build Executable

To get the list of go build targets use the following command:

go tool dist list
Window
GOOS=windows GOARCH=amd64 go build ./examples/fallingturtles/
Linux
GOOS=linux  GOARCH=amd64 go build ./examples/fallingturtles/
WASM
GOOS=js  GOARCH=wasm go build ./examples/fallingturtles/

Things to Research

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetNewestJustPressedFromChan

func GetNewestJustPressedFromChan(justPressedChan chan *models.UserInput) *models.UserInput

This returns nil if there is no new data. This will throw away all but the newest set of data available. So this should be called faster that the game update rate (60Hz), otherwise sim.PressedUserInput() should be used instead.

func Start

func Start(params ScratchParams, simStartFunc func(models.Scratch))

The drawFunc will be started as a go routine.

Types

type ScratchParams

type ScratchParams struct {
	Width   int  // Window Width in pixels
	Height  int  // Window Height in pixels
	ShowFPS bool // Show Frame-Rate and Update-Rate information in top left corner of window
}

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL