Reset bird if it leaves screen & add intro screen

yes, this "state machine" should be realized by a gamescreen and
introscreen class.

Should be 2 commits, but untangling it is a bit hard
This commit is contained in:
David96 2020-12-31 13:13:34 +01:00
parent e33ac1f839
commit c99194fb6c
2 changed files with 193 additions and 15 deletions

View File

@ -11,6 +11,12 @@ public class Bird
man.addDrawable(_rect);
}
public void reset()
{
_rect.setPos(20, 20);
_speed = 0;
}
public void update(double elapsed)
{
_speed += GRAVITY * elapsed * 1e-3d;

View File

@ -7,13 +7,21 @@ import javax.swing.JFrame;
class Manager implements KeyListener
{
enum States
{
INTRO,
GAME
}
private final String USAGE =
"------------------------------\n" +
"Simple version of Flappy Bird\n" +
"Press SPACE to jump!\n" +
"------------------------------\n";
private final Bird _bird;
private final List<Drawable> _drawables;
private Bird _bird;
private Welt _welt;
private States _state;
private boolean _introDrawn;
public Manager()
{
@ -42,28 +50,32 @@ class Manager implements KeyListener
System.out.print(USAGE);
_bird = new Bird(this);
Welt welt = new Welt(this);
//startGame();
_state = States.INTRO;
long lastNanos, elapsed;
long total = 0;
lastNanos = System.nanoTime();
while (true)
{
elapsed = System.nanoTime() - lastNanos;
total += elapsed;
lastNanos = System.nanoTime();
_bird.update(elapsed * 1e-6d);
welt.update(elapsed * 1e-6d);
if (welt.checkCollision(_bird.getRect()))
switch (_state)
{
welt.reset();
case GAME:
updateGame(elapsed * 1e-6d);
drawGame(turtle, elapsed * 1e-6d);
break;
case INTRO:
drawIntro(turtle);
// Start game after 4 seconds
if (total > 4e9)
{
startGame();
}
TurtleWelt.globaleWelt.bildschirmEinfaerben(255, 255, 255);
for (var d : _drawables)
{
d.draw(turtle, elapsed * 1e-6d);
break;
}
// Try to achieve approximately 60 FPS (1000ms / 60 FPS = 16.666ms)
@ -78,6 +90,162 @@ 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()
{
_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);
@ -85,10 +253,14 @@ class Manager implements KeyListener
public void keyPressed(KeyEvent e)
{
if (e.getKeyChar() == ' ')
if (e.getKeyCode() == KeyEvent.VK_SPACE)
{
_bird.jump();
}
else if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
{
System.exit(0);
}
}
public void keyReleased(KeyEvent e) {}