mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2025-12-24 23:28:44 +00:00
modernization, better food rendering, consistent player moving, snake intersections
This commit is contained in:
parent
adabb50a9e
commit
b4509733da
8 changed files with 119 additions and 39 deletions
|
|
@ -3,23 +3,9 @@
|
|||
#include <cmath>
|
||||
|
||||
#include "Engine.h"
|
||||
#include "Utils.hpp"
|
||||
#include "Vec.hpp"
|
||||
|
||||
enum class Color : uint32_t {
|
||||
BLACK = 0x000000ff,
|
||||
WHITE = 0x00ff6000,
|
||||
|
||||
BLUE = 0x000000ff,
|
||||
GREEN = 0x0000ff00,
|
||||
CYAN = 0x0000ffff,
|
||||
RED = 0x00ff0000,
|
||||
MAGENTA = 0x00ff00ff,
|
||||
YELLOW = 0x00ffff00,
|
||||
GRAY = 0x001f1f1f,
|
||||
ORANGE = 0x00ff6000,
|
||||
};
|
||||
constexpr Color CL_BG = Color::BLACK;
|
||||
|
||||
using Screen = uint32_t[SCREEN_HEIGHT][SCREEN_WIDTH];
|
||||
|
||||
namespace canvas {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
#include <iostream>
|
||||
|
||||
#include "Canvas.hpp"
|
||||
#include "Utils.hpp"
|
||||
|
|
@ -50,8 +51,12 @@ public:
|
|||
for (const auto &food : food_) {
|
||||
Veci food_pos = food.pos - offset;
|
||||
if (utils::is_valid_pos(food_pos) and not food.eaten) {
|
||||
paint::circle(
|
||||
{{.pos = food_pos, .color = config_.food_color}, food.weight * 3});
|
||||
paint::circle({{
|
||||
.pos = food_pos,
|
||||
.color = color::scale(config_.food_color,
|
||||
1.0 - food_lasted(food)),
|
||||
},
|
||||
food.weight * 2});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -59,21 +64,31 @@ public:
|
|||
private:
|
||||
void generate() {
|
||||
++current_gen_;
|
||||
while (food_.front().gen + config_.food_exists_gens < current_gen_) {
|
||||
while (food_.front().gen + config_.food_exists_gens <= current_gen_) {
|
||||
food_.pop_front();
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < config_.gen_food_count; ++i) {
|
||||
food_.push_back({
|
||||
.gen = current_gen_,
|
||||
.pos = {.x = rand() % config_.size.x, .y = rand() % config_.size.y},
|
||||
.pos =
|
||||
{
|
||||
.x = std::rand() % config_.size.x,
|
||||
.y = std::rand() % config_.size.y,
|
||||
},
|
||||
.weight = config_.min_food_weight +
|
||||
rand() % std::abs(config_.max_food_weight -
|
||||
config_.min_food_weight),
|
||||
std::rand() % std::abs(config_.max_food_weight -
|
||||
config_.min_food_weight),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
double food_lasted(const Food &food) {
|
||||
return ((current_gen_ - food.gen) * config_.gen_interval +
|
||||
time_from_last_gen_) /
|
||||
(config_.food_exists_gens * config_.gen_interval);
|
||||
}
|
||||
|
||||
private:
|
||||
Config config_;
|
||||
double time_from_last_gen_ = 0;
|
||||
|
|
|
|||
|
|
@ -5,4 +5,7 @@
|
|||
struct Player {
|
||||
Vecf pos;
|
||||
Vecf direction;
|
||||
double speed;
|
||||
double move_interval;
|
||||
double move_time_delta;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -21,6 +21,12 @@ public:
|
|||
|
||||
void draw(Veci offset = {}) const;
|
||||
|
||||
//
|
||||
|
||||
Veci get_pos() { return pos; }
|
||||
|
||||
//
|
||||
|
||||
size_t get_length() { return length; }
|
||||
|
||||
void set_length(size_t length) { this->length = length; }
|
||||
|
|
@ -33,6 +39,10 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
bool touches(const Worm &other);
|
||||
|
||||
protected:
|
||||
std::deque<Veci> track_;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -38,6 +38,41 @@ inline constexpr Veci get_center() {
|
|||
|
||||
} // namespace utils
|
||||
|
||||
namespace color {
|
||||
|
||||
enum FixedColor : uint32_t {
|
||||
BLACK = 0x000000ff,
|
||||
WHITE = 0x00ff6000,
|
||||
|
||||
BLUE = 0x000000ff,
|
||||
GREEN = 0x0000ff00,
|
||||
CYAN = 0x0000ffff,
|
||||
RED = 0x00ff0000,
|
||||
MAGENTA = 0x00ff00ff,
|
||||
YELLOW = 0x00ffff00,
|
||||
GRAY = 0x001f1f1f,
|
||||
ORANGE = 0x00ff6000,
|
||||
};
|
||||
constexpr FixedColor BG = BLACK;
|
||||
|
||||
struct Color {
|
||||
uint32_t v;
|
||||
};
|
||||
|
||||
inline Color scale(Color color, double scale) {
|
||||
static constexpr uint32_t COL_BITS = 8;
|
||||
static constexpr uint32_t COMPONENTS = 4;
|
||||
for (uint32_t i = COL_BITS; i < COMPONENTS * COL_BITS; i += COL_BITS) {
|
||||
uint32_t comp = (color.v >> i) % (1 << COL_BITS);
|
||||
color.v -= (comp << i);
|
||||
color.v += (uint32_t(comp * scale) << i);
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
} // namespace color
|
||||
using color::Color;
|
||||
|
||||
//
|
||||
|
||||
class GameObject {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue