5 Commits

Author SHA1 Message Date
Priec
6b10d13695 polished like a polish plates 2025-12-14 23:04:32 +01:00
Priec
74dca27c06 button scuffed 2025-12-14 22:57:04 +01:00
Priec
786ead2ffa working restart button 2025-12-14 17:29:33 +01:00
Priec
e5091a33e7 restart button on the game over is now wokring properly well 2025-12-14 17:15:50 +01:00
Priec
3cef68142d game over 2025-12-14 16:38:50 +01:00
10 changed files with 114 additions and 39 deletions

View File

@@ -14,7 +14,5 @@ public:
// Update animation, returns true if redraw needed
bool tick(int speed);
// Getters
int get_shift() const { return shift; }
};

View File

@@ -5,7 +5,6 @@
#include "../assets/character_run_frames.h"
#include "../assets/character_crawl_frames.h"
// Obstacle representation
struct Obstacle {
int x;
int y;

View File

@@ -0,0 +1,48 @@
// src/game/game_over.cpp
#include "game_over.h"
#include "mbed.h"
#include "../render/background.h"
#include "../assets/background_frame.h"
#include "../hardware/button.h"
#include <cstdio>
void show_game_over_screen(int seconds, ButtonHandler& button) {
printf("\033[2J\033[H");
// 34 znakov vo vypise
const int box_width = 34;
const int start_col = (VIEW_WIDTH - box_width) / 2;
const int box_height = 6;
const int top_padding = (VIEW_HEIGHT - box_height) / 2;
// Empty rows
for (int i = 0; i < top_padding; i++) {
printf("\r\n");
}
for (int i = 0; i < start_col; i++) printf(" ");
printf("==================================\r\n");
for (int i = 0; i < start_col; i++) printf(" ");
printf(" G A M E O V E R \r\n");
for (int i = 0; i < start_col; i++) printf(" ");
printf("==================================\r\n");
for (int i = 0; i < start_col; i++) printf(" ");
printf(" Doba hry %d sekund. \r\n", seconds);
for (int i = 0; i < start_col; i++) printf(" ");
printf("==================================\r\n");
for (int i = 0; i < start_col; i++) printf(" ");
printf("Stlacte 2x po sebe tlacidlo pre restart\r\n");
fflush(stdout);
while (button.poll() != ButtonEvent::Pressed)
ThisThread::sleep_for(25ms);
printf("\033[2J\033[H");
fflush(stdout);
}

View File

@@ -0,0 +1,8 @@
// src/game/game_over.h
#pragma once
#include "mbed.h"
#include "../hardware/button.h"
// Displays GAME OVER message with elapsed time.
// Takes a Timer that was measuring game duration.
void show_game_over_screen(int seconds, ButtonHandler& button);

View File

@@ -5,7 +5,7 @@
#include "../render/player_positioning.h"
#include "../assets/obstacle_crawl_frames.h"
// POZOR Z OBSTACLE MANAGERA
// POZOR JE Z OBSTACLE MANAGERA
constexpr int MAX_RENDER_OBSTACLES = 6;
struct ObstacleRenderData {
@@ -26,10 +26,11 @@ struct GameState {
int current_speed;
ObstacleRenderData obstacles[MAX_RENDER_OBSTACLES];
int elapsed_seconds;
bool game_over;
bool need_redraw;
};
// Global shared state and mutex
// Global shared
extern GameState g_state;
extern Mutex g_state_mutex;

View File

@@ -13,7 +13,7 @@ struct MovingObstacle {
constexpr int MAX_OBSTACLES = 6;
static MovingObstacle obstacle_pool[MAX_OBSTACLES];
// Create / reset obstacle in one slot
// Create/reset obstacle
inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
for (int i = 0; i < MAX_OBSTACLES; i++) {
if (!obstacle_pool[i].active) {

View File

@@ -14,7 +14,6 @@ class ObstacleSystem {
public:
ObstacleSystem() : tick_counter_(0), spawn_index_(0) {}
// Called once per frame
bool update_and_draw(const CharacterPosition &player_pos,
MovementType player_movement) {
// Update timers, move existing obstacles
@@ -30,7 +29,7 @@ public:
spawn_obstacle(type, VIEW_WIDTH - w - 1);
}
// Draw & collision check
// Draw n collision check
for (int i = 0; i < MAX_OBSTACLES; i++) {
if (!obstacle_pool[i].active)
continue;
@@ -47,7 +46,6 @@ public:
draw_clipped_obstacle(obstacle_pool[i], frame, height);
// Collision check
if (check_collision(player_pos, player_movement,
obstacle_pool[i].data)) {
printf("\033[2J\033[H");

View File

@@ -21,5 +21,7 @@ int main(void) {
serial_port.set_format(8, BufferedSerial::None, 1);
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
render_loop(1);
while (true) {
render_loop(4);
}
}

View File

@@ -6,6 +6,7 @@
#include "mbed.h"
#include "player.h"
#include "background.h"
#include "../game/game_over.h"
#include "../game/game_state.h"
#include "../game/state.h"
#include "../game/animation.h"
@@ -18,6 +19,7 @@
#include "../game/obstacle_system.h"
extern DigitalOut led;
ButtonHandler button(BUTTON1, LED1);
// Constants
// constexpr int PLAYER_X = 9;
@@ -25,13 +27,10 @@ constexpr int PLAYER_X = 29;
constexpr int PLAYER_Y = 6;
constexpr int STACK_SIZE = 4096;
Thread logic_thread(osPriorityNormal, STACK_SIZE);
Thread render_thread(osPriorityNormal, STACK_SIZE);
void logic_loop(void *arg) {
int speed = *(int *)arg;
MovementState player_state;
AnimationController animation;
ButtonHandler button;
MovementController mover(PLAYER_X, VIEW_WIDTH);
SpeedController timing;
timing.set_ground_speed(speed);
@@ -46,6 +45,8 @@ void logic_loop(void *arg) {
int anim_tick_counter = 0;
int tick_counter = 0;
bool game_over = false;
Timer game_timer;
game_timer.start();
// Spawn first obstacle
if (speed >= 5)
@@ -59,7 +60,7 @@ void logic_loop(void *arg) {
timing.set_ground_speed(speed);
speed_timer.reset();
// update player's animation mode
// update players animation mode
if (speed < 4)
player_state.set_state(PlayerState::Walk);
else
@@ -88,30 +89,50 @@ void logic_loop(void *arg) {
bool anim_tick = animation.tick(speed);
if (anim_tick) {
anim_tick_counter++;
// move obstacles
int ground_speed = timing.get_ground_speed();
for (int i = 0; i < MAX_OBSTACLES; i++) {
if (obstacle_pool[i].active) {
obstacle_pool[i].data.x -= ground_speed;
if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0)
obstacle_pool[i].active = false;
}
}
player_state.toggle_walk_frame(speed, anim_tick_counter);
FrameSelection frame = player_state.get_frame_selection();
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
// check collision
int ground_speed = timing.get_ground_speed();
bool collision = false;
for (int i = 0; i < MAX_OBSTACLES; i++) {
if (obstacle_pool[i].active &&
check_collision(pos, frame.movement, obstacle_pool[i].data)) {
if (!obstacle_pool[i].active)
continue;
// Keep original position
int old_x = obstacle_pool[i].data.x;
int new_x = old_x - ground_speed;
// Remove if fully offscreen after movement
if (new_x + obstacle_pool[i].data.width < 0) {
obstacle_pool[i].active = false;
continue;
}
// Swept for collision detections at speeds > 1
int left_swept = (new_x < old_x) ? new_x : old_x;
int right_swept = ((new_x + obstacle_pool[i].data.width) >
(old_x + obstacle_pool[i].data.width))
? (new_x + obstacle_pool[i].data.width)
: (old_x + obstacle_pool[i].data.width);
// Temporarily create a synthetic obstacle covering the swept range
Obstacle swept_obs{};
swept_obs.x = left_swept;
swept_obs.y = obstacle_pool[i].data.y;
swept_obs.width = right_swept - left_swept;
swept_obs.height = obstacle_pool[i].data.height;
if (check_collision(pos, frame.movement, swept_obs)) {
collision = true;
break;
}
obstacle_pool[i].data.x = new_x;
}
g_state_mutex.lock();
g_state.player_pos = draw_pos;
@@ -138,8 +159,11 @@ void logic_loop(void *arg) {
ThisThread::sleep_for(25ms);
}
// final update
game_timer.stop();
int seconds = static_cast<int>(game_timer.elapsed_time().count() / 1000000);
g_state_mutex.lock();
g_state.elapsed_seconds = seconds;
g_state.game_over = true;
g_state_mutex.unlock();
}
@@ -154,8 +178,7 @@ void render_loop_thread() {
g_state_mutex.unlock();
if (over) {
printf("\033[2J\033[H");
printf("GAME OVER\r\n");
show_game_over_screen(local.elapsed_seconds, button);
break;
}
@@ -184,6 +207,9 @@ void render_loop(int speed) {
int *speed_ptr = new int(speed);
Thread logic_thread(osPriorityNormal, STACK_SIZE);
Thread render_thread(osPriorityNormal, STACK_SIZE);
logic_thread.start(callback(logic_loop, (void *)speed_ptr));
render_thread.start(render_loop_thread);

View File

@@ -5,16 +5,13 @@
#include <cstdio>
// Draws an obstacle using bottombased world coordinates.
// x = left edge of the collision box in world space
// y = bottom edge of the collision box in world space (0 = ground)
void draw_obstacle(int x, int y, CrawlObstacleType type) {
const char **obstacle_frame = nullptr;
int frame_total_height = 0;
// Collisionmodel parameters for this obstacle
int collision_top_offset = 0; // rows from visual top to top of collision box
int collision_height = 0; // rows tall
int collision_left_offset = 0; // columns from visual left to collision left
int collision_top_offset = 0;
int collision_height = 0;
int collision_left_offset = 0;
switch (type) {
case CrawlObstacleType::Crawl1:
@@ -37,13 +34,11 @@ void draw_obstacle(int x, int y, CrawlObstacleType type) {
return; // no rendering for invalid type
}
// Convert bottombased (world) coordinates to screen coordinates.
// Y of the collision bottom is given; translate to where to start drawing the visual frame.
// Convert bottombased coordinates to screen coordinates.
const int frame_row_index_bottom = collision_top_offset + collision_height - 1;
const int frame_top_world_y = y + frame_row_index_bottom;
const int frame_top_screen_row = VIEW_HEIGHT - frame_top_world_y;
// X of the collision box is given; shift left to the visual frame's starting column.
const int frame_screen_x = x - collision_left_offset;
for (int i = 0; i < frame_total_height; i++) {