draw trees with left mouse click
This commit is contained in:
parent
9136672799
commit
3720389fef
49
main.go
49
main.go
@ -4,8 +4,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/faiface/pixel"
|
"github.com/faiface/pixel"
|
||||||
"github.com/faiface/pixel/pixelgl"
|
"github.com/faiface/pixel/pixelgl"
|
||||||
@ -45,42 +45,35 @@ func run() {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//smooth the edges of the image (antialiasing)
|
spritesheet, err := loadPicture("trees.png")
|
||||||
win.SetSmooth(true)
|
|
||||||
|
|
||||||
//load a specific image and catch any errors
|
|
||||||
pic, err := loadPicture("hiking.png")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//create a sprite using the loaded image
|
var treesFrames []pixel.Rect
|
||||||
sprite := pixel.NewSprite(pic, pic.Bounds())
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//set the angle of the image
|
var (
|
||||||
angle := 0.0
|
trees []*pixel.Sprite
|
||||||
|
matrices []pixel.Matrix
|
||||||
//set the time to compare later
|
)
|
||||||
last := time.Now()
|
|
||||||
|
|
||||||
//loop until window is closed by "X" button
|
//loop until window is closed by "X" button
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
//compare the last time to delta time
|
if win.JustPressed(pixelgl.MouseButtonLeft) {
|
||||||
dt := time.Since(last).Seconds()
|
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
|
||||||
last = time.Now()
|
trees = append(trees, tree)
|
||||||
|
matrices = append(matrices, pixel.IM.Scaled(pixel.ZV, 4).Moved(win.MousePosition()))
|
||||||
//set the angle according to delta time
|
}
|
||||||
angle += 3 * dt
|
|
||||||
|
|
||||||
//refresh screen and set background color
|
//refresh screen and set background color
|
||||||
win.Clear(colornames.Firebrick)
|
win.Clear(colornames.Forestgreen)
|
||||||
|
for i, tree := range trees {
|
||||||
//set the matrix and location of spirte in window
|
tree.Draw(win, matrices[i])
|
||||||
mat := pixel.IM
|
}
|
||||||
mat = mat.Rotated(pixel.ZV, angle)
|
|
||||||
mat = mat.Moved(win.Bounds().Center())
|
|
||||||
sprite.Draw(win, mat)
|
|
||||||
|
|
||||||
//update the window
|
//update the window
|
||||||
win.Update()
|
win.Update()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user