made framerate independent
This commit is contained in:
parent
e478381144
commit
3959af0d2f
Binary file not shown.
Before Width: | Height: | Size: 14 KiB After Width: | Height: | Size: 39 KiB |
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"image"
|
"image"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
|
|
||||||
|
@ -24,8 +25,10 @@ func loadPicture(path string) (pixel.Picture, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
windowWidth = 300
|
windowWidth = 600
|
||||||
windowHeight = 225
|
windowHeight = 450
|
||||||
|
// This is the scrolling speed
|
||||||
|
linesPerSecond = 60
|
||||||
)
|
)
|
||||||
|
|
||||||
func run() {
|
func run() {
|
||||||
|
@ -55,11 +58,14 @@ func run() {
|
||||||
vector2 := pixel.V(windowWidth+(windowWidth/2), (windowHeight/2)+1)
|
vector2 := pixel.V(windowWidth+(windowWidth/2), (windowHeight/2)+1)
|
||||||
|
|
||||||
i := float64(0)
|
i := float64(0)
|
||||||
|
last := time.Now()
|
||||||
for !win.Closed() {
|
for !win.Closed() {
|
||||||
|
dt := time.Since(last).Seconds()
|
||||||
|
last = time.Now()
|
||||||
// When one of the backgrounds has completely scrolled, we swap displacement vectors,
|
// When one of the backgrounds has completely scrolled, we swap displacement vectors,
|
||||||
// so the backgrounds will swap positions too regarding the previous iteration,
|
// so the backgrounds will swap positions too regarding the previous iteration,
|
||||||
// thus making the background look like infinite.
|
// thus making the background endless.
|
||||||
if i == -windowWidth {
|
if i <= -windowWidth {
|
||||||
i = 0
|
i = 0
|
||||||
vector1, vector2 = vector2, vector1
|
vector1, vector2 = vector2, vector1
|
||||||
}
|
}
|
||||||
|
@ -67,7 +73,7 @@ func run() {
|
||||||
d := pixel.V(-i, 0)
|
d := pixel.V(-i, 0)
|
||||||
background1.Draw(win, pixel.IM.Moved(vector1.Sub(d)))
|
background1.Draw(win, pixel.IM.Moved(vector1.Sub(d)))
|
||||||
background2.Draw(win, pixel.IM.Moved(vector2.Sub(d)))
|
background2.Draw(win, pixel.IM.Moved(vector2.Sub(d)))
|
||||||
i--
|
i -= linesPerSecond * dt
|
||||||
win.Update()
|
win.Update()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue