diff --git a/GameScreen.java b/GameScreen.java index 0ca6fe0..570f40a 100644 --- a/GameScreen.java +++ b/GameScreen.java @@ -1,17 +1,24 @@ +import java.awt.Color; import java.util.List; import java.util.ArrayList; -class GameScreen +class GameScreen implements LinearColorAnimator.AnimationListener { + 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, Color.WHITE, 1, this); + _backgroundAnimator.start(); _bird = new Bird(this); _world = new World(this); _totalTime = 0; @@ -25,10 +32,24 @@ class GameScreen { _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; + // Would be nice to add some more levels with different colors + if (_level == 1) + { + _world.setObstacleColor("grau"); + _backgroundAnimator.setStart(Color.WHITE); + _backgroundAnimator.setEnd(Color.BLACK); + _backgroundAnimator.start(); + } + } + Rectangle birdRect = _bird.getRect(); if (_world.checkCollision(_bird.getRect()) || birdRect.getY() + birdRect.getHeight() > TurtleWelt.HEIGHT @@ -36,16 +57,32 @@ class GameScreen { System.out.print("\n"); ++_try; - _totalTime = 0; + _totalTime = _level = 0; _bird.reset(); _world.reset(); + + // If the background is currently not white, smoothly animate it back + if (_backgroundAnimator.getCurrentColor().getRGB() != Color.WHITE.getRGB()) + { + _backgroundAnimator.setStart(_backgroundAnimator.getCurrentColor()); + _backgroundAnimator.setEnd(Color.WHITE); + _backgroundAnimator.start(); + } } } + public void onAnimationFinished() + { + if (_backgroundAnimator.getEnd().getRGB() == Color.BLACK.getRGB()) + { + _world.setObstacleColor("weiss"); + } + } public void draw(Turtle turtle, double elapsed) { - TurtleWelt.globaleWelt.bildschirmEinfaerben(255, 255, 255); + Color bg = _backgroundAnimator.getCurrentColor(); + TurtleWelt.globaleWelt.bildschirmEinfaerben(bg.getRed(), bg.getGreen(), bg.getBlue()); for (Drawable d : _drawables) { d.draw(turtle, elapsed); diff --git a/LinearColorAnimator.java b/LinearColorAnimator.java new file mode 100644 index 0000000..a7ce786 --- /dev/null +++ b/LinearColorAnimator.java @@ -0,0 +1,115 @@ +import java.awt.Color; + +class LinearColorAnimator +{ + private Color _start, _end; + private final AnimationListener _listener; + private int _duration; + private double _elapsed; + private boolean _started; + private boolean _finished; + + /* + * @param duration Duration in seconds + */ + public LinearColorAnimator(Color start, Color end, int duration, AnimationListener listener) + { + _start = start; + _end = end; + _duration = duration; + _listener = listener; + } + + /* + * @param elapsed Elapsed time since last call in ms + */ + public void update(double elapsed) + { + if (_started) + { + _elapsed += elapsed * 1e-3; + + if (_elapsed > _duration) + { + _started = false; + _finished = true; + if (_listener != null) + { + _listener.onAnimationFinished(); + } + } + } + } + + public void start() + { + _started = true; + _finished = false; + _elapsed = 0; + } + + public void reset() + { + _started = _finished = false; + _elapsed = 0; + } + + public void setStart(Color c) + { + _start = c; + } + + public void setEnd(Color c) + { + _end = c; + } + + public void setDuration(int duration) + { + _duration = duration; + } + + public boolean hasStarted() + { + return _started; + } + + public boolean hasFinished() + { + return _finished; + } + + public Color getStart() + { + return _start; + } + + public Color getEnd() + { + return _end; + } + + public Color getCurrentColor() + { + if (_finished) + { + return _end; + } + if (!_started) + { + return _start; + } + int diffR = _end.getRed() - _start.getRed(); + int diffG = _end.getGreen() - _start.getGreen(); + int diffB = _end.getBlue() - _start.getBlue(); + double elapsedRatio = _elapsed / _duration; + return new Color((int)(_start.getRed() + diffR * elapsedRatio), + (int)(_start.getGreen() + diffG * elapsedRatio), + (int)(_start.getBlue() + diffB * elapsedRatio)); + } + + interface AnimationListener + { + public abstract void onAnimationFinished(); + } +} diff --git a/Rectangle.java b/Rectangle.java index 61bf1e7..df51fd5 100644 --- a/Rectangle.java +++ b/Rectangle.java @@ -86,4 +86,9 @@ class Rectangle implements Drawable { _x = x; _y = y; } + + public void setColor(String c) + { + _color = c; + } } diff --git a/World.java b/World.java index 4c7411c..2318bec 100644 --- a/World.java +++ b/World.java @@ -8,6 +8,7 @@ class World implements Drawable private static final int ACCELERATION = 2; private final ArrayList _obstacles; + private String _obstacleColor; private double _speed; private final Random _random; @@ -22,15 +23,28 @@ class World implements Drawable public void reset() { _obstacles.clear(); + _obstacleColor = "schwarz"; createObstacle(TurtleWelt.WIDTH); _speed = START_SPEED; } + public void setObstacleColor(String c) + { + for (Drawable d : _obstacles) + { + if (d instanceof Rectangle) + { + ((Rectangle)d).setColor(c); + } + } + _obstacleColor = c; + } + private void createObstacle(int x) { int gap = _random.nextInt(TurtleWelt.HEIGHT - 200) + 50; - _obstacles.add(new Rectangle(x, 0, 30, gap, "black", false)); - _obstacles.add(new Rectangle(x, gap + 100, 30, TurtleWelt.HEIGHT, "black", false)); + _obstacles.add(new Rectangle(x, 0, 30, gap, _obstacleColor, false)); + _obstacles.add(new Rectangle(x, gap + 100, 30, TurtleWelt.HEIGHT, _obstacleColor, false)); } public void update(double elapsed) diff --git a/build/FlappyTurtle.jar b/build/FlappyTurtle.jar index 76a3f91..1c15ed4 100644 Binary files a/build/FlappyTurtle.jar and b/build/FlappyTurtle.jar differ