Make the game go faster after some time

This commit is contained in:
brxxh 2025-05-30 20:10:19 +02:00
parent 2d7277efec
commit b412c992ee

View file

@ -22,11 +22,15 @@
#include <iostream>
static const unsigned int FRAME_RATE = 60;
static const unsigned int WINDOW_WIDTH = 800;
static const unsigned int WINDOW_HEIGHT = 480;
static const unsigned int WINDOW_SCALE = 2;
static const char ACTION_KEY = ' ';
static constexpr float SCROLLING_SPEED_INC_FACTOR = 1.0002f;
// x,y+h --- x+w,y+h
// | |
// | |
@ -45,7 +49,9 @@ struct AABB {
};
struct Background {
static constexpr float SCROLLING_SPEED = 0.003f;
static constexpr float INIT_SCROLLING_SPEED = 0.003f;
float scrolling_speed = INIT_SCROLLING_SPEED;
std::array<paradiso::Sprite, 2> sprites;
paradiso::Renderer renderer{};
@ -54,6 +60,9 @@ struct Background {
paradiso::BitmapIO::get().load("PNG/background.png");
void init() {
// For resetting.
scrolling_speed = INIT_SCROLLING_SPEED;
sprites = {paradiso::Sprite{
.bitmap = image,
.pivot = {paradiso::Vector2<float>::make(0.0f, 0.0f)},
@ -66,7 +75,7 @@ struct Background {
void update() {
for (auto& sprite : sprites) {
sprite.pivot.x() -= SCROLLING_SPEED;
sprite.pivot.x() -= scrolling_speed;
if (sprite.pivot.x() <= -2.0f) {
sprite.pivot.x() += 4.0f;
}
@ -84,7 +93,9 @@ struct Background {
};
struct Ground {
static constexpr float SCROLLING_SPEED = 0.009f;
static constexpr float INIT_SCROLLING_SPEED = 0.009f;
float scrolling_speed = INIT_SCROLLING_SPEED;
std::array<paradiso::Sprite, 2> sprites;
paradiso::Renderer renderer{};
@ -97,6 +108,9 @@ struct Ground {
AABB aabb{};
void init() {
// For resetting.
scrolling_speed = INIT_SCROLLING_SPEED;
// The image's height is 71 px.
float scale_y = 71.0f / WINDOW_HEIGHT;
@ -120,7 +134,7 @@ struct Ground {
void update() {
for (auto& sprite : sprites) {
sprite.pivot.x() -= SCROLLING_SPEED;
sprite.pivot.x() -= scrolling_speed;
if (sprite.pivot.x() <= -2.0f) {
sprite.pivot.x() += 4.0f;
}
@ -138,9 +152,11 @@ struct Ground {
};
struct Rocks {
static constexpr float SCROLLING_SPEED = 0.006f;
static constexpr float INIT_SCROLLING_SPEED = 0.006f;
static constexpr float GAP_SIZE = 1.6f;
float scrolling_speed = INIT_SCROLLING_SPEED;
paradiso::Sprite top_sprite;
paradiso::Sprite bottom_sprite;
@ -157,7 +173,11 @@ struct Rocks {
AABB top_aabb;
AABB bottom_aabb;
void init(float x) {
void init(float x, bool reset_scrolling_speed = false) {
if (reset_scrolling_speed) {
scrolling_speed = INIT_SCROLLING_SPEED;
}
// The image's size is 108x239 px.
float scale_x = 108.0f / WINDOW_WIDTH;
float scale_y = 239.0f / WINDOW_HEIGHT;
@ -198,8 +218,8 @@ struct Rocks {
}
void update() {
top_sprite.pivot.x() -= SCROLLING_SPEED;
bottom_sprite.pivot.x() -= SCROLLING_SPEED;
top_sprite.pivot.x() -= scrolling_speed;
bottom_sprite.pivot.x() -= scrolling_speed;
// +----------+
// | |
@ -290,7 +310,7 @@ struct Plane {
.scale = scale},
};
// For reseting.
// For resetting.
position_y = INIT_POSITION_Y;
velocity_y = INIT_VELOCITY_Y;
rotation = INIT_ROTATION;
@ -528,8 +548,8 @@ auto main() -> int {
background.init();
ground.init();
rocks1.init(1.2f);
rocks2.init(2.4f);
rocks1.init(1.2f, true); // We don't need to set this to true,
rocks2.init(2.4f, true); // but why not?
plane.init();
start_text.init();
game_over_text.init();
@ -571,6 +591,11 @@ auto main() -> int {
plane.update(window.keyboard_input());
background.scrolling_speed *= SCROLLING_SPEED_INC_FACTOR;
ground.scrolling_speed *= SCROLLING_SPEED_INC_FACTOR;
rocks1.scrolling_speed *= SCROLLING_SPEED_INC_FACTOR;
rocks2.scrolling_speed *= SCROLLING_SPEED_INC_FACTOR;
if (plane.aabb.is_colliding_with(rocks1.top_aabb) ||
plane.aabb.is_colliding_with(rocks1.bottom_aabb) ||
plane.aabb.is_colliding_with(rocks2.top_aabb) ||
@ -586,8 +611,8 @@ auto main() -> int {
// Reset everything.
background.init();
ground.init();
rocks1.init(1.2f);
rocks2.init(2.4f);
rocks1.init(1.2f, true);
rocks2.init(2.4f, true);
plane.init();
game_state = GameState::Start;
}