Add sprites; create spritesheet loader
This commit is contained in:
parent
a60e14f4d2
commit
53af30d6fb
BIN
assets/ship_sprite_sheet.png
Normal file
BIN
assets/ship_sprite_sheet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 MiB |
74
main.go
Normal file
74
main.go
Normal file
@ -0,0 +1,74 @@
|
||||
// main.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
_ "image/png"
|
||||
"os"
|
||||
|
||||
"github.com/faiface/pixel"
|
||||
"github.com/faiface/pixel/pixelgl"
|
||||
"golang.org/x/image/colornames"
|
||||
)
|
||||
|
||||
func loadSprite(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
|
||||
}
|
||||
|
||||
func createSpriteSheet(SpriteSheetImg pixel.Picture, spriteSizeX, spriteSizeY float64) []pixel.Rect {
|
||||
var frames []pixel.Rect
|
||||
|
||||
for x := SpriteSheetImg.Bounds().Min.X; x < SpriteSheetImg.Bounds().Max.X; x += spriteSizeX {
|
||||
for y := SpriteSheetImg.Bounds().Min.Y; y < SpriteSheetImg.Bounds().Max.Y; y += spriteSizeY {
|
||||
frames = append(frames, pixel.R(x, y, x+256, y+256))
|
||||
}
|
||||
}
|
||||
return frames
|
||||
}
|
||||
|
||||
func run() {
|
||||
|
||||
cfgMain := pixelgl.WindowConfig{
|
||||
Title: "AsterCal",
|
||||
Bounds: pixel.R(0, 0, 1024, 768),
|
||||
VSync: true,
|
||||
}
|
||||
|
||||
win, err := pixelgl.NewWindow(cfgMain)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
spritesheet, err := loadSprite("assets/ship_sprite_sheet.png")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
shipFrames := createSpriteSheet(spritesheet, 256, 256)
|
||||
|
||||
player := pixel.NewSprite(spritesheet, shipFrames[0])
|
||||
|
||||
for !win.Closed() {
|
||||
//dt := time.Since(last).Seconds()
|
||||
//last = time.Now()
|
||||
win.Clear(colornames.Whitesmoke)
|
||||
|
||||
player.Draw(win, pixel.IM.Scaled(pixel.ZV, 0.25).Moved(win.Bounds().Center()))
|
||||
|
||||
win.Update()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
pixelgl.Run(run)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user