added direction change to sprite
This commit is contained in:
commit
c6b9153782
BIN
background.png
Normal file
BIN
background.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 17 KiB |
178
main.go
178
main.go
@ -1,30 +1,24 @@
|
|||||||
// main
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"image"
|
"image"
|
||||||
_ "image/png"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
_ "image/png"
|
||||||
|
|
||||||
"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 {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
img, _, err := image.Decode(file)
|
img, _, err := image.Decode(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -37,65 +31,143 @@ 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),
|
||||||
//Going to use different method then vsync
|
VSync: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
win, err := pixelgl.NewWindow(cfg)
|
win, err := pixelgl.NewWindow(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pic, err := loadPicture("hiking.png")
|
spritesheet, err := loadPicture("sprites.png")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
sprite := pixel.NewSprite(pic, pic.Bounds())
|
background, err := loadPicture("background.png")
|
||||||
|
|
||||||
//Create handler
|
if err != nil {
|
||||||
var handler Handler
|
panic(err)
|
||||||
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() {
|
|
||||||
|
|
||||||
now = time.Now().UnixNano()
|
|
||||||
dt += now-last
|
|
||||||
last = now
|
|
||||||
|
|
||||||
if (dt >= timePerFrame) {
|
|
||||||
dt -= 0
|
|
||||||
|
|
||||||
update(&handler)
|
|
||||||
render(handler, win)
|
|
||||||
}
|
}
|
||||||
|
bg := pixel.NewSprite(background, background.Bounds())
|
||||||
|
//batch := pixel.NewBatch(&pixel.TrianglesData{}, spritesheet)
|
||||||
|
|
||||||
|
/*
|
||||||
|
When creating the sprite, you need to know big each frame is inside the spritesheet.
|
||||||
|
If you use aseprite or some other pixel art program, you can find the number of pixels
|
||||||
|
within each frame. For our sprite sheet, each frame is 96 px wide and 96 px high. So
|
||||||
|
we increment it by 96 for both x and y so that it can get the position of the frame
|
||||||
|
at the bottom right corner.
|
||||||
|
*/
|
||||||
|
var spritesFrames []pixel.Rect
|
||||||
|
for x := spritesheet.Bounds().Min.X; x < spritesheet.Bounds().Max.X; x += 96 {
|
||||||
|
for y := spritesheet.Bounds().Min.Y; y < spritesheet.Bounds().Max.Y; y += 96 {
|
||||||
|
spritesFrames = append(spritesFrames, pixel.R(x, y, x+96, y+96))// (x, y, width, height) of frame
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Sprite := pixel.NewSprite(spritesheet, spritesFrames[3])
|
||||||
|
|
||||||
|
var (
|
||||||
|
camPos = pixel.ZV
|
||||||
|
//camSpeed = 500.0
|
||||||
|
camZoom = 0.3
|
||||||
|
camZoomSpeed = 1.2
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
frames = 0
|
||||||
|
second = time.Tick(time.Second)
|
||||||
|
)
|
||||||
|
playerX := win.Bounds().Max.X / 2
|
||||||
|
playerY := win.Bounds().Max.Y / 2
|
||||||
|
|
||||||
|
var (
|
||||||
|
playerXY = pixel.Vec{
|
||||||
|
playerX,
|
||||||
|
playerY,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
var fps int64 = 60
|
||||||
|
timePerFrame := 1000000000/fps
|
||||||
|
var lastTime int64 = time.Now().UnixNano()
|
||||||
|
var now int64
|
||||||
|
var dt int64
|
||||||
|
|
||||||
|
// Game Loop
|
||||||
|
for !win.Closed() {
|
||||||
|
now = time.Now().UnixNano()
|
||||||
|
dt += (now-lastTime)
|
||||||
|
lastTime = time.Now().UnixNano()
|
||||||
|
|
||||||
|
//Execute a frame.
|
||||||
|
if (dt >= timePerFrame) {
|
||||||
|
|
||||||
|
// *** Update begins *** //
|
||||||
|
|
||||||
|
playerXY = pixel.Vec{
|
||||||
|
playerX,
|
||||||
|
playerY,
|
||||||
|
}
|
||||||
|
cam := pixel.IM.Scaled(camPos, camZoom).Moved(win.Bounds().Center().Sub(camPos))
|
||||||
|
|
||||||
|
win.SetMatrix(cam)
|
||||||
|
/*
|
||||||
|
This is where Sprite and bg was originally. Creating them over and over inside
|
||||||
|
the main loop is not very good resource management. When i added my own dt, the game was
|
||||||
|
only going about 22 fps, when it should've been going 60. I moved them
|
||||||
|
out of the loop and created them up top. It fixed it and now runs faster.
|
||||||
|
|
||||||
|
the tutorial only has it in the loop with the exception that it creates it only
|
||||||
|
if you click the button to make a tree. It only created it once and added it.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//mouse := cam.Unproject(win.MousePosition())
|
||||||
|
|
||||||
|
|
||||||
|
speed := 2.0
|
||||||
|
if win.Pressed(pixelgl.KeyA) {//left
|
||||||
|
playerX -= speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[2])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyD) {//Right
|
||||||
|
playerX += speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[1])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyS) { //Down
|
||||||
|
playerY -= speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[3])
|
||||||
|
}
|
||||||
|
if win.Pressed(pixelgl.KeyW) { //up
|
||||||
|
playerY += speed
|
||||||
|
Sprite.Set(spritesheet, spritesFrames[0])
|
||||||
|
}
|
||||||
|
camPos.X = playerX
|
||||||
|
camPos.Y = playerY
|
||||||
|
camZoom *= math.Pow(camZoomSpeed, win.MouseScroll().Y)
|
||||||
|
|
||||||
|
// *** Update Ends *** //
|
||||||
|
|
||||||
|
// *** Render begin *** //
|
||||||
|
win.Clear(colornames.Forestgreen)
|
||||||
|
bg.Draw(win, pixel.IM.Moved(win.Bounds().Center()))
|
||||||
|
Sprite.Draw(win, pixel.IM.Scaled(pixel.ZV, 4).Moved(playerXY))
|
||||||
|
|
||||||
|
//batch.Draw(win)
|
||||||
|
win.Update()
|
||||||
|
|
||||||
|
// *** Render End *** //
|
||||||
|
|
||||||
|
frames++
|
||||||
|
select {
|
||||||
|
case <-second:
|
||||||
|
win.SetTitle(fmt.Sprintf("%s | FPS: %d", cfg.Title, frames))
|
||||||
|
frames = 0
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
BIN
sprites.png
Normal file
BIN
sprites.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 62 KiB |
Loading…
x
Reference in New Issue
Block a user