examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 4a5ddc7c624231f99e97c6810a08c3ffb3d307fa
parent 9f2c69b4af7d95cc5592864416d1bb4c143a2054
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Wed,  6 Apr 2022 18:10:02 +0100

gl-asteroids: Improve scoring and stop wrapping rocks

Diffstat:
Msrc/gl-asteroids.cpp | 22+++++++++-------------
1 file changed, 9 insertions(+), 13 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -285,7 +285,7 @@ visual const& random_rock_visual(void) int main(int argc, char* argv[]) { - unsigned score = 0; + signed score = 0; unsigned ammo = 10; std::random_device rd; @@ -316,23 +316,20 @@ int main(int argc, char* argv[]) if (rock_time < t) { - rock_time += (rocks.size() < 3 ? 2 : 6) / log(t+1); + rock_time += (rocks.size() < 3 ? 2 : 6) / log(2*t+1); int dir = dis_d(gen); auto scale = dis_s(gen); - coord p = {dis_p(gen), -1 - scale}; + coord p = {dis_p(gen) / 2 - .5f, -1 - scale}; coord v(dis_v(gen), dis_v(gen)); v *= log(t+1); if (dir & 1) - v.x = -v.x; + v.x = -v.x, p.x = -p.x; if (dir & 2) - { - p.y = -p.y; - v.y = -v.y; - } + p.y = -p.y, v.y = -v.y; if (dir & 4) { std::swap(p.x, p.y); @@ -382,10 +379,7 @@ int main(int argc, char* argv[]) // resolve motion for (auto& r : rocks) - { r.p.move(dt); - r.p.wrap_pos(); - } for (auto& b : bullets) b.p.move(dt); @@ -427,7 +421,7 @@ int main(int argc, char* argv[]) rocks.erase(r); b = bullets.erase(b); - ++score; + score += 3; goto bang; } ++b; @@ -440,13 +434,15 @@ int main(int argc, char* argv[]) { using namespace std::chrono_literals; auto timeout = std::chrono::steady_clock::now() + 1s; + score += t; std::cout << "Score: " << score << '\n'; while (std::chrono::steady_clock::now() < timeout) draw_scene(t); break; } - std::erase_if(bullets, [](auto const& e) { return e.p.left_arena(); }); + score -= std::erase_if(bullets, [](auto const& e) { return e.p.left_arena(); }); + score -= std::erase_if(rocks, [](auto const& e) { return e.p.left_arena(); }); draw_scene(t); }