diff --git a/community/scrolling-background/gamebackground.jpg b/community/scrolling-background/gamebackground.jpg index e5eab2b..050632d 100644 Binary files a/community/scrolling-background/gamebackground.jpg and b/community/scrolling-background/gamebackground.jpg differ diff --git a/community/scrolling-background/main.go b/community/scrolling-background/main.go index 9b402fa..b606b2f 100644 --- a/community/scrolling-background/main.go +++ b/community/scrolling-background/main.go @@ -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() } }