project-rpg/main.go

101 lines
1.6 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"
"os"
2018-01-16 07:35:04 -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 07:35:04 -07:00
//something like this should handle everything within the game.
//this is not the final form. I was just testing things.
type Handler struct {
s *pixel.Sprite
x, y float64
}
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
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, err
}
return pixel.PictureDataFromImage(img), nil
}
2018-01-15 22:42:11 -07:00
func run() {
cfg := pixelgl.WindowConfig{
Title: "Pixel Rocks!",
Bounds: pixel.R(0, 0, 1024, 768),
2018-01-16 07:35:04 -07:00
//Going to use different method then vsync
2018-01-15 22:42:11 -07:00
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
panic(err)
}
2018-01-15 23:20:34 -07:00
pic, err := loadPicture("hiking.png")
if err != nil {
panic(err)
}
sprite := pixel.NewSprite(pic, pic.Bounds())
2018-01-16 07:35:04 -07:00
//Create handler
var handler Handler
handler.s = sprite
handler.x = 0
handler.y = 0
2018-01-15 22:50:18 -07:00
2018-01-16 07:35:04 -07:00
//Game Loop -- Should be 60 fps
var timePerFrame int64 = 1000000000/60
last := time.Now().UnixNano()
var now, dt int64
2018-01-15 22:42:11 -07:00
for !win.Closed() {
2018-01-16 07:35:04 -07:00
now = time.Now().UnixNano()
dt += now-last
last = now
if (dt >= timePerFrame) {
dt -= 0
update(&handler)
render(handler, win)
}
2018-01-15 22:42:11 -07:00
}
}
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
}
2018-01-16 07:35:04 -07:00
func update(handler *Handler) {
handler.x += 0.5
handler.y += 0.5
}
func render(handler Handler, win *pixelgl.Window) {
win.Clear(colornames.Skyblue)
//Draw here---
handler.s.Draw(win, pixel.IM.Moved(pixel.V(handler.x,handler.y)))
//---DrawEnd
//update
win.Update()
}