// main package main import ( "image" _ "image/png" "math/rand" "os" "github.com/faiface/pixel" "github.com/faiface/pixel/pixelgl" "golang.org/x/image/colornames" ) //open file from the system func loadPicture(path string) (pixel.Picture, error) { file, err := os.Open(path) if err != nil { return nil, err } //user 'defer' to close the file later on defer file.Close() //decode the file to find it's type img, _, err := image.Decode(file) if err != nil { return nil, err } return pixel.PictureDataFromImage(img), nil } //Pixel's main function (because GO forced them to do it this way) func run() { //set the Window parameters cfg := pixelgl.WindowConfig{ Title: "Pixel Rocks!", Bounds: pixel.R(0, 0, 1024, 768), VSync: true, } //create a window and pass the parameters win, err := pixelgl.NewWindow(cfg) if err != nil { panic(err) } spritesheet, err := loadPicture("trees.png") if err != nil { panic(err) } var treesFrames []pixel.Rect for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 32 { for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 32 { treesFrames = append(treesFrames, pixel.R(x, y, x+32, y+32)) } } var ( trees []*pixel.Sprite matrices []pixel.Matrix ) //loop until window is closed by "X" button for !win.Closed() { if win.JustPressed(pixelgl.MouseButtonLeft) { tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))]) trees = append(trees, tree) matrices = append(matrices, pixel.IM.Scaled(pixel.ZV, 4).Moved(win.MousePosition())) } //refresh screen and set background color win.Clear(colornames.Forestgreen) for i, tree := range trees { tree.Draw(win, matrices[i]) } //update the window win.Update() } } func main() { pixelgl.Run(run) }