Add some background color animation depending on level

This commit is contained in:
David96 2021-01-04 19:45:13 +01:00
parent f17b88a108
commit 9daa9af2e9
5 changed files with 176 additions and 5 deletions

View File

@ -1,17 +1,24 @@
import java.awt.Color;
import java.util.List; import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
class GameScreen class GameScreen implements LinearColorAnimator.AnimationListener
{ {
private final static int LEVEL_TIME = 10;
private final List<Drawable> _drawables; private final List<Drawable> _drawables;
private final Bird _bird; private final Bird _bird;
private final World _world; private final World _world;
private final LinearColorAnimator _backgroundAnimator;
private double _totalTime; private double _totalTime;
private int _try; private int _try;
private int _level;
public GameScreen() public GameScreen()
{ {
_drawables = new ArrayList<Drawable>(); _drawables = new ArrayList<Drawable>();
_backgroundAnimator = new LinearColorAnimator(Color.BLACK, Color.WHITE, 1, this);
_backgroundAnimator.start();
_bird = new Bird(this); _bird = new Bird(this);
_world = new World(this); _world = new World(this);
_totalTime = 0; _totalTime = 0;
@ -25,10 +32,24 @@ class GameScreen
{ {
_bird.update(elapsed); _bird.update(elapsed);
_world.update(elapsed); _world.update(elapsed);
_backgroundAnimator.update(elapsed);
_totalTime += elapsed; _totalTime += elapsed;
System.out.print("\r" + _try + " - Time survived: " + (int)(_totalTime * 1e-3) + "s, " + System.out.print("\r" + _try + " - Time survived: " + (int)(_totalTime * 1e-3) + "s, " +
"speed: " + (int)_world.getSpeed() + "px/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(); Rectangle birdRect = _bird.getRect();
if (_world.checkCollision(_bird.getRect()) if (_world.checkCollision(_bird.getRect())
|| birdRect.getY() + birdRect.getHeight() > TurtleWelt.HEIGHT || birdRect.getY() + birdRect.getHeight() > TurtleWelt.HEIGHT
@ -36,16 +57,32 @@ class GameScreen
{ {
System.out.print("\n"); System.out.print("\n");
++_try; ++_try;
_totalTime = 0; _totalTime = _level = 0;
_bird.reset(); _bird.reset();
_world.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) 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) for (Drawable d : _drawables)
{ {
d.draw(turtle, elapsed); d.draw(turtle, elapsed);

115
LinearColorAnimator.java Normal file
View File

@ -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();
}
}

View File

@ -86,4 +86,9 @@ class Rectangle implements Drawable {
_x = x; _x = x;
_y = y; _y = y;
} }
public void setColor(String c)
{
_color = c;
}
} }

View File

@ -8,6 +8,7 @@ class World implements Drawable
private static final int ACCELERATION = 2; private static final int ACCELERATION = 2;
private final ArrayList<Rectangle> _obstacles; private final ArrayList<Rectangle> _obstacles;
private String _obstacleColor;
private double _speed; private double _speed;
private final Random _random; private final Random _random;
@ -22,15 +23,28 @@ class World implements Drawable
public void reset() public void reset()
{ {
_obstacles.clear(); _obstacles.clear();
_obstacleColor = "schwarz";
createObstacle(TurtleWelt.WIDTH); createObstacle(TurtleWelt.WIDTH);
_speed = START_SPEED; _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) private void createObstacle(int x)
{ {
int gap = _random.nextInt(TurtleWelt.HEIGHT - 200) + 50; int gap = _random.nextInt(TurtleWelt.HEIGHT - 200) + 50;
_obstacles.add(new Rectangle(x, 0, 30, gap, "black", false)); _obstacles.add(new Rectangle(x, 0, 30, gap, _obstacleColor, false));
_obstacles.add(new Rectangle(x, gap + 100, 30, TurtleWelt.HEIGHT, "black", false)); _obstacles.add(new Rectangle(x, gap + 100, 30, TurtleWelt.HEIGHT, _obstacleColor, false));
} }
public void update(double elapsed) public void update(double elapsed)

Binary file not shown.