Use proper Color class to animate everything
Don't need to care about not modifying Turtle.java anymore
This commit is contained in:
parent
d5683e2153
commit
4a1bbf2be0
|
|
@ -1,3 +1,5 @@
|
|||
import java.awt.Color;
|
||||
|
||||
public class Bird
|
||||
{
|
||||
private static final int GRAVITY = 20;
|
||||
|
|
@ -5,7 +7,7 @@ public class Bird
|
|||
private final Rectangle _rect;
|
||||
private double _speed;
|
||||
|
||||
public Bird(GameScreen game, String color)
|
||||
public Bird(GameScreen game, Color color)
|
||||
{
|
||||
_rect = new Rectangle(20, 20, 20, 20, color, true);
|
||||
game.addDrawable(_rect);
|
||||
|
|
@ -28,7 +30,7 @@ public class Bird
|
|||
_speed = -JUMP_SPEED;
|
||||
}
|
||||
|
||||
public void setColor(String color)
|
||||
public void setColor(Color color)
|
||||
{
|
||||
_rect.setColor(color);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,16 +6,14 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
{
|
||||
private static class Level
|
||||
{
|
||||
public String birdColor, birdColorTransition;
|
||||
public String obstacleColor, obstacleColorTransition;
|
||||
public Color birdColor;
|
||||
public Color obstacleColor;
|
||||
public Color backgroundColor;
|
||||
|
||||
public Level(String bird, String birdTrans, String obst, String obstTrans, Color bg)
|
||||
public Level(Color bird, Color obst, Color bg)
|
||||
{
|
||||
birdColor = bird;
|
||||
birdColorTransition = birdTrans;
|
||||
obstacleColor = obst;
|
||||
obstacleColorTransition = obstTrans;
|
||||
backgroundColor = bg;
|
||||
}
|
||||
}
|
||||
|
|
@ -23,10 +21,10 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
private final static ArrayList<Level> LEVELS = new ArrayList<Level>();
|
||||
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));
|
||||
LEVELS.add(new Level(Color.CYAN, Color.BLACK, Color.WHITE));
|
||||
LEVELS.add(new Level(Color.WHITE, Color.WHITE, Color.BLACK));
|
||||
LEVELS.add(new Level(Color.ORANGE, Color.ORANGE, Color.RED));
|
||||
LEVELS.add(new Level(Color.CYAN, Color.CYAN, 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
|
||||
|
|
@ -35,7 +33,7 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
private final List<Drawable> _drawables;
|
||||
private final Bird _bird;
|
||||
private final World _world;
|
||||
private final LinearColorAnimator _backgroundAnimator;
|
||||
private final LinearColorAnimator _backgroundAnimator, _birdAnimator, _obstacleAnimator;
|
||||
private double _totalTime;
|
||||
private int _try;
|
||||
private int _level;
|
||||
|
|
@ -44,6 +42,8 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
{
|
||||
_drawables = new ArrayList<Drawable>();
|
||||
_backgroundAnimator = new LinearColorAnimator(Color.BLACK, LEVELS.get(0).backgroundColor, 1, this);
|
||||
_birdAnimator = new LinearColorAnimator(LEVELS.get(0).birdColor, LEVELS.get(0).birdColor, 1, this);
|
||||
_obstacleAnimator = new LinearColorAnimator(LEVELS.get(0).obstacleColor, LEVELS.get(0).obstacleColor, 1, this);
|
||||
_backgroundAnimator.start();
|
||||
_bird = new Bird(this, LEVELS.get(0).birdColor);
|
||||
_world = new World(this);
|
||||
|
|
@ -52,6 +52,16 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
_try = 1;
|
||||
}
|
||||
|
||||
private void animateBack(LinearColorAnimator animator, Color target)
|
||||
{
|
||||
if (animator.getCurrentColor().getRGB() != target.getRGB())
|
||||
{
|
||||
animator.setStart(animator.getCurrentColor());
|
||||
animator.setEnd(target);
|
||||
animator.start();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Elapsed time is expected to be in ms
|
||||
*/
|
||||
|
|
@ -60,6 +70,8 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
_bird.update(elapsed);
|
||||
_world.update(elapsed);
|
||||
_backgroundAnimator.update(elapsed);
|
||||
_birdAnimator.update(elapsed);
|
||||
_obstacleAnimator.update(elapsed);
|
||||
_totalTime += elapsed;
|
||||
System.out.print("\r" + _try + " - Time survived: " + (int)(_totalTime * 1e-3) + "s, " +
|
||||
"speed: " + (int)_world.getSpeed() + "px/s");
|
||||
|
|
@ -69,11 +81,15 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
++_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();
|
||||
_birdAnimator.setStart(LEVELS.get(_level-1).birdColor);
|
||||
_birdAnimator.setEnd(LEVELS.get(_level).birdColor);
|
||||
_birdAnimator.start();
|
||||
_obstacleAnimator.setStart(LEVELS.get(_level-1).obstacleColor);
|
||||
_obstacleAnimator.setEnd(LEVELS.get(_level).obstacleColor);
|
||||
_obstacleAnimator.start();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -90,28 +106,27 @@ class GameScreen implements LinearColorAnimator.AnimationListener
|
|||
_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();
|
||||
}
|
||||
animateBack(_backgroundAnimator, LEVELS.get(0).backgroundColor);
|
||||
animateBack(_birdAnimator, LEVELS.get(0).birdColor);
|
||||
animateBack(_obstacleAnimator, LEVELS.get(0).obstacleColor);
|
||||
}
|
||||
}
|
||||
|
||||
public void onAnimationFinished()
|
||||
{
|
||||
if (_level < LEVELS.size())
|
||||
{
|
||||
_world.setObstacleColor(LEVELS.get(_level).obstacleColor);
|
||||
_bird.setColor(LEVELS.get(_level).birdColor);
|
||||
}
|
||||
}
|
||||
public void onAnimationFinished() {}
|
||||
|
||||
public void draw(Turtle turtle, double elapsed)
|
||||
{
|
||||
Color bg = _backgroundAnimator.getCurrentColor();
|
||||
TurtleWelt.globaleWelt.bildschirmEinfaerben(bg.getRed(), bg.getGreen(), bg.getBlue());
|
||||
|
||||
if (!_birdAnimator.hasFinished())
|
||||
{
|
||||
_bird.setColor(_birdAnimator.getCurrentColor());
|
||||
}
|
||||
if (!_obstacleAnimator.hasFinished())
|
||||
{
|
||||
_world.setObstacleColor(_obstacleAnimator.getCurrentColor());
|
||||
}
|
||||
for (Drawable d : _drawables)
|
||||
{
|
||||
d.draw(turtle, elapsed);
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
import java.awt.Color;
|
||||
|
||||
class IntroScreen
|
||||
{
|
||||
private boolean _introDrawn;
|
||||
|
|
@ -57,7 +59,7 @@ class IntroScreen
|
|||
TurtleWelt.globaleWelt.bildschirmEinfaerben(0, 0, 0);
|
||||
_introDrawn = true;
|
||||
|
||||
t.setzeFarbe("gruen");
|
||||
t.setzeFarbe(Color.GREEN);
|
||||
// F
|
||||
goTo(t, 100, 100);
|
||||
t.setzeGeschwindigkeit(1);
|
||||
|
|
@ -119,7 +121,7 @@ class IntroScreen
|
|||
t.setzeRichtung(-90);
|
||||
t.geheVor(50);
|
||||
|
||||
t.setzeFarbe("blau");
|
||||
t.setzeFarbe(Color.BLUE);
|
||||
// T
|
||||
goTo(t, 50, 210);
|
||||
drawT(t);
|
||||
|
|
|
|||
|
|
@ -30,7 +30,6 @@ class Manager implements KeyListener
|
|||
// a KeyListener lol
|
||||
try
|
||||
{
|
||||
|
||||
Field frameField = TurtleWelt.class.getDeclaredField("_frame");
|
||||
|
||||
frameField.setAccessible(true);
|
||||
|
|
|
|||
|
|
@ -1,11 +1,12 @@
|
|||
import java.awt.Color;
|
||||
|
||||
class Rectangle implements Drawable {
|
||||
private double _x, _y;
|
||||
private double _width, _height;
|
||||
private String _color;
|
||||
private Color _color;
|
||||
private boolean _fill;
|
||||
|
||||
public Rectangle(double x, double y, double w, double h, String c, boolean fill)
|
||||
public Rectangle(double x, double y, double w, double h, Color c, boolean fill)
|
||||
{
|
||||
_x = x;
|
||||
_y = y;
|
||||
|
|
@ -87,7 +88,7 @@ class Rectangle implements Drawable {
|
|||
_y = y;
|
||||
}
|
||||
|
||||
public void setColor(String c)
|
||||
public void setColor(Color c)
|
||||
{
|
||||
_color = c;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -174,12 +174,17 @@ class Turtle
|
|||
* Moegliche Farben sind "schwarz", "blau", "cyan", "dunkelgrau", "grau", "gruen", "hellgrau", "magenta", "orange", "pink", "rot", "weiss", "gelb".
|
||||
* @param neueFarbe die neue Spur-Farbe der Turtle
|
||||
*/
|
||||
public void setzeFarbe(String neueFarbe)
|
||||
public void setzeFarbe(Color farbe)
|
||||
{
|
||||
if ((neueFarbe == null) || ((_farbe = FARBEN.get(neueFarbe.toLowerCase())) == null))
|
||||
if (farbe == null)
|
||||
{
|
||||
_farbe = Color.BLACK;
|
||||
}
|
||||
else
|
||||
{
|
||||
_farbe = farbe;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import java.awt.Color;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Random;
|
||||
|
||||
|
|
@ -9,7 +10,7 @@ class World implements Drawable
|
|||
private static final int GAP_SIZE = 100;
|
||||
|
||||
private final ArrayList<Rectangle> _obstacles;
|
||||
private String _obstacleColor;
|
||||
private Color _obstacleColor;
|
||||
private double _speed;
|
||||
private final Random _random;
|
||||
|
||||
|
|
@ -18,18 +19,18 @@ class World implements Drawable
|
|||
game.addDrawable(this);
|
||||
_random = new Random();
|
||||
_obstacles = new ArrayList<Rectangle>();
|
||||
_obstacleColor = Color.BLACK;
|
||||
reset();
|
||||
}
|
||||
|
||||
public void reset()
|
||||
{
|
||||
_obstacles.clear();
|
||||
_obstacleColor = "schwarz";
|
||||
createObstacle(TurtleWelt.WIDTH);
|
||||
_speed = START_SPEED;
|
||||
}
|
||||
|
||||
public void setObstacleColor(String c)
|
||||
public void setObstacleColor(Color c)
|
||||
{
|
||||
for (Drawable d : _obstacles)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user