diff options
| author | fschildt <florian.schildt@protonmail.com> | 2025-12-13 04:12:10 +0100 |
|---|---|---|
| committer | fschildt <florian.schildt@protonmail.com> | 2025-12-13 04:12:10 +0100 |
| commit | ebe4a92027aabc9c01caf0fbe791d22773abfe75 (patch) | |
| tree | e697282a885f54de7af22f36a58c1a684f67faf0 /src/games/snake/Snake.cpp | |
| parent | f24d302be51f8cd2f4c5796283ea9f9a3be8d922 (diff) | |
Diffstat (limited to 'src/games/snake/Snake.cpp')
| -rw-r--r-- | src/games/snake/Snake.cpp | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/src/games/snake/Snake.cpp b/src/games/snake/Snake.cpp index b7c9004..de9dad7 100644 --- a/src/games/snake/Snake.cpp +++ b/src/games/snake/Snake.cpp @@ -2,18 +2,30 @@ #include "games/Game.hpp" #include "renderer/Renderer.hpp" #include "common/defs.hpp" +#include "common/MemoryManager.hpp" +#include "common/math.hpp" #include <cstdint> #include <imgui.h> +#include <fstream> std::mt19937 Snake::s_rng{std::random_device{}()}; Snake::Snake() + : m_font {s_dejavu_sans_mono_filepath, 22} { static_assert(max_map_width <= sizeof(m_body_bitmap[0])*8); static_assert(max_map_height <= sizeof(m_body_bitmap[0])*8); + + int32_t highscore = 0; + std::ifstream highscore_file_in {"snake_highscore.txt"}; + if (highscore_file_in) { + highscore_file_in >> highscore; + highscore_file_in.close(); + } + m_highscore = highscore; } void @@ -42,6 +54,8 @@ Snake::Start() m_dist = std::uniform_int_distribution<int32_t>(0, m_map_width*m_map_height - 3); SpawnFood(); + m_score = 0; + m_game_status = game_resume; } @@ -76,7 +90,7 @@ Snake::MaybeMoveSnake(float dt) if ((head_pos.x < 0 || head_pos.x >= m_map_width) || (head_pos.y < 0 || head_pos.y >= m_map_height)) { - m_game_status = game_over; + HandleGameOver(); return; } uint64_t head_bit = 1 << head_pos.x; @@ -85,7 +99,7 @@ Snake::MaybeMoveSnake(float dt) body_bits &= (uint32_t)~(1 << tail_pos.x); } if (head_bit & body_bits) { - m_game_status = game_over; + HandleGameOver(); return; } @@ -102,6 +116,7 @@ Snake::MaybeMoveSnake(float dt) if (m_body_positions[m_head] == m_food_position) { + m_score += 1; SpawnFood(); } else { @@ -125,6 +140,24 @@ Snake::MaybeMoveSnake(float dt) } void +Snake::HandleGameOver() +{ + m_game_status = game_over; + if (m_score > m_highscore) { + m_highscore = m_score; + + std::ofstream highscore_file_out {highscore_path}; + if (highscore_file_out) { + highscore_file_out << m_highscore << std::endl; + highscore_file_out.close(); + } + else { + SDL_LogInfo(0, "Snake: cannot open %s for writing", highscore_path); + } + } +} + +void Snake::ProcessEvent(SDL_Event& event) { switch (event.type) { @@ -306,5 +339,26 @@ Snake::Draw() food_y + bodypart_size }; g_renderer.PushRectangle(rect, color_food, z_food); + + + /* draw scores */ + String32Id score_label = MemoryManager::EmplaceString32_Frame(U"Score"); + String32Id score_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_score)); + + String32Id highscore_label = MemoryManager::EmplaceString32_Frame(U"Highscore"); + String32Id highscore_value = MemoryManager::EmplaceString32_Frame(int32_to_u32string(m_highscore)); + + + V2F32 score_pos = {1.3f, 2.5f}; + V2F32 highscore_pos = {2.3f, 2.5f}; + Color anyscore_color {0.9f, 0.9f, 0.9f, 1.0f}; + + g_renderer.PushString32(highscore_label, m_font, highscore_pos, anyscore_color, z_text); + highscore_pos.y -= 0.1f; + g_renderer.PushString32(highscore_value, m_font, highscore_pos, anyscore_color, z_text); + + g_renderer.PushString32(score_label, m_font, score_pos, anyscore_color, z_text); + score_pos.y -= 0.1f; + g_renderer.PushString32(score_value, m_font, score_pos, anyscore_color, z_text); } |
