Small adjustments

This commit is contained in:
Sergio Vera Castellano 2018-01-12 12:19:58 +01:00
parent bdbc1220c6
commit fe852f27d5
3 changed files with 55 additions and 31 deletions

View File

@ -2,6 +2,6 @@
Created by [Sergio Vera](https://github.com/svera) Created by [Sergio Vera](https://github.com/svera)
This example shows how to implement an infinite side scrolling background with a depth effect, using [parallax scrolling](https://en.wikipedia.org/wiki/Parallax_scrolling). This example shows how to implement an infinite side scrolling background with a depth effect, using [parallax scrolling](https://en.wikipedia.org/wiki/Parallax_scrolling). Code is based in the [infinite scrolling background](https://github.com/faiface/pixel/tree/master/examples/community/scrolling-background) demo.
Credits to [Peter Hellberg](https://github.com/peterhellberg) for the reworked background image. Credits to [Peter Hellberg](https://github.com/peterhellberg) for the improved background images.

View File

@ -29,8 +29,10 @@ const (
windowHeight = 450 windowHeight = 450
foregroundHeight = 149 foregroundHeight = 149
// This is the scrolling speed (pixels per second) // This is the scrolling speed (pixels per second)
backgroundSpeed = 60 // Negative values will make background to scroll to the left,
foregroundSpeed = 120 // positive to the right.
backgroundSpeed = -60
foregroundSpeed = -120
) )
func run() { func run() {
@ -54,15 +56,15 @@ func run() {
panic(err) panic(err)
} }
background := newScrollingBackground(picBackground, windowWidth, windowHeight, windowWidth) background := NewScrollingBackground(picBackground, windowWidth, windowHeight, backgroundSpeed)
foreground := newScrollingBackground(picForeground, windowWidth, foregroundHeight, windowWidth) foreground := NewScrollingBackground(picForeground, windowWidth, foregroundHeight, foregroundSpeed)
last := time.Now() last := time.Now()
for !win.Closed() { for !win.Closed() {
dt := time.Since(last).Seconds() dt := time.Since(last).Seconds()
last = time.Now() last = time.Now()
background.update(win, backgroundSpeed, dt) background.Update(win, dt)
foreground.update(win, foregroundSpeed, dt) foreground.Update(win, dt)
win.Update() win.Update()
} }
} }

View File

@ -1,42 +1,64 @@
package main package main
import ( import (
"math"
"github.com/faiface/pixel" "github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl" "github.com/faiface/pixel/pixelgl"
) )
type scrollingBackground struct { // ScrollingBackground stores all needed information to scroll a background
width float64 // to the left or right
height float64 type ScrollingBackground struct {
windowWidth float64 width float64
displacementCounter float64 height float64
backgrounds [2]*pixel.Sprite displacement float64
positions [2]pixel.Vec speed float64
backgrounds [2]*pixel.Sprite
positions [2]pixel.Vec
} }
func newScrollingBackground(pic pixel.Picture, width, height, windowWidth float64) *scrollingBackground { // NewScrollingBackground construct and returns a new instance of scrollingBackground,
return &scrollingBackground{ // positioning the background images according to the speed value
width: width, func NewScrollingBackground(pic pixel.Picture, width, height, speed float64) *ScrollingBackground {
height: height, sb := &ScrollingBackground{
windowWidth: windowWidth, width: width,
height: height,
speed: speed,
backgrounds: [2]*pixel.Sprite{ backgrounds: [2]*pixel.Sprite{
pixel.NewSprite(pic, pixel.R(0, 0, width, height)), pixel.NewSprite(pic, pixel.R(0, 0, width, height)),
pixel.NewSprite(pic, pixel.R(width, 0, width*2, height)), pixel.NewSprite(pic, pixel.R(width, 0, width*2, height)),
}, },
positions: [2]pixel.Vec{ }
pixel.V(width/2, (height/2)+1),
pixel.V(width+(width/2), (height/2)+1), sb.positionImages()
}, return sb
}
// If scrolling speed > 0, put second background image ouside the screen,
// at the left side, otherwise put it at the right side.
func (sb *ScrollingBackground) positionImages() {
if sb.speed > 0 {
sb.positions = [2]pixel.Vec{
pixel.V(sb.width/2, (sb.height/2)+1),
pixel.V((sb.width/2)-sb.width, (sb.height/2)+1),
}
} else {
sb.positions = [2]pixel.Vec{
pixel.V(sb.width/2, (sb.height/2)+1),
pixel.V(sb.width+(sb.width/2), (sb.height/2)+1),
}
} }
} }
func (sb *scrollingBackground) update(win *pixelgl.Window, speed, dt float64) { // Update will move backgrounds certain pixels, depending of the amount of time passed
if sb.displacementCounter <= -sb.windowWidth { func (sb *ScrollingBackground) Update(win *pixelgl.Window, dt float64) {
sb.displacementCounter = 0 if math.Abs(sb.displacement) >= sb.width {
sb.displacement = 0
sb.positions[0], sb.positions[1] = sb.positions[1], sb.positions[0] sb.positions[0], sb.positions[1] = sb.positions[1], sb.positions[0]
} }
d := pixel.V(-sb.displacementCounter, 0) d := pixel.V(sb.displacement, 0)
sb.backgrounds[0].Draw(win, pixel.IM.Moved(sb.positions[0].Sub(d))) sb.backgrounds[0].Draw(win, pixel.IM.Moved(sb.positions[0].Add(d)))
sb.backgrounds[1].Draw(win, pixel.IM.Moved(sb.positions[1].Sub(d))) sb.backgrounds[1].Draw(win, pixel.IM.Moved(sb.positions[1].Add(d)))
sb.displacementCounter -= speed * dt sb.displacement += sb.speed * dt
} }