public class game3 extends World
{
private int spawnX = 0;
private int spawnX2 = 0;
private int spawnX3 = 0;
private int spawnCounter = 0;
private int spawnTreasure = 0;
private Counter theCounter;
private static final String bgImageName = "watergoed.jpg";
private static final int scrollSpeed = 3;
private static final int picHeight = (new GreenfootImage(bgImageName)).getHeight();
private GreenfootImage bgImage, bgBase;
private int scrollPosition = 0;
private int timer = 3600;
TimerText timerText = new TimerText();
/**
* Dit is de constructor voor game3.
* We pakken ook de background image hier zodat we in een andere methode
* de background kunnen tekenen zodat het lijkt alsof die naar beneden scrolled.
*/
public game3()
{
super(900, 900, 1);
setBackground(bgImageName);
bgImage = new GreenfootImage(getBackground());
bgBase = new GreenfootImage(getWidth(), picHeight);
bgBase.drawImage(bgImage, 0, 0);
prepare();
addObject(timerText, 100, 15); //wherever
timerText.setText("Time left: " + (timer/60));
}
/**
* We returnen de waarde van theCounter hier.
*/
public Counter getCounter()
{
return theCounter;
}
/**
* In de act methode tekenen we de background image voor de scroll effect.
* We hebben ook de methode addObjects gebruikt om random objecten te spawnen in de wereld.
* Ook de werking van de timer staat erin.
*/
public void act()
{
addObjects();
if (timer%60==0) timerText.setText("Time left: " + (timer/60));
timerFunc();
scrollPosition += scrollSpeed;
while(scrollSpeed > 0 && scrollPosition > picHeight) scrollPosition -= picHeight;
while(scrollSpeed < 0 && scrollPosition < 0) scrollPosition += picHeight;
paint(scrollPosition);
}
}
/**
* Hier word de background image echt getekend.
*/
private void paint(int position)
{
GreenfootImage bg = getBackground();
bg.drawImage(bgBase,0, position);
bg.drawImage(bgImage, 0, position-(int)Math.signum(scrollSpeed)*picHeight);
}
