Added a handler, update, and render
This commit is contained in:
parent
736ffaa9da
commit
b2e8ec4f3d
52
main.go
52
main.go
@ -5,12 +5,20 @@ import (
|
|||||||
"image"
|
"image"
|
||||||
_ "image/png"
|
_ "image/png"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/faiface/pixel"
|
"github.com/faiface/pixel"
|
||||||
"github.com/faiface/pixel/pixelgl"
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"golang.org/x/image/colornames"
|
"golang.org/x/image/colornames"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//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
|
||||||
|
}
|
||||||
|
|
||||||
func loadPicture(path string) (pixel.Picture, error) {
|
func loadPicture(path string) (pixel.Picture, error) {
|
||||||
file, err := os.Open(path)
|
file, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -29,7 +37,7 @@ func run() {
|
|||||||
cfg := pixelgl.WindowConfig{
|
cfg := pixelgl.WindowConfig{
|
||||||
Title: "Pixel Rocks!",
|
Title: "Pixel Rocks!",
|
||||||
Bounds: pixel.R(0, 0, 1024, 768),
|
Bounds: pixel.R(0, 0, 1024, 768),
|
||||||
VSync: true,
|
//Going to use different method then vsync
|
||||||
}
|
}
|
||||||
|
|
||||||
win, err := pixelgl.NewWindow(cfg)
|
win, err := pixelgl.NewWindow(cfg)
|
||||||
@ -44,14 +52,50 @@ func run() {
|
|||||||
|
|
||||||
sprite := pixel.NewSprite(pic, pic.Bounds())
|
sprite := pixel.NewSprite(pic, pic.Bounds())
|
||||||
|
|
||||||
win.Clear(colornames.Skyblue)
|
//Create handler
|
||||||
sprite.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
var handler Handler
|
||||||
|
handler.s = sprite
|
||||||
|
handler.x = 0
|
||||||
|
handler.y = 0
|
||||||
|
|
||||||
|
//Game Loop -- Should be 60 fps
|
||||||
|
var timePerFrame int64 = 1000000000/60
|
||||||
|
last := time.Now().UnixNano()
|
||||||
|
var now, dt int64
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
win.Update()
|
|
||||||
|
now = time.Now().UnixNano()
|
||||||
|
dt += now-last
|
||||||
|
last = now
|
||||||
|
|
||||||
|
if (dt >= timePerFrame) {
|
||||||
|
dt -= 0
|
||||||
|
|
||||||
|
update(&handler)
|
||||||
|
render(handler, win)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
pixelgl.Run(run)
|
pixelgl.Run(run)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
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()
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user