From 027a18ac066032e27eeef16eb73a95a6b92916bf Mon Sep 17 00:00:00 2001 From: David96 Date: Tue, 5 Jan 2021 12:26:57 +0100 Subject: [PATCH] Add multiple Levels --- Bird.java | 9 +++++++-- GameScreen.java | 49 +++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/Bird.java b/Bird.java index 6ecc3b9..83d72e6 100644 --- a/Bird.java +++ b/Bird.java @@ -5,9 +5,9 @@ public class Bird private final Rectangle _rect; private double _speed; - public Bird(GameScreen game) + public Bird(GameScreen game, String color) { - _rect = new Rectangle(20, 20, 20, 20, "cyan", true); + _rect = new Rectangle(20, 20, 20, 20, color, true); game.addDrawable(_rect); } @@ -28,6 +28,11 @@ public class Bird _speed = -JUMP_SPEED; } + public void setColor(String color) + { + _rect.setColor(color); + } + public Rectangle getRect() { return _rect; diff --git a/GameScreen.java b/GameScreen.java index 570f40a..2c0840a 100644 --- a/GameScreen.java +++ b/GameScreen.java @@ -4,6 +4,32 @@ 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; @@ -17,10 +43,11 @@ class GameScreen implements LinearColorAnimator.AnimationListener public GameScreen() { _drawables = new ArrayList(); - _backgroundAnimator = new LinearColorAnimator(Color.BLACK, Color.WHITE, 1, this); + _backgroundAnimator = new LinearColorAnimator(Color.BLACK, LEVELS.get(0).backgroundColor, 1, this); _backgroundAnimator.start(); - _bird = new Bird(this); + _bird = new Bird(this, LEVELS.get(0).birdColor); _world = new World(this); + _world.setObstacleColor(LEVELS.get(0).obstacleColor); _totalTime = 0; _try = 1; } @@ -40,12 +67,12 @@ class GameScreen implements LinearColorAnimator.AnimationListener if (_totalTime / (LEVEL_TIME * 1e3) > _level + 1) { ++_level; - // Would be nice to add some more levels with different colors - if (_level == 1) + if (_level < LEVELS.size()) { - _world.setObstacleColor("grau"); - _backgroundAnimator.setStart(Color.WHITE); - _backgroundAnimator.setEnd(Color.BLACK); + _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(); } } @@ -60,12 +87,13 @@ class GameScreen implements LinearColorAnimator.AnimationListener _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(Color.WHITE); + _backgroundAnimator.setEnd(LEVELS.get(0).backgroundColor); _backgroundAnimator.start(); } } @@ -73,9 +101,10 @@ class GameScreen implements LinearColorAnimator.AnimationListener public void onAnimationFinished() { - if (_backgroundAnimator.getEnd().getRGB() == Color.BLACK.getRGB()) + if (_level < LEVELS.size()) { - _world.setObstacleColor("weiss"); + _world.setObstacleColor(LEVELS.get(_level).obstacleColor); + _bird.setColor(LEVELS.get(_level).birdColor); } }