From 5b8cefbc9cceebd39d3786d57b8ac30faa54d745 Mon Sep 17 00:00:00 2001 From: faiface Date: Sun, 9 Apr 2017 15:32:46 +0200 Subject: [PATCH] add code for 04 guide --- .../main.go | 101 ++++++++++++++++++ .../trees.png | Bin 0 -> 2541 bytes 2 files changed, 101 insertions(+) create mode 100644 guide/04_pressing_keys_and_clicking_mouse/main.go create mode 100644 guide/04_pressing_keys_and_clicking_mouse/trees.png diff --git a/guide/04_pressing_keys_and_clicking_mouse/main.go b/guide/04_pressing_keys_and_clicking_mouse/main.go new file mode 100644 index 0000000..3c41512 --- /dev/null +++ b/guide/04_pressing_keys_and_clicking_mouse/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "image" + "math" + "math/rand" + "os" + "time" + + _ "image/png" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/pixelgl" + "golang.org/x/image/colornames" +) + +func loadPicture(path string) (pixel.Picture, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + img, _, err := image.Decode(file) + if err != nil { + return nil, err + } + return pixel.PictureDataFromImage(img), nil +} + +func run() { + cfg := pixelgl.WindowConfig{ + Title: "Pixel Rocks!", + Bounds: pixel.R(0, 0, 1024, 768), + VSync: true, + } + 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 ( + camPos = pixel.V(0, 0) + camSpeed = 500.0 + camZoom = 1.0 + camZoomSpeed = 1.2 + trees []*pixel.Sprite + ) + + last := time.Now() + for !win.Closed() { + dt := time.Since(last).Seconds() + last = time.Now() + + cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center() - camPos) + win.SetMatrix(cam) + + if win.JustPressed(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)) + trees = append(trees, tree) + } + if win.Pressed(pixelgl.KeyLeft) { + camPos -= pixel.X(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyRight) { + camPos += pixel.X(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyDown) { + camPos -= pixel.Y(camSpeed * dt) + } + if win.Pressed(pixelgl.KeyUp) { + camPos += pixel.Y(camSpeed * dt) + } + camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y()) + + win.Clear(colornames.Forestgreen) + + for _, tree := range trees { + tree.Draw(win) + } + + win.Update() + } +} + +func main() { + pixelgl.Run(run) +} diff --git a/guide/04_pressing_keys_and_clicking_mouse/trees.png b/guide/04_pressing_keys_and_clicking_mouse/trees.png new file mode 100644 index 0000000000000000000000000000000000000000..73a6beca00db1b82bf2443eb116873b0414229b9 GIT binary patch literal 2541 zcmV{+P)}rLG?7$+}9Gnqa{J_F5@Bu_bq$?tRX+L9bu=k9Q zkE_Qs{g^h3OB%^umW70? zmr3>Tc3odrFO&K@n4k9!#KHW$h~7qdzCWv8pYD>Az`!d32lMl4I2%>N*{D}AhO<$1 z@i^Tq0-SR$H30w|9>>7r0s(z_YHhwk0W)1?`hOYf4rKHXKXPj_BrJ_5Ruz@RetY%p>H6NA5rw{%r-@7>GOr5V8E z%AvQGb>Q>gr6d5MhrQkkNx6Xl;JcTn)y3melaYl0fXU!< zf`r!{%}?sjc|jISz`^|78*YZP(MpcsDXZp!;F*x&Y!pO*Z^sk&*KmVs>EZ2qWtAFgKawo& zgM`bVYgL3*GLIot{|W&Bm_rR4%{hTRmI;`S2b~DugvYO~kmK932(g_j94pRiDQeA( z=ul*BDQGf;_cJLVB8C8Kw~@iUz1d#^+5$c{3^4c#xV{HryhRaRnkoU)@n9wK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