update 05 guide code

This commit is contained in:
faiface 2017-05-26 14:04:44 +02:00
parent 3e7d03b4f7
commit 30f59e9798
1 changed files with 9 additions and 10 deletions

View File

@ -46,8 +46,8 @@ func run() {
batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
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 {
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))
}
}
@ -69,28 +69,27 @@ func run() {
dt := time.Since(last).Seconds()
last = time.Now()
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center() - camPos)
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam)
if win.Pressed(pixelgl.MouseButtonLeft) {
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
mouse := cam.Unproject(win.MousePosition())
tree.SetMatrix(pixel.IM.Scaled(0, 4).Moved(mouse))
tree.Draw(batch)
tree.Draw(batch, pixel.IM.Scaled(pixel.ZV, 4).Moved(mouse))
}
if win.Pressed(pixelgl.KeyLeft) {
camPos -= pixel.X(camSpeed * dt)
camPos.X -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyRight) {
camPos += pixel.X(camSpeed * dt)
camPos.X += camSpeed * dt
}
if win.Pressed(pixelgl.KeyDown) {
camPos -= pixel.Y(camSpeed * dt)
camPos.Y -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyUp) {
camPos += pixel.Y(camSpeed * dt)
camPos.Y += camSpeed * dt
}
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y())
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
win.Clear(colornames.Forestgreen)
batch.Draw(win)