made framerate independent

This commit is contained in:
Sergio Vera Castellano 2017-12-28 10:43:17 +01:00
parent e478381144
commit 3959af0d2f
2 changed files with 11 additions and 5 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 39 KiB

View File

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