text rendering + food couter, minor fixes

This commit is contained in:
programsnail 2024-07-26 20:25:52 +03:00
parent 421574ab40
commit 0d30ca5575
8 changed files with 216 additions and 17 deletions

View file

@ -89,12 +89,12 @@ private:
.gen = current_gen_,
.pos =
{
.x = std::rand() % config_.size.x,
.y = std::rand() % config_.size.y,
.x = utils::rand_to(config_.size.x),
.y = utils::rand_to(config_.size.y),
},
.weight = config_.min_food_weight +
std::rand() % std::abs(config_.max_food_weight -
config_.min_food_weight),
utils::rand_to(std::abs(config_.max_food_weight -
config_.min_food_weight)),
});
}
}

View file

@ -11,7 +11,7 @@ class SnakeObject {
public:
struct Config {
Object obj;
size_t initial_length;
uint initial_length;
int radius;
};
@ -34,12 +34,14 @@ public:
//
size_t get_length() { return length_; }
uint get_length() { return length_; }
void set_length(size_t length) { length_ = length; }
uint get_initial_length() { return canvas_config_.initial_length; }
void set_length(uint length) { length_ = length; }
void inc_length(int inc) {
if (-inc > static_cast<int>(length_)) {
if (-inc > int(length_)) {
length_ = 1;
} else {
length_ += inc;
@ -85,14 +87,7 @@ public:
protected:
virtual void change_direction(float dt) = 0;
virtual void move(float dt) {
real_pos_ += direction_ * dt * snake_config_.speed;
move_time_delta_ += dt;
if (move_time_delta_ > snake_config_.move_interval) {
move_time_delta_ -= snake_config_.move_interval;
add(Veci(real_pos_));
}
}
virtual void move(float dt);
protected:
const Config snake_config_;

18
include/Text.hpp Normal file
View file

@ -0,0 +1,18 @@
#pragma once
#include "Canvas.hpp"
namespace canvas {
struct Text : public Object {
uint scale;
uint value;
};
} // namespace canvas
namespace paint {
void text(const canvas::Text &t);
} // namespace paint

View file

@ -56,6 +56,15 @@ template <typename T> inline Vec<T> to_world_coord(Vec<T> pos) {
};
}
inline int rand_to(int x) {
if (std::abs(x) <= 1) {
return 0;
}
bool is_negative = x < 0;
return (is_negative ? -1 : 1) + rand() % std::abs(x);
}
} // namespace utils
namespace color {