fixed accessing png files from disk every frame

This commit is contained in:
Brandon 2018-06-25 11:51:55 -06:00
parent f33825d48e
commit 31192ae8e8
1 changed files with 24 additions and 12 deletions

View File

@ -88,15 +88,33 @@ func run() {
tilt := 0.01 // Default: 0.001 tilt := 0.01 // Default: 0.001
whichOn := false whichOn := false
onNumber := 0 onNumber := 0
jetpackName := "jetpack.png" jetpackOffName := "jetpack.png"
jetpackOn1Name := "jetpack-on.png"
jetpackOn2Name := "jetpack-on2.png"
camVector := win.Bounds().Center() camVector := win.Bounds().Center()
bg, _ := loadSprite("sky.png") bg, _ := loadSprite("sky.png")
// Jetpack - Rendering
jetpackOff, err := loadSprite(jetpackOffName)
if err != nil {
panic(err)
}
jetpackOn1, err := loadSprite(jetpackOn1Name)
if err != nil {
panic(err)
}
jetpackOn2, err := loadSprite(jetpackOn2Name)
if err != nil {
panic(err)
}
// Tutorial Text // Tutorial Text
txt := loadTTF("intuitive.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200)) txt := loadTTF("intuitive.ttf", 50, pixel.V(win.Bounds().Center().X-450, win.Bounds().Center().Y-200))
fmt.Fprintf(txt, "Explore the Skies with WASD or Arrow Keys!") fmt.Fprintf(txt, "Explore the Skies with WASD or Arrow Keys!")
currentSprite := jetpackOff
// Game Loop // Game Loop
for !win.Closed() { for !win.Closed() {
win.Update() win.Update()
@ -132,26 +150,20 @@ func run() {
if jetpackOn { if jetpackOn {
velY += jetAcc velY += jetAcc
whichOn = !whichOn whichOn = !whichOn
onNumber += 1 onNumber++
if onNumber == 5 { // every 5 frames, toggle anijetMation if onNumber == 5 { // every 5 frames, toggle anijetMation
onNumber = 0 onNumber = 0
if whichOn { if whichOn {
jetpackName = "jetpack-on.png" currentSprite = jetpackOn1
} else { } else {
jetpackName = "jetpack-on2.png" currentSprite = jetpackOn2
} }
} }
} else { } else {
jetpackName = "jetpack.png" currentSprite = jetpackOff
velY -= gravity velY -= gravity
} }
// Jetpack - Rendering
jetpack, err := loadSprite(jetpackName)
if err != nil {
panic(err)
}
positionVector := pixel.V(win.Bounds().Center().X+jetX, win.Bounds().Center().Y+jetY-372) positionVector := pixel.V(win.Bounds().Center().X+jetX, win.Bounds().Center().Y+jetY-372)
jetMat := pixel.IM jetMat := pixel.IM
jetMat = jetMat.Scaled(pixel.ZV, 4) jetMat = jetMat.Scaled(pixel.ZV, 4)
@ -185,7 +197,7 @@ func run() {
bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10)) bg.Draw(win, pixel.IM.Moved(pixel.V(win.Bounds().Center().X, win.Bounds().Center().Y+766)).Scaled(pixel.ZV, 10))
txt.Draw(win, pixel.IM) txt.Draw(win, pixel.IM)
win.SetSmooth(false) win.SetSmooth(false)
jetpack.Draw(win, jetMat) currentSprite.Draw(win, jetMat)
} }