import greenfoot.*;
public class Background extends World
{
private static final String bgImageName = "brick.jpg";
private static final double scrollSpeed = 2.5;
private static final int picWidth = (new GreenfootImage(bgImageName)).getWidth();
private GreenfootImage bgImage, bgBase;
private int scrollPosition = 0;
public Background()
{
super(800, 400, 1);
setBackground(bgImageName);
bgImage = new GreenfootImage(getBackground());
bgBase = new GreenfootImage(picWidth, getHeight());
bgBase.drawImage(bgImage, 0, 0);
}
public void act()
{
scrollPosition -= scrollSpeed;
while(scrollSpeed > 0 && scrollPosition < -picWidth) scrollPosition += picWidth;
while(scrollSpeed < 0 && scrollPosition > 0) scrollPosition -= picWidth;
paint(scrollPosition);
}
private void paint(int position)
{
GreenfootImage bg = getBackground();
bg.drawImage(bgBase, position, 0);
bg.drawImage(bgImage, position + picWidth, 0);
}
}
