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
whichOn := false
onNumber := 0
jetpackName := "jetpack.png"
jetpackOffName := "jetpack.png"
jetpackOn1Name := "jetpack-on.png"
jetpackOn2Name := "jetpack-on2.png"
camVector := win.Bounds().Center()
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
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!")
currentSprite := jetpackOff
// Game Loop
for !win.Closed() {
win.Update()
@ -132,26 +150,20 @@ func run() {
if jetpackOn {
velY += jetAcc
whichOn = !whichOn
onNumber += 1
onNumber++
if onNumber == 5 { // every 5 frames, toggle anijetMation
onNumber = 0
if whichOn {
jetpackName = "jetpack-on.png"
currentSprite = jetpackOn1
} else {
jetpackName = "jetpack-on2.png"
currentSprite = jetpackOn2
}
}
} else {
jetpackName = "jetpack.png"
currentSprite = jetpackOff
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)
jetMat := pixel.IM
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))
txt.Draw(win, pixel.IM)
win.SetSmooth(false)
jetpack.Draw(win, jetMat)
currentSprite.Draw(win, jetMat)
}