Add FPS; Add turning that properly clips

This commit is contained in:
Logen Kain 2018-01-17 14:17:07 -07:00
parent fd504632d1
commit 94f88d9010

58
main.go
View File

@ -2,8 +2,10 @@
package main package main
import ( import (
"fmt"
"image" "image"
_ "image/png" _ "image/png"
"math"
"os" "os"
"time" "time"
@ -58,7 +60,18 @@ func run() {
player := pixel.NewSprite(spritesheet, shipFrames[0]) player := pixel.NewSprite(spritesheet, shipFrames[0])
angle := 0.0 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}
last := time.Now() last := time.Now()
for !win.Closed() { for !win.Closed() {
@ -67,21 +80,62 @@ func run() {
win.Clear(colornames.Whitesmoke) win.Clear(colornames.Whitesmoke)
myVec = pixel.Vec{playerX, playerY}
mat := pixel.IM mat := pixel.IM
mat = mat.Rotated(pixel.ZV, angle) mat = mat.Rotated(pixel.ZV, angle)
mat = mat.Scaled(pixel.ZV, 0.25).Moved(win.Bounds().Center()) mat = mat.Scaled(pixel.ZV, 0.25)
//mat = mat.Moved(win.Bounds().Center())
mat = mat.Moved(myVec)
player.Draw(win, mat) player.Draw(win, mat)
if win.Pressed(pixelgl.KeyLeft) { if win.Pressed(pixelgl.KeyLeft) {
//angle += 3 * dt
angle += 3 * dt angle += 3 * dt
} }
if win.Pressed(pixelgl.KeyRight) { if win.Pressed(pixelgl.KeyRight) {
angle -= 3 * dt angle -= 3 * dt
} }
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
}
win.Update() win.Update()
frames++
select {
case <-second:
win.SetTitle(fmt.Sprintf("%s | FPS: %d | Degrees: %f %f", cfgMain.Title,
frames, angleInDegrees, angle))
frames = 0
default:
}
} }
} }