mirror of
https://github.com/ProgramSnail/snake_2024.git
synced 2026-01-02 19:48:15 +00:00
initial game
This commit is contained in:
parent
4570d6e593
commit
adabb50a9e
10 changed files with 200 additions and 132 deletions
|
|
@ -0,0 +1,25 @@
|
|||
#include "Canvas.hpp"
|
||||
|
||||
#include "Utils.hpp"
|
||||
|
||||
using namespace utils;
|
||||
|
||||
namespace paint {
|
||||
|
||||
void square(const canvas::Square &s) {
|
||||
for (int x = s.pos.x; x < s.pos.x + s.side; ++x) {
|
||||
std::fill(screen_at(x, s.pos.y), screen_at(x, s.pos.y + s.side),
|
||||
static_cast<uint32_t>(s.color));
|
||||
}
|
||||
}
|
||||
|
||||
void circle(const canvas::Circle &c) {
|
||||
for (int x = -c.radius; x < c.radius; ++x) {
|
||||
int size_y = std::sqrt(c.radius * c.radius - std::abs(x) * std::abs(x));
|
||||
std::fill(screen_at(c.pos.x + x, c.pos.y - size_y),
|
||||
screen_at(c.pos.x + x, c.pos.y + size_y),
|
||||
static_cast<uint32_t>(c.color));
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace paint
|
||||
57
src/Game.cpp
57
src/Game.cpp
|
|
@ -6,6 +6,11 @@
|
|||
// #include <stdio.h>
|
||||
|
||||
#include "Canvas.hpp"
|
||||
#include "Map.hpp"
|
||||
#include "Player.hpp"
|
||||
#include "Snake.hpp"
|
||||
#include "Utils.hpp"
|
||||
#include "World.hpp"
|
||||
|
||||
constexpr double MIN_CONTROL_DISTANCE = 10;
|
||||
|
||||
|
|
@ -22,12 +27,28 @@ constexpr double MIN_CONTROL_DISTANCE = 10;
|
|||
// left button, 1 - right button) schedule_quit_game() - quit game after act()
|
||||
|
||||
// initialize game data in this function
|
||||
void initialize() {}
|
||||
void initialize() { srand(time(0)); }
|
||||
|
||||
float game_time = 0.0f;
|
||||
Vecf pos = {.x = 20, .y = 20};
|
||||
Vecf direction{.x = 1.0, .y = 0.0};
|
||||
Veci prev_cursor = {.x = 0, .y = 0};
|
||||
|
||||
Player player{
|
||||
.pos = {.x = 20, .y = 20},
|
||||
.direction = {.x = 1.0, .y = 0.0},
|
||||
};
|
||||
|
||||
World world;
|
||||
|
||||
Map map{{
|
||||
.gen_interval = 1.0,
|
||||
.food_exists_gens = 10,
|
||||
.gen_food_count = 1000,
|
||||
.size = {.x = 20000, .y = 20000},
|
||||
.min_food_weight = 1,
|
||||
.max_food_weight = 10,
|
||||
.food_color = Color::RED,
|
||||
}};
|
||||
|
||||
auto snake = Worm({{.pos = Veci(player.pos), .color = Color::GREEN}, 100, 10});
|
||||
|
||||
// this function is called to update game data,
|
||||
// dt - time elapsed since the previous update (in seconds)
|
||||
|
|
@ -35,23 +56,26 @@ void act(float dt) {
|
|||
if (is_key_pressed(VK_ESCAPE))
|
||||
schedule_quit_game();
|
||||
|
||||
Veci cursor = get_cursor();
|
||||
if (cursor != prev_cursor and is_valid_pos(cursor)) {
|
||||
Vecf diff = Vecf(cursor) - pos;
|
||||
world.cursor = utils::get_cursor();
|
||||
if (world.cursor != world.prev_cursor and utils::is_valid_pos(world.cursor)) {
|
||||
Vecf diff(world.cursor - utils::get_center()); // - pos;
|
||||
|
||||
if (diff.len() > MIN_CONTROL_DISTANCE) {
|
||||
direction = diff.norm();
|
||||
player.direction = diff.norm();
|
||||
}
|
||||
}
|
||||
|
||||
int speed = 200;
|
||||
pos += direction * dt * speed;
|
||||
player.pos += player.direction * dt * speed;
|
||||
|
||||
game_time += dt;
|
||||
prev_cursor = cursor;
|
||||
// std::cout << "pos: " << pos.x << ' ' << pos.y << std::endl;
|
||||
// std::cout << "curs: " << get_cursor().x << ' ' << get_cursor().y <<
|
||||
// std::endl;
|
||||
world.prev_cursor = world.cursor;
|
||||
|
||||
snake.add(Veci(player.pos));
|
||||
|
||||
snake.inc_length(map.eat(Veci(player.pos), 30));
|
||||
|
||||
map.act(dt);
|
||||
}
|
||||
|
||||
// fill buffer in this function
|
||||
|
|
@ -61,8 +85,11 @@ void draw() {
|
|||
// clear backbuffer
|
||||
memset(buffer, 0, SCREEN_HEIGHT * SCREEN_WIDTH * sizeof(uint32_t));
|
||||
|
||||
Circle sq({.pos = Veci(pos), .size = 20});
|
||||
sq.show();
|
||||
Veci map_offset = Veci(player.pos) - utils::get_center();
|
||||
|
||||
snake.draw(map_offset);
|
||||
map.draw(map_offset);
|
||||
// paint::circle({{.pos = Veci(pos), .color = Color::ORANGE}, 20});
|
||||
}
|
||||
|
||||
// free game data in this function
|
||||
|
|
|
|||
14
src/Snake.cpp
Normal file
14
src/Snake.cpp
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
#include "Snake.hpp"
|
||||
|
||||
void Worm::add(Veci pos) {
|
||||
track_.push_back(pos);
|
||||
if (track_.size() > length) {
|
||||
track_.pop_front();
|
||||
}
|
||||
}
|
||||
|
||||
void Worm::draw(Veci offset) const {
|
||||
for (const auto &pos : track_) {
|
||||
paint::circle({{.pos = pos - offset, .color = color}, radius});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue