diff --git a/examples/community/go-jetpack/README.md b/examples/community/go-jetpack/README.md new file mode 100644 index 0000000..2f4eb31 --- /dev/null +++ b/examples/community/go-jetpack/README.md @@ -0,0 +1,7 @@ +# Go Jetpack +by [Branson Camp](https://github.com/bcamp1) using [Pixel](https://github.com/faiface/pixel) + +Welcome to Go-Jetpack, where you explore the skies using WASD or Arrow Keys. +This example showcases most of the fundamental pixel elements described in the [Pixel Wiki](https://github.com/faiface/pixel/wiki) + +![Screenshot](https://github.com/bcamp1/pixel/blob/master/examples/community/go-jetpack/screenshot.png) diff --git a/examples/community/go-jetpack/intuitive.ttf b/examples/community/go-jetpack/intuitive.ttf new file mode 100644 index 0000000..9039d7b Binary files /dev/null and b/examples/community/go-jetpack/intuitive.ttf differ diff --git a/examples/community/go-jetpack/jetpack-on.png b/examples/community/go-jetpack/jetpack-on.png new file mode 100644 index 0000000..2df103f Binary files /dev/null and b/examples/community/go-jetpack/jetpack-on.png differ diff --git a/examples/community/go-jetpack/jetpack-on2.png b/examples/community/go-jetpack/jetpack-on2.png new file mode 100644 index 0000000..2bfb514 Binary files /dev/null and b/examples/community/go-jetpack/jetpack-on2.png differ diff --git a/examples/community/go-jetpack/jetpack.go b/examples/community/go-jetpack/jetpack.go new file mode 100644 index 0000000..3927072 --- /dev/null +++ b/examples/community/go-jetpack/jetpack.go @@ -0,0 +1,196 @@ +package main + +import ( + "fmt" + "image" + "io/ioutil" + "os" + + _ "image/png" + + "github.com/faiface/pixel" + "github.com/faiface/pixel/pixelgl" + "github.com/faiface/pixel/text" + "github.com/golang/freetype/truetype" + "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 loadSprite(path string) (pixel.Sprite, error) { + pic, err := loadPicture(path) + if err != nil { + return *pixel.NewSprite(pic, pic.Bounds()), err + } + sprite := pixel.NewSprite(pic, pic.Bounds()) + return *sprite, nil +} + +func loadTTF(path string, size float64, origin pixel.Vec) *text.Text { + file, err := os.Open(path) + if err != nil { + panic(err) + } + defer file.Close() + + bytes, err := ioutil.ReadAll(file) + if err != nil { + panic(err) + } + + font, err := truetype.Parse(bytes) + if err != nil { + panic(err) + } + + face := truetype.NewFace(font, &truetype.Options{ + Size: size, + GlyphCacheEntries: 1, + }) + + atlas := text.NewAtlas(face, text.ASCII) + + txt := text.New(origin, atlas) + + return txt + +} + +func run() { + // Set up window configs + cfg := pixelgl.WindowConfig{ // Default: 1024 x 768 + Title: "Golang Jetpack!", + Bounds: pixel.R(0, 0, 1024, 768), + VSync: true, + } + win, err := pixelgl.NewWindow(cfg) + if err != nil { + panic(err) + } + + // Importantr variables + var jetX, jetY, velX, velY, radians float64 = 0, 0, 0, 0, 0 + flipped := 1.0 + jetpackOn := false + gravity := 0.6 // Default: 0.004 + jetAcc := 0.9 // Default: 0.008 + tilt := 0.01 // Default: 0.001 + whichOn := false + onNumber := 0 + jetpackName := "jetpack.png" + camVector := win.Bounds().Center() + + bg, _ := loadSprite("sky.png") + + // Tutorial Text + txt := loadTTF("intuitive.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200)) + fmt.Fprintf(txt, "Explore the Skies with WASD or Arrow Keys!") + + // Game Loop + for !win.Closed() { + win.Update() + win.Clear(colornames.Green) + + // Jetpack - Controls + jetpackOn = win.Pressed(pixelgl.KeyUp) || win.Pressed(pixelgl.KeyW) + + if win.Pressed(pixelgl.KeyRight) || win.Pressed(pixelgl.KeyD) { + jetpackOn = true + flipped = -1 + radians -= tilt + velX += tilt * 30 + } else if win.Pressed(pixelgl.KeyLeft) || win.Pressed(pixelgl.KeyA) { + jetpackOn = true + flipped = 1 + radians += tilt + velX -= tilt * 30 + } else { + if velX < 0 { + radians -= tilt / 3 + velX += tilt * 10 + } else if velX > 0 { + radians += tilt / 3 + velX -= tilt * 10 + } + } + if jetY < 0 { + jetY = 0 + velY = -0.3 * velY + } + + if jetpackOn { + velY += jetAcc + whichOn = !whichOn + onNumber += 1 + if onNumber == 5 { // every 5 frames, toggle anijetMation + onNumber = 0 + if whichOn { + jetpackName = "jetpack-on.png" + } else { + jetpackName = "jetpack-on2.png" + } + } + } else { + jetpackName = "jetpack.png" + velY -= gravity + } + + // Jetpack - Rendering + jetpack, err := loadSprite(jetpackName) + if err != nil { + panic(err) + } + + positionVector := pixel.V(win.Bounds().Center().X+jetX, win.Bounds().Center().Y+jetY-372) + jetMat := pixel.IM + jetMat = jetMat.Scaled(pixel.ZV, 4) + jetMat = jetMat.Moved(positionVector) + jetMat = jetMat.ScaledXY(positionVector, pixel.V(flipped, 1)) + jetMat = jetMat.Rotated(positionVector, radians) + + jetX += velX + jetY += velY + + // Camera + camVector.X += (positionVector.X - camVector.X) * 0.2 + camVector.Y += (positionVector.Y - camVector.Y) * 0.2 + + if camVector.X > 25085 { + camVector.X = 25085 + } else if camVector.X < -14843 { + camVector.X = -14843 + } + + if camVector.Y > 22500 { + camVector.Y = 22500 + } + + cam := pixel.IM.Moved(win.Bounds().Center().Sub(camVector)) + + win.SetMatrix(cam) + + // Drawing to the screen + win.SetSmooth(true) + bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10)) + txt.Draw(win, pixel.IM) + win.SetSmooth(false) + jetpack.Draw(win, jetMat) + + } + +} + +func main() { + pixelgl.Run(run) +} diff --git a/examples/community/go-jetpack/jetpack.png b/examples/community/go-jetpack/jetpack.png new file mode 100644 index 0000000..4ea39e0 Binary files /dev/null and b/examples/community/go-jetpack/jetpack.png differ diff --git a/examples/community/go-jetpack/screenshot.png b/examples/community/go-jetpack/screenshot.png new file mode 100644 index 0000000..a41e269 Binary files /dev/null and b/examples/community/go-jetpack/screenshot.png differ diff --git a/examples/community/go-jetpack/sky.png b/examples/community/go-jetpack/sky.png new file mode 100644 index 0000000..ab5fa36 Binary files /dev/null and b/examples/community/go-jetpack/sky.png differ