From 7a3afe12819754d597cf30099423fabe381348cb Mon Sep 17 00:00:00 2001 From: _brxxh <11dac2t@huhn-online.de> Date: Thu, 23 Nov 2023 18:59:30 +0100 Subject: [PATCH] Addded game over to quickwings --- examples/quickwings/quickwings.cpp | 92 +++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 26 deletions(-) diff --git a/examples/quickwings/quickwings.cpp b/examples/quickwings/quickwings.cpp index ac626a0..2a91d22 100644 --- a/examples/quickwings/quickwings.cpp +++ b/examples/quickwings/quickwings.cpp @@ -5,6 +5,7 @@ * */ +#include #include #include #include @@ -24,6 +25,7 @@ // #include "lib/image_loader.hpp" const int frame_rate = 60; +bool game_over = false; struct Background { paradiso::Sprite backgroundLeft; @@ -187,47 +189,60 @@ struct QuickWings { void update() { + if (game_over == true) { + paused = true; + } + // Stop game if (paused) return; + else { + // Apply gravity + velocity += gravity; - // Apply gravity - velocity += gravity; + if (move_up) + velocity += move_up_velocity - gravity; - if (move_up) - velocity += move_up_velocity - gravity; + // Cap velocity + if (velocity > max_velocity) + velocity = max_velocity; + if (velocity < -max_velocity) + velocity = -max_velocity; - // Cap velocity - if (velocity > max_velocity) - velocity = max_velocity; - if (velocity < -max_velocity) - velocity = -max_velocity; + // Cap position + pos += velocity; + if (pos < min_pos) { + pos = min_pos; + velocity = 0.0f; + game_over = true; + } + if (pos > max_pos) { + pos = max_pos; + velocity = 0.0f; + game_over = true; + } - // Cap position - pos += velocity; - if (pos < min_pos) { - pos = min_pos; - velocity = 0.0f; + // Update rotation + rotation = velocity * 10.0f; } - if (pos > max_pos) { - pos = max_pos; - velocity = 0.0f; - } - - // Update rotation - rotation = velocity * 10.0f; } // keyboard handler void on_keyboard(const paradiso::Window::KeyboardInputStack& input) { if (input.size()) { paused = false; - bool pressed_up = input.top().key == ' ' || input.top().key == 'W'; - if (input.top().action == 1 && pressed_up) { - move_up = true; - } else if (input.top().action == 0 && pressed_up) { - move_up = false; + if (paused == false) { + bool pressed_up = input.top().key == ' ' || input.top().key == 'W'; + + if (input.top().action == 1 && pressed_up) { + move_up = true; + } else if (input.top().action == 0 && pressed_up) { + move_up = false; + } + } + else { + return; } } } @@ -269,6 +284,25 @@ struct Message { } }; +struct GameOverMessage { + paradiso::Sprite messageSprite; + paradiso::Renderer renderer{}; + + GameOverMessage() { + auto messageImage = paradiso::BitmapIO::get().load("gameover.png"); + messageSprite = paradiso::Sprite{ + .bitmap = messageImage, + .pivot = {paradiso::Vector2::make(0.0f, 0.4f)}, + .scale = {paradiso::Vector2::make(((500.0f - (500.0f - 192.0f)) / 500.0f) * 2.25f, ((700.0f - (700.0f - 42.0f)) / 700.0f) * 2.25f)} + }; + }; + + void draw(const paradiso::Shader& shader) { + shader.set_uniform("pivot", messageSprite.pivot); + shader.set_uniform("scale", messageSprite.scale); + renderer.draw(messageSprite, shader); + } +}; auto main() -> int { @@ -317,6 +351,7 @@ auto main() -> int { auto background = Background{}; auto grass = Grass{}; auto quickwingsapp = QuickWings{}; + auto gameover = GameOverMessage{}; // timer @@ -341,6 +376,11 @@ auto main() -> int { background.draw(shader); grass.draw(shader); message.draw(shader); + + if (game_over == true) { + gameover.draw(shader); + } + quickwingsapp.draw(shader); // wait for frame rate