project-rpg/main.go

113 lines
2.3 KiB
Go
Raw Normal View History

2018-01-15 21:41:59 -07:00
// main
package main
import (
2018-01-15 23:20:34 -07:00
"image"
_ "image/png"
2018-01-16 21:33:27 -07:00
"math/rand"
2018-01-15 23:20:34 -07:00
"os"
2018-01-16 21:54:57 -07:00
"time"
2018-01-15 23:20:34 -07:00
2018-01-15 22:42:11 -07:00
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
2018-01-15 22:50:18 -07:00
"golang.org/x/image/colornames"
2018-01-15 21:41:59 -07:00
)
2018-01-16 21:10:29 -07:00
//open file from the system
2018-01-15 23:20:34 -07:00
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
2018-01-16 21:10:29 -07:00
//user 'defer' to close the file later on
2018-01-15 23:20:34 -07:00
defer file.Close()
2018-01-16 21:10:29 -07:00
//decode the file to find it's type
2018-01-15 23:20:34 -07:00
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
2018-01-16 21:10:29 -07:00
//Pixel's main function (because GO forced them to do it this way)
2018-01-15 22:42:11 -07:00
func run() {
2018-01-16 21:10:29 -07:00
//set the Window parameters
2018-01-15 22:42:11 -07:00
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
2018-01-15 22:50:18 -07:00
VSync: true,
2018-01-15 22:42:11 -07:00
}
2018-01-16 21:10:29 -07:00
//create a window and pass the parameters
2018-01-15 22:42:11 -07:00
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
2018-01-16 21:33:27 -07:00
spritesheet, err := loadPicture("trees.png")
2018-01-15 23:20:34 -07:00
if err != nil {
panic(err)
}
2018-01-16 21:33:27 -07:00
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))
}
}
2018-01-16 21:10:29 -07:00
2018-01-16 21:54:57 -07:00
//camera
2018-01-16 21:33:27 -07:00
var (
2018-01-16 21:54:57 -07:00
camPos = pixel.ZV
camSpeed = 500.0
2018-01-16 21:33:27 -07:00
trees []*pixel.Sprite
matrices []pixel.Matrix
)
2018-01-15 22:50:18 -07:00
2018-01-16 21:54:57 -07:00
last := time.Now()
2018-01-16 21:10:29 -07:00
//loop until window is closed by "X" button
2018-01-15 22:42:11 -07:00
for !win.Closed() {
2018-01-16 21:54:57 -07:00
dt := time.Since(last).Seconds()
last = time.Now()
cam := pixel.IM.Moved(win.Bounds().Center().Sub(camPos))
win.SetMatrix(cam)
2018-01-16 21:33:27 -07:00
if win.JustPressed(pixelgl.MouseButtonLeft) {
tree := pixel.NewSprite(spritesheet, treesFrames[rand.Intn(len(treesFrames))])
trees = append(trees, tree)
2018-01-16 21:54:57 -07:00
mouse := cam.Unproject(win.MousePosition())
matrices = append(matrices, pixel.IM.Scaled(pixel.ZV, 4).Moved(mouse))
}
if win.Pressed(pixelgl.KeyA) {
camPos.X -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyD) {
camPos.X += camSpeed * dt
}
if win.Pressed(pixelgl.KeyS) {
camPos.Y -= camSpeed * dt
}
if win.Pressed(pixelgl.KeyW) {
camPos.Y += camSpeed * dt
2018-01-16 21:33:27 -07:00
}
2018-01-16 21:10:29 -07:00
//refresh screen and set background color
2018-01-16 21:33:27 -07:00
win.Clear(colornames.Forestgreen)
for i, tree := range trees {
tree.Draw(win, matrices[i])
}
2018-01-16 21:10:29 -07:00
//update the window
2018-01-15 22:42:11 -07:00
win.Update()
}
}
2018-01-15 21:41:59 -07:00
func main() {
2018-01-15 22:42:11 -07:00
pixelgl.Run(run)
2018-01-15 21:41:59 -07:00
}