Move states in their own classes
This commit is contained in:
parent
c99194fb6c
commit
b0ceb684cb
|
|
@ -5,10 +5,10 @@ public class Bird
|
||||||
private final Rectangle _rect;
|
private final Rectangle _rect;
|
||||||
private double _speed;
|
private double _speed;
|
||||||
|
|
||||||
public Bird(Manager man)
|
public Bird(GameScreen game)
|
||||||
{
|
{
|
||||||
_rect = new Rectangle(20, 20, 20, 20, "cyan", true);
|
_rect = new Rectangle(20, 20, 20, 20, "cyan", true);
|
||||||
man.addDrawable(_rect);
|
game.addDrawable(_rect);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset()
|
public void reset()
|
||||||
|
|
|
||||||
49
GameScreen.java
Normal file
49
GameScreen.java
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
class GameScreen
|
||||||
|
{
|
||||||
|
private final List<Drawable> _drawables;
|
||||||
|
private final Bird _bird;
|
||||||
|
private final Welt _welt;
|
||||||
|
|
||||||
|
public GameScreen()
|
||||||
|
{
|
||||||
|
_drawables = new ArrayList<Drawable>();
|
||||||
|
_bird = new Bird(this);
|
||||||
|
_welt = new Welt(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(double elapsed)
|
||||||
|
{
|
||||||
|
_bird.update(elapsed);
|
||||||
|
_welt.update(elapsed);
|
||||||
|
|
||||||
|
if (_welt.checkCollision(_bird.getRect()) ||
|
||||||
|
_bird.getRect().getY() + _bird.getRect().getHeight() > TurtleWelt.HEIGHT)
|
||||||
|
{
|
||||||
|
_bird.reset();
|
||||||
|
_welt.reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void draw(Turtle turtle, double elapsed)
|
||||||
|
{
|
||||||
|
TurtleWelt.globaleWelt.bildschirmEinfaerben(255, 255, 255);
|
||||||
|
for (var d : _drawables)
|
||||||
|
{
|
||||||
|
d.draw(turtle, elapsed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addDrawable(Drawable d)
|
||||||
|
{
|
||||||
|
_drawables.add(d);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void jump()
|
||||||
|
{
|
||||||
|
_bird.jump();
|
||||||
|
}
|
||||||
|
}
|
||||||
142
IntroScreen.java
Normal file
142
IntroScreen.java
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
class IntroScreen
|
||||||
|
{
|
||||||
|
private boolean _introDrawn;
|
||||||
|
|
||||||
|
public IntroScreen() {}
|
||||||
|
|
||||||
|
private void goTo(Turtle t, int x, int y)
|
||||||
|
{
|
||||||
|
t.hinterlasseKeineSpur();
|
||||||
|
t.geheZu(x, y);
|
||||||
|
t.hinterlasseSpur();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Draws Flappy Bird on screen.
|
||||||
|
* There are surely much nicer ways of doing this but this had the least
|
||||||
|
* cognitive overhead.
|
||||||
|
*/
|
||||||
|
public void draw(Turtle t)
|
||||||
|
{
|
||||||
|
if (!_introDrawn)
|
||||||
|
{
|
||||||
|
TurtleWelt.globaleWelt.bildschirmEinfaerben(0, 0, 0);
|
||||||
|
_introDrawn = true;
|
||||||
|
|
||||||
|
t.setzeFarbe("gruen");
|
||||||
|
// F
|
||||||
|
goTo(t, 100, 100);
|
||||||
|
t.setzeGeschwindigkeit(1);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(40);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(40);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
// L
|
||||||
|
goTo(t, 110, 100);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
// A
|
||||||
|
goTo(t, 170, 200);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.geheVor(-50);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
// PP
|
||||||
|
goTo(t, 230, 200);
|
||||||
|
for (int i = 0; i < 2; ++i)
|
||||||
|
{
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(50);
|
||||||
|
goTo(t, 290, 200);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Y
|
||||||
|
goTo(t, 350, 100);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(25);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.geheVor(-50);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(25);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
t.setzeFarbe("blau");
|
||||||
|
// B
|
||||||
|
goTo(t, 50, 210);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.geheVor(-40);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(40);
|
||||||
|
|
||||||
|
// I
|
||||||
|
goTo(t, 110, 210);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(100);
|
||||||
|
|
||||||
|
// R
|
||||||
|
goTo(t, 170, 310);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(40);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(40);
|
||||||
|
t.geheVor(-50);
|
||||||
|
t.setzeRichtung(90);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
// D
|
||||||
|
goTo(t, 235, 210);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(0);
|
||||||
|
t.geheVor(-5);
|
||||||
|
t.geheVor(50);
|
||||||
|
t.setzeRichtung(-90);
|
||||||
|
t.geheVor(100);
|
||||||
|
t.setzeRichtung(180);
|
||||||
|
t.geheVor(50);
|
||||||
|
|
||||||
|
t.setzeGeschwindigkeit(10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
177
Manager.java
177
Manager.java
|
|
@ -17,15 +17,11 @@ class Manager implements KeyListener
|
||||||
"Simple version of Flappy Bird\n" +
|
"Simple version of Flappy Bird\n" +
|
||||||
"Press SPACE to jump!\n" +
|
"Press SPACE to jump!\n" +
|
||||||
"------------------------------\n";
|
"------------------------------\n";
|
||||||
private final List<Drawable> _drawables;
|
private final GameScreen _game;
|
||||||
private Bird _bird;
|
|
||||||
private Welt _welt;
|
|
||||||
private States _state;
|
private States _state;
|
||||||
private boolean _introDrawn;
|
|
||||||
|
|
||||||
public Manager()
|
public Manager()
|
||||||
{
|
{
|
||||||
_drawables = new ArrayList<Drawable>();
|
|
||||||
Turtle turtle = new Turtle();
|
Turtle turtle = new Turtle();
|
||||||
turtle.setzeGeschwindigkeit(10);
|
turtle.setzeGeschwindigkeit(10);
|
||||||
|
|
||||||
|
|
@ -50,7 +46,8 @@ class Manager implements KeyListener
|
||||||
|
|
||||||
System.out.print(USAGE);
|
System.out.print(USAGE);
|
||||||
|
|
||||||
//startGame();
|
IntroScreen intro = new IntroScreen();
|
||||||
|
_game = new GameScreen();
|
||||||
_state = States.INTRO;
|
_state = States.INTRO;
|
||||||
|
|
||||||
long lastNanos, elapsed;
|
long lastNanos, elapsed;
|
||||||
|
|
@ -65,13 +62,13 @@ class Manager implements KeyListener
|
||||||
switch (_state)
|
switch (_state)
|
||||||
{
|
{
|
||||||
case GAME:
|
case GAME:
|
||||||
updateGame(elapsed * 1e-6d);
|
_game.update(elapsed * 1e-6d);
|
||||||
drawGame(turtle, elapsed * 1e-6d);
|
_game.draw(turtle, elapsed * 1e-6d);
|
||||||
break;
|
break;
|
||||||
case INTRO:
|
case INTRO:
|
||||||
drawIntro(turtle);
|
intro.draw(turtle);
|
||||||
// Start game after 4 seconds
|
// Start game after 3 seconds
|
||||||
if (total > 4e9)
|
if (total > 3e9)
|
||||||
{
|
{
|
||||||
startGame();
|
startGame();
|
||||||
}
|
}
|
||||||
|
|
@ -90,172 +87,16 @@ class Manager implements KeyListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void goTo(Turtle t, int x, int y)
|
|
||||||
{
|
|
||||||
t.hinterlasseKeineSpur();
|
|
||||||
t.geheZu(x, y);
|
|
||||||
t.hinterlasseSpur();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawIntro(Turtle t)
|
|
||||||
{
|
|
||||||
if (!_introDrawn)
|
|
||||||
{
|
|
||||||
_introDrawn = true;
|
|
||||||
|
|
||||||
// F
|
|
||||||
goTo(t, 100, 100);
|
|
||||||
t.setzeGeschwindigkeit(1);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(40);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(40);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
// L
|
|
||||||
goTo(t, 110, 100);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
// A
|
|
||||||
goTo(t, 170, 200);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.geheVor(-50);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
// PP
|
|
||||||
goTo(t, 230, 200);
|
|
||||||
for (int i = 0; i < 2; ++i)
|
|
||||||
{
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(50);
|
|
||||||
goTo(t, 290, 200);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Y
|
|
||||||
goTo(t, 350, 100);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(25);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.geheVor(-50);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(25);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
// B
|
|
||||||
goTo(t, 50, 210);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.geheVor(-40);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(40);
|
|
||||||
|
|
||||||
// I
|
|
||||||
goTo(t, 110, 210);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(100);
|
|
||||||
|
|
||||||
// R
|
|
||||||
goTo(t, 170, 310);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(40);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(40);
|
|
||||||
t.geheVor(-50);
|
|
||||||
t.setzeRichtung(90);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
// D
|
|
||||||
goTo(t, 235, 210);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(0);
|
|
||||||
t.geheVor(-5);
|
|
||||||
t.geheVor(50);
|
|
||||||
t.setzeRichtung(-90);
|
|
||||||
t.geheVor(100);
|
|
||||||
t.setzeRichtung(180);
|
|
||||||
t.geheVor(50);
|
|
||||||
|
|
||||||
t.setzeGeschwindigkeit(10);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startGame()
|
private void startGame()
|
||||||
{
|
{
|
||||||
_state = States.GAME;
|
_state = States.GAME;
|
||||||
_bird = new Bird(this);
|
|
||||||
_welt = new Welt(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateGame(double elapsed)
|
|
||||||
{
|
|
||||||
_bird.update(elapsed);
|
|
||||||
_welt.update(elapsed);
|
|
||||||
|
|
||||||
if (_welt.checkCollision(_bird.getRect()) ||
|
|
||||||
_bird.getRect().getY() + _bird.getRect().getHeight() > TurtleWelt.HEIGHT)
|
|
||||||
{
|
|
||||||
_bird.reset();
|
|
||||||
_welt.reset();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawGame(Turtle turtle, double elapsed)
|
|
||||||
{
|
|
||||||
TurtleWelt.globaleWelt.bildschirmEinfaerben(255, 255, 255);
|
|
||||||
for (var d : _drawables)
|
|
||||||
{
|
|
||||||
d.draw(turtle, elapsed);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addDrawable(Drawable d)
|
|
||||||
{
|
|
||||||
_drawables.add(d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void keyPressed(KeyEvent e)
|
public void keyPressed(KeyEvent e)
|
||||||
{
|
{
|
||||||
if (e.getKeyCode() == KeyEvent.VK_SPACE)
|
if (e.getKeyCode() == KeyEvent.VK_SPACE)
|
||||||
{
|
{
|
||||||
_bird.jump();
|
_game.jump();
|
||||||
}
|
}
|
||||||
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
|
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,9 @@ class Welt implements Drawable
|
||||||
private double _acc = 2;
|
private double _acc = 2;
|
||||||
private final Random _random;
|
private final Random _random;
|
||||||
|
|
||||||
public Welt(Manager manager)
|
public Welt(GameScreen game)
|
||||||
{
|
{
|
||||||
manager.addDrawable(this);
|
game.addDrawable(this);
|
||||||
_random = new Random();
|
_random = new Random();
|
||||||
_obstacles = new ArrayList<Rectangle>();
|
_obstacles = new ArrayList<Rectangle>();
|
||||||
reset();
|
reset();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user