import java.awt.Color; import java.util.List; import java.util.ArrayList; class GameScreen implements LinearColorAnimator.AnimationListener { private static class Level { public String birdColor, birdColorTransition; public String obstacleColor, obstacleColorTransition; public Color backgroundColor; public Level(String bird, String birdTrans, String obst, String obstTrans, Color bg) { birdColor = bird; birdColorTransition = birdTrans; obstacleColor = obst; obstacleColorTransition = obstTrans; backgroundColor = bg; } } private final static ArrayList LEVELS = new ArrayList(); static { LEVELS.add(new Level("cyan", "cyan", "schwarz", "schwarz", Color.WHITE)); LEVELS.add(new Level("weiss", "grau", "weiss", "grau", Color.BLACK)); LEVELS.add(new Level("orange", "rot", "orange", "rot", Color.RED)); LEVELS.add(new Level("cyan", "orange", "cyan", "orange", Color.BLACK)); }; // Time in seconds until new level is reached // You might wanna set that to 3 or so to enjoy more rapid color changes private final static int LEVEL_TIME = 10; private final List _drawables; private final Bird _bird; private final World _world; private final LinearColorAnimator _backgroundAnimator; private double _totalTime; private int _try; private int _level; public GameScreen() { _drawables = new ArrayList(); _backgroundAnimator = new LinearColorAnimator(Color.BLACK, LEVELS.get(0).backgroundColor, 1, this); _backgroundAnimator.start(); _bird = new Bird(this, LEVELS.get(0).birdColor); _world = new World(this); _world.setObstacleColor(LEVELS.get(0).obstacleColor); _totalTime = 0; _try = 1; } /* * Elapsed time is expected to be in ms */ public void update(double elapsed) { _bird.update(elapsed); _world.update(elapsed); _backgroundAnimator.update(elapsed); _totalTime += elapsed; System.out.print("\r" + _try + " - Time survived: " + (int)(_totalTime * 1e-3) + "s, " + "speed: " + (int)_world.getSpeed() + "px/s"); if (_totalTime / (LEVEL_TIME * 1e3) > _level + 1) { ++_level; if (_level < LEVELS.size()) { _world.setObstacleColor(LEVELS.get(_level).obstacleColorTransition); _bird.setColor(LEVELS.get(_level).birdColorTransition); _backgroundAnimator.setStart(LEVELS.get(_level-1).backgroundColor); _backgroundAnimator.setEnd(LEVELS.get(_level).backgroundColor); _backgroundAnimator.start(); } } Rectangle birdRect = _bird.getRect(); if (_world.checkCollision(_bird.getRect()) || birdRect.getY() > TurtleWelt.HEIGHT || birdRect.getY() + birdRect.getHeight() < 0) { System.out.print("\n"); ++_try; _totalTime = _level = 0; _bird.reset(); _world.reset(); _bird.setColor(LEVELS.get(0).birdColor); // If the background is currently not white, smoothly animate it back if (_backgroundAnimator.getCurrentColor().getRGB() != Color.WHITE.getRGB()) { _backgroundAnimator.setStart(_backgroundAnimator.getCurrentColor()); _backgroundAnimator.setEnd(LEVELS.get(0).backgroundColor); _backgroundAnimator.start(); } } } public void onAnimationFinished() { if (_level < LEVELS.size()) { _world.setObstacleColor(LEVELS.get(_level).obstacleColor); _bird.setColor(LEVELS.get(_level).birdColor); } } public void draw(Turtle turtle, double elapsed) { Color bg = _backgroundAnimator.getCurrentColor(); TurtleWelt.globaleWelt.bildschirmEinfaerben(bg.getRed(), bg.getGreen(), bg.getBlue()); for (Drawable d : _drawables) { d.draw(turtle, elapsed); } } public void addDrawable(Drawable d) { _drawables.add(d); } public void jump() { _bird.jump(); } }