project-rpg/main.go
2018-01-16 21:10:29 -07:00

92 lines
1.8 KiB
Go

// main
package main
import (
"image"
_ "image/png"
"os"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
//open file from the system
func loadPicture(path string) (pixel.Picture, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
//user 'defer' to close the file later on
defer file.Close()
//decode the file to find it's type
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
//Pixel's main function (because GO forced them to do it this way)
func run() {
//set the Window parameters
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
VSync: true,
}
//create a window and pass the parameters
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
//smooth the edges of the image (antialiasing)
win.SetSmooth(true)
//load a specific image and catch any errors
pic, err := loadPicture("hiking.png")
if err != nil {
panic(err)
}
//create a sprite using the loaded image
sprite := pixel.NewSprite(pic, pic.Bounds())
//set the angle of the image
angle := 0.0
//set the time to compare later
last := time.Now()
//loop until window is closed by "X" button
for !win.Closed() {
//compare the last time to delta time
dt := time.Since(last).Seconds()
last = time.Now()
//set the angle according to delta time
angle += 3 * dt
//refresh screen and set background color
win.Clear(colornames.Firebrick)
//set the matrix and location of spirte in window
mat := pixel.IM
mat = mat.Rotated(pixel.ZV, angle)
mat = mat.Moved(win.Bounds().Center())
sprite.Draw(win, mat)
//update the window
win.Update()
}
}
func main() {
pixelgl.Run(run)
}