mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-11 00:48:42 +00:00
text rendering + food couter, minor fixes
This commit is contained in:
parent
421574ab40
commit
0d30ca5575
8 changed files with 216 additions and 17 deletions
|
|
@ -89,12 +89,12 @@ private:
|
||||||
.gen = current_gen_,
|
.gen = current_gen_,
|
||||||
.pos =
|
.pos =
|
||||||
{
|
{
|
||||||
.x = std::rand() % config_.size.x,
|
.x = utils::rand_to(config_.size.x),
|
||||||
.y = std::rand() % config_.size.y,
|
.y = utils::rand_to(config_.size.y),
|
||||||
},
|
},
|
||||||
.weight = config_.min_food_weight +
|
.weight = config_.min_food_weight +
|
||||||
std::rand() % std::abs(config_.max_food_weight -
|
utils::rand_to(std::abs(config_.max_food_weight -
|
||||||
config_.min_food_weight),
|
config_.min_food_weight)),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ class SnakeObject {
|
||||||
public:
|
public:
|
||||||
struct Config {
|
struct Config {
|
||||||
Object obj;
|
Object obj;
|
||||||
size_t initial_length;
|
uint initial_length;
|
||||||
int radius;
|
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) {
|
void inc_length(int inc) {
|
||||||
if (-inc > static_cast<int>(length_)) {
|
if (-inc > int(length_)) {
|
||||||
length_ = 1;
|
length_ = 1;
|
||||||
} else {
|
} else {
|
||||||
length_ += inc;
|
length_ += inc;
|
||||||
|
|
@ -85,14 +87,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
virtual void change_direction(float dt) = 0;
|
virtual void change_direction(float dt) = 0;
|
||||||
|
|
||||||
virtual void move(float dt) {
|
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_));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
const Config snake_config_;
|
const Config snake_config_;
|
||||||
|
|
|
||||||
18
include/Text.hpp
Normal file
18
include/Text.hpp
Normal 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
|
||||||
|
|
@ -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 utils
|
||||||
|
|
||||||
namespace color {
|
namespace color {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
|
|
||||||
using namespace utils;
|
using utils::screen_at;
|
||||||
|
|
||||||
namespace paint {
|
namespace paint {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@
|
||||||
#include "Params.hpp"
|
#include "Params.hpp"
|
||||||
#include "Player.hpp"
|
#include "Player.hpp"
|
||||||
#include "Snake.hpp"
|
#include "Snake.hpp"
|
||||||
|
#include "Text.hpp"
|
||||||
#include "Utils.hpp"
|
#include "Utils.hpp"
|
||||||
#include "World.hpp"
|
#include "World.hpp"
|
||||||
|
|
||||||
|
|
@ -90,6 +91,12 @@ void draw() {
|
||||||
|
|
||||||
player.draw(map_offset);
|
player.draw(map_offset);
|
||||||
map.draw(map_offset);
|
map.draw(map_offset);
|
||||||
|
|
||||||
|
paint::text({
|
||||||
|
{.pos = {.x = 10, .y = 10}, .color = {color::BLUE}},
|
||||||
|
5,
|
||||||
|
player.get_length() - player.get_initial_length(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// free game data in this function
|
// free game data in this function
|
||||||
|
|
|
||||||
|
|
@ -27,3 +27,12 @@ bool SnakeObject::touches(const SnakeObject &other) {
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace canvas
|
} // namespace canvas
|
||||||
|
|
||||||
|
void Snake::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_));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
161
src/Text.cpp
Normal file
161
src/Text.cpp
Normal file
|
|
@ -0,0 +1,161 @@
|
||||||
|
#include "Text.hpp"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
using utils::screen_at;
|
||||||
|
|
||||||
|
constexpr uint LETTER_WIDTH = 4;
|
||||||
|
constexpr uint LETTER_HEIGHT = 6;
|
||||||
|
|
||||||
|
using Letter = bool[LETTER_WIDTH][LETTER_HEIGHT];
|
||||||
|
|
||||||
|
namespace utils {
|
||||||
|
|
||||||
|
constexpr Letter letter_0 = {
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
{1, 0, 0, 0, 1},
|
||||||
|
{1, 0, 0, 0, 1},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_1 = {
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_2 = {
|
||||||
|
{1, 0, 1, 1, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 1, 1, 0, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_3 = {
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_4 = {
|
||||||
|
{1, 1, 1, 0, 0},
|
||||||
|
{0, 0, 1, 0, 0},
|
||||||
|
{0, 0, 1, 0, 0},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_5 = {
|
||||||
|
{1, 1, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_6 = {
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_7 = {
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{1, 0, 0, 0, 0},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_8 = {
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_9 = {
|
||||||
|
{1, 1, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 0, 1, 0, 1},
|
||||||
|
{1, 1, 1, 1, 1},
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Letter letter_undef = {
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
{0, 0, 0, 0, 0},
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace utils
|
||||||
|
|
||||||
|
namespace paint {
|
||||||
|
|
||||||
|
void text(const canvas::Text &t) {
|
||||||
|
std::string value_str = std::to_string(t.value);
|
||||||
|
size_t offset = 0;
|
||||||
|
for (const auto &ch : value_str) {
|
||||||
|
const Letter *letter = nullptr;
|
||||||
|
switch (ch) {
|
||||||
|
case '0':
|
||||||
|
letter = &utils::letter_0;
|
||||||
|
break;
|
||||||
|
case '1':
|
||||||
|
letter = &utils::letter_1;
|
||||||
|
break;
|
||||||
|
case '2':
|
||||||
|
letter = &utils::letter_2;
|
||||||
|
break;
|
||||||
|
case '3':
|
||||||
|
letter = &utils::letter_3;
|
||||||
|
break;
|
||||||
|
case '4':
|
||||||
|
letter = &utils::letter_4;
|
||||||
|
break;
|
||||||
|
case '5':
|
||||||
|
letter = &utils::letter_5;
|
||||||
|
break;
|
||||||
|
case '6':
|
||||||
|
letter = &utils::letter_6;
|
||||||
|
break;
|
||||||
|
case '7':
|
||||||
|
letter = &utils::letter_7;
|
||||||
|
break;
|
||||||
|
case '8':
|
||||||
|
letter = &utils::letter_8;
|
||||||
|
break;
|
||||||
|
case '9':
|
||||||
|
letter = &utils::letter_9;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
letter = &utils::letter_undef;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (uint i = 0; i < LETTER_WIDTH; ++i) {
|
||||||
|
for (uint j = 0; j < LETTER_HEIGHT; ++j) {
|
||||||
|
if ((*letter)[i][j]) {
|
||||||
|
for (uint k = 0; k < t.scale; ++k) {
|
||||||
|
int pixel_offset_y =
|
||||||
|
i * t.scale + offset * (LETTER_WIDTH + 1) * t.scale;
|
||||||
|
int pixel_offset_x = j * t.scale + k;
|
||||||
|
std::fill(screen_at({
|
||||||
|
.x = t.pos.x + pixel_offset_x,
|
||||||
|
.y = t.pos.y + pixel_offset_y,
|
||||||
|
}),
|
||||||
|
screen_at({
|
||||||
|
.x = t.pos.x + pixel_offset_x,
|
||||||
|
.y = t.pos.y + pixel_offset_y + int(t.scale),
|
||||||
|
}),
|
||||||
|
t.color.v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
++offset;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace paint
|
||||||
Loading…
Add table
Add a link
Reference in a new issue