2018-01-16 22:57:14 -07:00
|
|
|
// main.go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-17 14:17:07 -07:00
|
|
|
"fmt"
|
2018-01-16 22:57:14 -07:00
|
|
|
"image"
|
|
|
|
_ "image/png"
|
2018-01-17 14:17:07 -07:00
|
|
|
"math"
|
2018-01-16 22:57:14 -07:00
|
|
|
"os"
|
2018-01-17 12:36:37 -07:00
|
|
|
"time"
|
2018-01-16 22:57:14 -07:00
|
|
|
|
|
|
|
"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])
|
|
|
|
|
2018-01-17 14:17:07 -07:00
|
|
|
var angle float64 = 0.0
|
|
|
|
var angleInDegrees float64 = 0
|
|
|
|
|
|
|
|
var (
|
|
|
|
frames = 0
|
|
|
|
second = time.Tick(time.Second)
|
|
|
|
)
|
|
|
|
|
|
|
|
playerX := win.Bounds().Max.X / 2
|
|
|
|
playerY := win.Bounds().Max.Y / 8
|
|
|
|
|
|
|
|
var myVec = pixel.Vec{playerX, playerY}
|
2018-01-17 12:36:37 -07:00
|
|
|
|
|
|
|
last := time.Now()
|
2018-01-16 22:57:14 -07:00
|
|
|
for !win.Closed() {
|
2018-01-17 12:36:37 -07:00
|
|
|
dt := time.Since(last).Seconds()
|
|
|
|
last = time.Now()
|
|
|
|
|
2018-01-16 22:57:14 -07:00
|
|
|
win.Clear(colornames.Whitesmoke)
|
|
|
|
|
2018-01-17 14:17:07 -07:00
|
|
|
myVec = pixel.Vec{playerX, playerY}
|
|
|
|
|
2018-01-17 12:36:37 -07:00
|
|
|
mat := pixel.IM
|
|
|
|
mat = mat.Rotated(pixel.ZV, angle)
|
2018-01-17 14:17:07 -07:00
|
|
|
mat = mat.Scaled(pixel.ZV, 0.25)
|
|
|
|
//mat = mat.Moved(win.Bounds().Center())
|
|
|
|
mat = mat.Moved(myVec)
|
2018-01-17 12:36:37 -07:00
|
|
|
|
|
|
|
player.Draw(win, mat)
|
|
|
|
|
|
|
|
if win.Pressed(pixelgl.KeyLeft) {
|
2018-01-17 14:17:07 -07:00
|
|
|
|
|
|
|
//angle += 3 * dt
|
2018-01-17 12:36:37 -07:00
|
|
|
angle += 3 * dt
|
|
|
|
}
|
|
|
|
if win.Pressed(pixelgl.KeyRight) {
|
|
|
|
angle -= 3 * dt
|
|
|
|
|
|
|
|
}
|
2018-01-17 14:17:07 -07:00
|
|
|
if win.Pressed(pixelgl.KeyUp) {
|
|
|
|
playerY += 3 * dt
|
|
|
|
|
|
|
|
}
|
|
|
|
if win.Pressed(pixelgl.KeyDown) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if angle == 0 {
|
|
|
|
angleInDegrees = 0
|
|
|
|
} else {
|
|
|
|
|
|
|
|
angleInDegrees = (angle) * 180 / math.Pi
|
|
|
|
}
|
|
|
|
|
|
|
|
if angleInDegrees == 360 || angleInDegrees == -360 {
|
|
|
|
angle = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
if angleInDegrees > 360 {
|
|
|
|
angle = angle - (360*math.Pi)/180
|
|
|
|
}
|
|
|
|
if angleInDegrees < -360 {
|
|
|
|
angle = angle + (360*math.Pi)/180
|
|
|
|
}
|
2018-01-16 22:57:14 -07:00
|
|
|
|
|
|
|
win.Update()
|
2018-01-17 14:17:07 -07:00
|
|
|
|
|
|
|
frames++
|
|
|
|
select {
|
|
|
|
case <-second:
|
|
|
|
win.SetTitle(fmt.Sprintf("%s | FPS: %d | Degrees: %f %f", cfgMain.Title,
|
|
|
|
frames, angleInDegrees, angle))
|
|
|
|
frames = 0
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2018-01-16 22:57:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
pixelgl.Run(run)
|
|
|
|
}
|