From 49ceb1d8c87a3dfc12f414ac452d2f814980448e Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 6 Jul 2021 09:56:48 +0300 Subject: [PATCH 01/11] =?UTF-8?q?=D0=A3=D0=BB=D1=83=D1=87=D1=88=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=D0=B0=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=81=D1=81=D0=B8=D0=B2=D0=B0=20=D0=B8=D0=B3=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BE=D0=B9=20=D0=BA=D0=B0=D1=80=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/figure.cpp | 7 +++++- src/Core/figure.h | 7 +++++- src/Core/player.cpp | 51 +++++++++--------------------------------- src/Core/player.h | 1 - src/Core/rectangle.cpp | 7 ++---- src/Core/rectangle.h | 5 ++--- src/Core/world.cpp | 25 ++++++++++++++++++++- src/Core/world.h | 7 ++++++ src/Game.cpp | 46 +++++++++++++++++++------------------ 9 files changed, 81 insertions(+), 75 deletions(-) diff --git a/src/Core/figure.cpp b/src/Core/figure.cpp index f2f2f79..a15973b 100644 --- a/src/Core/figure.cpp +++ b/src/Core/figure.cpp @@ -3,7 +3,7 @@ Figure::Figure() : recs{ 0 } {} -Figure::Figure(std::vector rectangles, Color color, Color outline_color) //Color color = BLACK, Color outline_color = BLACK +Figure::Figure(std::vector rectangles, Color color, Color outline_color) //color = BLACK, outline_color = BLACK : recs{ rectangles } { size = recs.size(); @@ -13,4 +13,9 @@ Figure::Figure(std::vector rectangles, Color color, Color outline_color) // recs[i].color = color; recs[i].outline_color = outline_color; } +} + +Rec& Figure::operator[](unsigned int index) +{ + return recs[index]; } \ No newline at end of file diff --git a/src/Core/figure.h b/src/Core/figure.h index 216f50b..6d62af6 100644 --- a/src/Core/figure.h +++ b/src/Core/figure.h @@ -9,10 +9,15 @@ class Figure { public: - std::vector recs; int size; +private: + std::vector recs; + +public: Figure(); Figure(std::vector rectangles, Color color = BLACK, Color outline_color = BLACK); + + Rec& operator[](unsigned int index); }; diff --git a/src/Core/player.cpp b/src/Core/player.cpp index e2bde6d..08e4195 100644 --- a/src/Core/player.cpp +++ b/src/Core/player.cpp @@ -1,7 +1,7 @@ #include "player.h" Player::Player(World* world, FigureEnum figure) - : x{ 0 }, y{ 0 }, structure{ 0 }, world{ world } + : x{ 0 }, y{ 0 }, world{ world } { ChangeFigure(figure); } @@ -11,12 +11,13 @@ void Player::LoadToWorldArr() // for (int i = 0; i < figure.size; ++i) { - Rec rec = Rec(figure.recs[i]); + Rec rec = Rec(figure[i]); + // rec.x += x; rec.y += y; + rec.is_occupied = true; - //TODO: - world->arr.push_back(rec); + world->SetElement(rec); } } @@ -44,48 +45,30 @@ bool Player::CanMove(const char* direction) for (int i = 0; i < figure.size; ++i) { // , - int world_x, world_y; - - world_x = x + figure.recs[i].x; - world_y = y + figure.recs[i].y; + int world_x = x + figure[i].x; + int world_y = y + figure[i].y; // TODO: up, down . if ((direction == "right")) { - if ((world_x + 1) >= world->bound_x) + if ((world_x + 1) >= world->bound_x || world->GetElement(world_x + 1, world_y).is_occupied) { return false; } - - for (int j = 0; j < world->arr.size(); ++j) - { - if ((world_x + 1) == world->arr[j].x && world_y == world->arr[j].y) - { - return false; - } - } } if (direction == "left") { - if ((world_x - 1) < 0) + if ((world_x - 1) < 0 || world->GetElement(world_x - 1, world_y).is_occupied) { return false; } - - for (int j = 0; j < world->arr.size(); ++j) - { - if ((world_x - 1) == world->arr[j].x && world_y == world->arr[j].y) - { - return false; - } - } } if (direction == "down") { - if ((world_y + 1) >= world->bound_y) + if ((world_y + 1) >= world->bound_y || world->GetElement(world_x, world_y + 1).is_occupied) { LoadToWorldArr(); @@ -95,20 +78,6 @@ bool Player::CanMove(const char* direction) return false; } - - for (int j = 0; j < world->arr.size(); ++j) - { - if (world_x == world->arr[j].x && (world_y + 1) == world->arr[j].y) - { - LoadToWorldArr(); - - // - x = 0; - y = 0; - - return false; - } - } } } diff --git a/src/Core/player.h b/src/Core/player.h index 95e8ca3..f1ef11a 100644 --- a/src/Core/player.h +++ b/src/Core/player.h @@ -10,7 +10,6 @@ class Player public: int x; int y; - Rectangle structure; World* world; Figure figure; diff --git a/src/Core/rectangle.cpp b/src/Core/rectangle.cpp index 6898b54..19c0cf5 100644 --- a/src/Core/rectangle.cpp +++ b/src/Core/rectangle.cpp @@ -2,8 +2,5 @@ #include "rectangle.h" -Rec::Rec() - : x{ 0 }, y{ 0 }, color{ BLACK }, outline_color{ BLACK } {} - -Rec::Rec(int x, int y, Color color, Color outline_color) // Color color = BLACK, Color outline_color = BLACK - : x{ x }, y{ y }, color{ color }, outline_color{ outline_color } {} +Rec::Rec(int x, int y, Color color, Color outline_color) // x = 0, y = 0, color = BLACK, outline_color = BLACK + : x{ x }, y{ y }, is_occupied{ false }, color{ color }, outline_color{ outline_color } {} diff --git a/src/Core/rectangle.h b/src/Core/rectangle.h index 80938e6..57999f5 100644 --- a/src/Core/rectangle.h +++ b/src/Core/rectangle.h @@ -7,10 +7,9 @@ class Rec { public: int x, y; + bool is_occupied; Color color; Color outline_color; - Rec(); - - Rec(int x, int y, Color color = BLACK, Color outline_color = BLACK); + Rec(int x = 0, int y = 0, Color color = BLACK, Color outline_color = BLACK); }; diff --git a/src/Core/world.cpp b/src/Core/world.cpp index a60004a..b80b9f1 100644 --- a/src/Core/world.cpp +++ b/src/Core/world.cpp @@ -1,4 +1,27 @@ #include "world.h" World::World(int bound_x, int bound_y) - : bound_x{ bound_x }, bound_y{ bound_y }, arr{ 0 } {} \ No newline at end of file + : bound_x{ bound_x }, bound_y{ bound_y }, arr{ } +{ + // TODO: Пофиксить месево, возможно заменить вектор на аррей + size = bound_x * bound_y; + arr.resize(size); +} + +void World::SetElement(Rec& element) +{ + // Переводим двумерный индекс массида в одномерный + arr[static_cast(element.x) + static_cast(element.y) * static_cast(bound_x)] = element; +} + +// Обращение как к двумерному массиву +Rec& World::GetElement(unsigned int x, unsigned int y) +{ + return arr[static_cast(x) + static_cast(y) * static_cast(bound_x)]; +} + +// Обращение как к одномерному массиву +Rec& World::GetElement(unsigned int element) +{ + return arr[element]; +} \ No newline at end of file diff --git a/src/Core/world.h b/src/Core/world.h index f92cf36..1e36afa 100644 --- a/src/Core/world.h +++ b/src/Core/world.h @@ -10,8 +10,15 @@ class World public: int bound_x; int bound_y; + unsigned int size; +private: std::vector arr; +public: World(int bound_x, int bound_y); + + void SetElement(Rec& element); + Rec& GetElement(unsigned int x, unsigned int y); + Rec& GetElement(unsigned int element); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 0a7c392..68825c4 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -57,34 +57,36 @@ int main() ClearBackground(RAYWHITE); + Rectangle rec_struct{}; + rec_struct.width = sector_size; + rec_struct.height = sector_size; + + // for (int i = 0; i < player.figure.size; ++i) { - int tmp_pos_x, tmp_pos_y; - - tmp_pos_x = player.x + player.figure.recs[i].x; - tmp_pos_y = player.y + player.figure.recs[i].y; - + int tmp_pos_x = player.x + player.figure[i].x; + int tmp_pos_y = player.y + player.figure[i].y; - //TODO: , ? - player.structure.x = tmp_pos_x * sector_size; - player.structure.y = tmp_pos_y * sector_size; - player.structure.width = sector_size; - player.structure.height = sector_size; + rec_struct.x = tmp_pos_x * sector_size; + rec_struct.y = tmp_pos_y * sector_size; - DrawRectangleRounded(player.structure, 0.5f, 1, player.figure.recs[i].color); - DrawRectangleRoundedLines(player.structure, 0.5f, 1, 3.0f, player.figure.recs[i].outline_color); + DrawRectangleRounded(rec_struct, 0.5f, 1, player.figure[i].color); + DrawRectangleRoundedLines(rec_struct, 0.5f, 1, 3.0f, player.figure[i].outline_color); } - - // - for (int i = 0; i < world.arr.size(); ++i) + + // + for (int i = 0; i < world.size; ++i) { - player.structure.x = world.arr[i].x * sector_size; - player.structure.y = world.arr[i].y * sector_size; - player.structure.width = sector_size; - player.structure.height = sector_size; + if(world.GetElement(i).is_occupied) + { + Rec rec = Rec(world.GetElement(i)); + + rec_struct.x = rec.x * sector_size; + rec_struct.y = rec.y * sector_size; - DrawRectangleRounded(player.structure, 0.5f, 1, world.arr[i].color); - DrawRectangleRoundedLines(player.structure, 0.5f, 1, 3.0f, world.arr[i].outline_color); + DrawRectangleRounded(rec_struct, 0.5f, 1, rec.color); + DrawRectangleRoundedLines(rec_struct, 0.5f, 1, 3.0f, rec.outline_color); + } } // Debug @@ -92,7 +94,7 @@ int main() + "\n left: " + std::to_string(player.CanMove("left")) + "\n right: " + std::to_string(player.CanMove("right")) + "\n down: " + std::to_string(player.CanMove("down")) - + "\n map size: " + std::to_string(world.arr.size()); + + "\n map size: " + std::to_string(world.size); DrawText(debug.c_str(), 10, 10, 30, RED); EndDrawing(); From 7abfcedc422250b087f6f3a5b8b14b52764fad66 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Wed, 7 Jul 2021 18:50:56 +0300 Subject: [PATCH 02/11] Figure add FigureEnum MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Добавил переменную с FigureEnum в класс Figure, для отслеживания типа фигуры --- src/Core/figure.cpp | 4 ++-- src/Core/figure.h | 4 +++- src/Core/figures.cpp | 14 +++++++------- src/Core/player.cpp | 24 +++++++++++++++++++++--- src/Core/player.h | 1 + src/Core/world.cpp | 40 +++++++++++++++++++++++++++++++--------- src/Core/world.h | 6 ++++-- src/Game.cpp | 12 ++++++++---- 8 files changed, 77 insertions(+), 28 deletions(-) diff --git a/src/Core/figure.cpp b/src/Core/figure.cpp index a15973b..b9c18c0 100644 --- a/src/Core/figure.cpp +++ b/src/Core/figure.cpp @@ -3,8 +3,8 @@ Figure::Figure() : recs{ 0 } {} -Figure::Figure(std::vector rectangles, Color color, Color outline_color) //color = BLACK, outline_color = BLACK - : recs{ rectangles } +Figure::Figure(std::vector rectangles, FigureEnum type, Color color, Color outline_color) //color = BLACK, outline_color = BLACK + : recs{ rectangles }, figure_type{ type } { size = recs.size(); diff --git a/src/Core/figure.h b/src/Core/figure.h index 6d62af6..859c652 100644 --- a/src/Core/figure.h +++ b/src/Core/figure.h @@ -4,12 +4,14 @@ #include #include "rectangle.h" +#include "figure_enum.h" // class Figure { public: int size; + FigureEnum figure_type; private: std::vector recs; @@ -17,7 +19,7 @@ class Figure public: Figure(); - Figure(std::vector rectangles, Color color = BLACK, Color outline_color = BLACK); + Figure(std::vector rectangles, FigureEnum type, Color color = BLACK, Color outline_color = BLACK); Rec& operator[](unsigned int index); }; diff --git a/src/Core/figures.cpp b/src/Core/figures.cpp index 8731c34..918bfea 100644 --- a/src/Core/figures.cpp +++ b/src/Core/figures.cpp @@ -1,9 +1,9 @@ #include "figures.h" -Figure Figures::o{ std::vector{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }, YELLOW }; -Figure Figures::i{ std::vector{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 } }, SKYBLUE }; -Figure Figures::s{ std::vector{ { 0, 1 }, { 1, 0 }, { 1, 1 }, { 2, 0 } }, RED }; -Figure Figures::z{ std::vector{ { 0, 0 }, { 1, 0 }, { 1, 1 }, { 2, 1 } }, GREEN }; -Figure Figures::l{ std::vector{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 2 } }, ORANGE }; -Figure Figures::j{ std::vector{ { 0, 2 }, { 1, 0 }, { 1, 1 }, { 1, 2 } }, PINK }; -Figure Figures::t{ std::vector{ { 0, 0 }, { 1, 0 }, { 2, 0}, { 1, 1 } }, PURPLE }; \ No newline at end of file +Figure Figures::o{ std::vector{ { 0, 0 }, { 0, 1 }, { 1, 0 }, { 1, 1 } }, FigureEnum::O, YELLOW }; +Figure Figures::i{ std::vector{ { 0,-2 }, { 0,-1 }, { 0, 0 }, { 0, 1 } }, FigureEnum::I, SKYBLUE }; +Figure Figures::s{ std::vector{ {-1, 0 }, { 0,-1 }, { 0, 0 }, { 1,-1 } }, FigureEnum::S, RED }; +Figure Figures::z{ std::vector{ {-1,-1 }, { 0,-1 }, { 0, 0 }, { 1, 0 } }, FigureEnum::Z, GREEN }; +Figure Figures::l{ std::vector{ { 0,-1 }, { 0, 0 }, { 0, 1 }, { 1, 1 } }, FigureEnum::L, ORANGE }; +Figure Figures::j{ std::vector{ {-1, 1 }, { 0,-1 }, { 0, 0 }, { 0, 1 } }, FigureEnum::J, PINK }; +Figure Figures::t{ std::vector{ {-1, 0 }, { 0,-1 }, { 0, 0 }, { 1, 0 } }, FigureEnum::T, PURPLE }; \ No newline at end of file diff --git a/src/Core/player.cpp b/src/Core/player.cpp index 08e4195..81e44a3 100644 --- a/src/Core/player.cpp +++ b/src/Core/player.cpp @@ -52,7 +52,7 @@ bool Player::CanMove(const char* direction) if ((direction == "right")) { - if ((world_x + 1) >= world->bound_x || world->GetElement(world_x + 1, world_y).is_occupied) + if ((world_x + 1) >= world->bound_x || world->IsElementOccupied(world_x + 1, world_y)) { return false; } @@ -60,7 +60,7 @@ bool Player::CanMove(const char* direction) if (direction == "left") { - if ((world_x - 1) < 0 || world->GetElement(world_x - 1, world_y).is_occupied) + if ((world_x - 1) < 0 || world->IsElementOccupied(world_x - 1, world_y)) { return false; } @@ -68,7 +68,7 @@ bool Player::CanMove(const char* direction) if (direction == "down") { - if ((world_y + 1) >= world->bound_y || world->GetElement(world_x, world_y + 1).is_occupied) + if ((world_y + 1) >= world->bound_y || world->IsElementOccupied(world_x, world_y + 1)) { LoadToWorldArr(); @@ -82,4 +82,22 @@ bool Player::CanMove(const char* direction) } return true; +} + +void Player::RotateFigure() +{ + for (int i = 0; i < figure.size; ++i) + { + if(figure.figure_type != FigureEnum::O) + { + // + // x' = x * cos(90) + y * sin(90) // sin(90) = 1, cos(90) = 0; + // y' = -x * sin(90) + y * cos(90) + int x = figure[i].x; + int y = figure[i].y; + + figure[i].x = y; + figure[i].y = -x; + } + } } \ No newline at end of file diff --git a/src/Core/player.h b/src/Core/player.h index f1ef11a..32c0b2e 100644 --- a/src/Core/player.h +++ b/src/Core/player.h @@ -21,4 +21,5 @@ class Player public: void ChangeFigure(FigureEnum figures); bool CanMove(const char* direction); + void RotateFigure(); }; \ No newline at end of file diff --git a/src/Core/world.cpp b/src/Core/world.cpp index b80b9f1..09b34a3 100644 --- a/src/Core/world.cpp +++ b/src/Core/world.cpp @@ -8,20 +8,42 @@ World::World(int bound_x, int bound_y) arr.resize(size); } +void World::ClearWorld() +{ + for (auto& elem : arr) + { + if(elem.is_occupied) + elem = Rec(); + } +} + +// Обращение как к одномерному массиву +Rec& World::GetElement(int element) +{ + if (element <= arr.size()) + return arr[element]; + else + throw("Element at this number doesn't exist"); +} + void World::SetElement(Rec& element) { - // Переводим двумерный индекс массида в одномерный - arr[static_cast(element.x) + static_cast(element.y) * static_cast(bound_x)] = element; + // Переводим двумерный индекс массива в одномерный + int element_number = element.x + element.y * bound_x; + + if(element_number <= arr.size()) + arr[element_number] = element; + else + throw("Element at this number doesn't exist"); } // Обращение как к двумерному массиву -Rec& World::GetElement(unsigned int x, unsigned int y) +bool World::IsElementOccupied(int x, int y) { - return arr[static_cast(x) + static_cast(y) * static_cast(bound_x)]; + if ((x >= 0 && x <= bound_x) && (y >= 0 && y <= bound_y)) + return arr[x + y * bound_x].is_occupied; + else + return false; + } -// Обращение как к одномерному массиву -Rec& World::GetElement(unsigned int element) -{ - return arr[element]; -} \ No newline at end of file diff --git a/src/Core/world.h b/src/Core/world.h index 1e36afa..e41ea0a 100644 --- a/src/Core/world.h +++ b/src/Core/world.h @@ -18,7 +18,9 @@ class World public: World(int bound_x, int bound_y); + + void ClearWorld(); void SetElement(Rec& element); - Rec& GetElement(unsigned int x, unsigned int y); - Rec& GetElement(unsigned int element); + Rec& GetElement(int element); + bool IsElementOccupied(int x, int y); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 68825c4..d4d8611 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -28,15 +28,20 @@ int main() { using namespace std::string_literals; // ""s - // + // if (IsKeyPressed(KEY_UP) && player.CanMove("up")) player.y -= 1; - if (IsKeyDown(KEY_DOWN) && player.CanMove("down") ) + if (IsKeyPressed(KEY_DOWN) && player.CanMove("down") ) player.y += 1; if (IsKeyPressed(KEY_RIGHT) && player.CanMove("right") ) player.x += 1; if (IsKeyPressed(KEY_LEFT) && player.CanMove("left")) player.x -= 1; + if (IsKeyPressed(KEY_SPACE)) + player.RotateFigure(); + + if (IsKeyPressed(KEY_BACKSPACE)) + world.ClearWorld(); if (IsKeyPressed(KEY_O)) player.ChangeFigure(FigureEnum::O); @@ -93,8 +98,7 @@ int main() std::string debug = "pos "s + std::to_string(player.x) + ", " + std::to_string(player.y) + "\n left: " + std::to_string(player.CanMove("left")) + "\n right: " + std::to_string(player.CanMove("right")) - + "\n down: " + std::to_string(player.CanMove("down")) - + "\n map size: " + std::to_string(world.size); + + "\n down: " + std::to_string(player.CanMove("down")); DrawText(debug.c_str(), 10, 10, 30, RED); EndDrawing(); From f89f9941a6f7c9a040d114e1f69b944ef56abc69 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Thu, 8 Jul 2021 14:16:31 +0300 Subject: [PATCH 03/11] =?UTF-8?q?=D0=97=D0=B0=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=BA=D0=BE=D0=B4=D0=B8=D1=80=D0=BE=D0=B2=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=82=D0=B5=D0=BA=D1=81=D1=82=D0=BE=D0=B2=D1=8B=D1=85?= =?UTF-8?q?=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/core.h | 2 +- src/Core/figure.h | 2 +- src/Core/figure_enum.h | 2 +- src/Core/figures.h | 2 +- src/Core/player.cpp | 16 ++++++++-------- src/Core/rectangle.h | 2 +- src/Core/world.cpp | 2 +- src/Core/world.h | 2 +- src/Game.cpp | 12 ++++++------ src/Game.h | 2 +- 10 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Core/core.h b/src/Core/core.h index 356bcb0..0d0d262 100644 --- a/src/Core/core.h +++ b/src/Core/core.h @@ -1,5 +1,5 @@ /* -* , +* содержит все заголовочные файлы, на данный момент */ #pragma once diff --git a/src/Core/figure.h b/src/Core/figure.h index 859c652..bd635e2 100644 --- a/src/Core/figure.h +++ b/src/Core/figure.h @@ -6,7 +6,7 @@ #include "rectangle.h" #include "figure_enum.h" -// +// Представляет собой фигуру, которой управляет игрок class Figure { public: diff --git a/src/Core/figure_enum.h b/src/Core/figure_enum.h index 2752a36..b8a7b57 100644 --- a/src/Core/figure_enum.h +++ b/src/Core/figure_enum.h @@ -1,6 +1,6 @@ #pragma once -// +// Перечисление всех возможных фигур enum class FigureEnum { O, I, S, Z, L, J, T diff --git a/src/Core/figures.h b/src/Core/figures.h index 0093318..fb559b0 100644 --- a/src/Core/figures.h +++ b/src/Core/figures.h @@ -1,7 +1,7 @@ #pragma once #include "figure.h" -// +// Представляет собой предзаготовленные фигуры struct Figures { static Figure o; diff --git a/src/Core/player.cpp b/src/Core/player.cpp index 81e44a3..3cd1031 100644 --- a/src/Core/player.cpp +++ b/src/Core/player.cpp @@ -1,4 +1,4 @@ -#include "player.h" +#include "player.h" Player::Player(World* world, FigureEnum figure) : x{ 0 }, y{ 0 }, world{ world } @@ -8,11 +8,11 @@ Player::Player(World* world, FigureEnum figure) void Player::LoadToWorldArr() { - // + // Загружаем фигуру в массив карты for (int i = 0; i < figure.size; ++i) { Rec rec = Rec(figure[i]); - // + // Переводим локальные координаты в глобальные лол rec.x += x; rec.y += y; rec.is_occupied = true; @@ -41,14 +41,14 @@ void Player::ChangeFigure(FigureEnum figures) bool Player::CanMove(const char* direction) { - // rec , + // Сканируем rec сверху вниз, слева направо for (int i = 0; i < figure.size; ++i) { - // , + // Координаты квадратов, переведенные в мировые int world_x = x + figure[i].x; int world_y = y + figure[i].y; - // TODO: up, down . + // TODO: сделать перечисление up, down и тд. if ((direction == "right")) { @@ -72,7 +72,7 @@ bool Player::CanMove(const char* direction) { LoadToWorldArr(); - // + // Возвращаем фигуру в начало x = 0; y = 0; @@ -90,7 +90,7 @@ void Player::RotateFigure() { if(figure.figure_type != FigureEnum::O) { - // + // Поворот левосторонней системы против часовой // x' = x * cos(90) + y * sin(90) // sin(90) = 1, cos(90) = 0; // y' = -x * sin(90) + y * cos(90) int x = figure[i].x; diff --git a/src/Core/rectangle.h b/src/Core/rectangle.h index 57999f5..3425423 100644 --- a/src/Core/rectangle.h +++ b/src/Core/rectangle.h @@ -2,7 +2,7 @@ #include -// - +// Представляе стобой квадрат - минимальную частицу мира class Rec { public: diff --git a/src/Core/world.cpp b/src/Core/world.cpp index 09b34a3..4b88a22 100644 --- a/src/Core/world.cpp +++ b/src/Core/world.cpp @@ -3,7 +3,7 @@ World::World(int bound_x, int bound_y) : bound_x{ bound_x }, bound_y{ bound_y }, arr{ } { - // TODO: Пофиксить месево, возможно заменить вектор на аррей + // TODO: Пофиксить месево, возможно заменить вектор на аррей лол size = bound_x * bound_y; arr.resize(size); } diff --git a/src/Core/world.h b/src/Core/world.h index e41ea0a..0321505 100644 --- a/src/Core/world.h +++ b/src/Core/world.h @@ -4,7 +4,7 @@ #include "rectangle.h" -// +// Представляет собой игровой мир class World { public: diff --git a/src/Game.cpp b/src/Game.cpp index d4d8611..8703534 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,6 +1,6 @@ /* -* , RayLib 3 -*Rec( rectangle) - , +*Эта игра классический тетрис, написана с помощью RayLib 3 +*Rec(значит rectangle) - минимальна¤ единица, из чего состоит фигура и уровень */ #include "Game.h" @@ -26,9 +26,9 @@ int main() while (!WindowShouldClose()) { - using namespace std::string_literals; // ""s + using namespace std::string_literals; // дл¤ ""s - // + // ”правление движением if (IsKeyPressed(KEY_UP) && player.CanMove("up")) player.y -= 1; if (IsKeyPressed(KEY_DOWN) && player.CanMove("down") ) @@ -66,7 +66,7 @@ int main() rec_struct.width = sector_size; rec_struct.height = sector_size; - // + // ќтрисовка элементов фигуры for (int i = 0; i < player.figure.size; ++i) { int tmp_pos_x = player.x + player.figure[i].x; @@ -79,7 +79,7 @@ int main() DrawRectangleRoundedLines(rec_struct, 0.5f, 1, 3.0f, player.figure[i].outline_color); } - // + // ќтрисовка элементов уровн¤ for (int i = 0; i < world.size; ++i) { if(world.GetElement(i).is_occupied) diff --git a/src/Game.h b/src/Game.h index 56f67ab..c62dd58 100644 --- a/src/Game.h +++ b/src/Game.h @@ -1,4 +1,4 @@ -/* +/* * на случай, если проект станет сложнее я создал отдельный заголовок для заголовков */ From d5cc002116c0b4f614b7d27583423822f533b075 Mon Sep 17 00:00:00 2001 From: Grave Date: Tue, 13 Jul 2021 11:42:23 +0300 Subject: [PATCH 04/11] Create README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..e9f77a6 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +# Tetris +Игра Тетрис, сделанная с помощью RayLib 3.8 + +Для работы программы вам понадобится библиотека raylib.dll, которая должна находиться рядом с Tetris.exe +Для сборки программы вам понадобится набор заголовочных файлов и скомпилированных библиотек из этого репозитория: +https://github.com/raysan5/raylib From f44999a2d5a495d84f48118b71eb5c19c318906c Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 11:44:39 +0300 Subject: [PATCH 05/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B0=20=D0=B3=D0=B5=D0=BD=D0=B5=D1=80=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D1=8F=20=D1=81=D0=BB=D1=83=D1=87=D0=B0=D0=B9=D0=BD=D1=8B?= =?UTF-8?q?=D1=85=20=D1=87=D0=B8=D1=81=D0=B5=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/random_number.cpp | 16 ++++++++++++++++ src/Core/random_number.h | 8 ++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/Core/random_number.cpp create mode 100644 src/Core/random_number.h diff --git a/src/Core/random_number.cpp b/src/Core/random_number.cpp new file mode 100644 index 0000000..147cfc6 --- /dev/null +++ b/src/Core/random_number.cpp @@ -0,0 +1,16 @@ +#include "random_number.h" +#include + +int RandomNumber::GetRandomNumber(int min, int max) +{ + // используем static, так как это значение нужно вычислить единожды + static const double fraction = 1.0 / (static_cast(RAND_MAX) + 1.0); + // Равномерно распределяем вычисление значения из нашего диапазона + return static_cast(rand() * fraction * (max - min + 1) + min); +} + +void RandomNumber::ResetRandomNumber() +{ + srand(static_cast(time(0))); // используем системные часы в качестве стартового значения + rand(); +} diff --git a/src/Core/random_number.h b/src/Core/random_number.h new file mode 100644 index 0000000..6b3eeb6 --- /dev/null +++ b/src/Core/random_number.h @@ -0,0 +1,8 @@ +#include + +class RandomNumber +{ +public: + static int GetRandomNumber(int min, int max); + static void ResetRandomNumber(); +}; \ No newline at end of file From 59b4c0b68b06b12b315f19818e5d48e1735ffe45 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:29:39 +0300 Subject: [PATCH 06/11] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B9=D0=BA=D0=B0=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Tetris.vcxproj | 62 +++++++++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/Tetris.vcxproj b/Tetris.vcxproj index b580a3f..be8063c 100644 --- a/Tetris.vcxproj +++ b/Tetris.vcxproj @@ -22,6 +22,7 @@ + @@ -32,6 +33,7 @@ + @@ -91,106 +93,114 @@ true - C:\Programs\Visual Studio\Ode\ode-0.16.2\include;C:\Programs\Visual Studio\RayLib 3.7.0\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); - C:\Programs\Visual Studio\Ode\build\Debug;C:\Programs\Visual Studio\RayLib 3.7.0\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86) + C:\dev\dependencies\raylib\raylib 3.8.0 dev\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); + C:\dev\dependencies\raylib\raylib 3.8.0 dev\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86) $(SolutionDir)bin\$(ProjectName)\$(Platform)\$(Configuration)\ $(SolutionDir)bin\$(ProjectName)\intermediates\$(Platform)\$(Configuration)\ false - C:\Programs\Visual Studio\Ode\ode-0.16.2\include;C:\Programs\Visual Studio\RayLib 3.7.0\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); - C:\Programs\Visual Studio\Ode\build\Debug;C:\Programs\Visual Studio\RayLib 3.7.0\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86) + C:\dev\dependencies\raylib\raylib 3.8.0 dev\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); + C:\dev\dependencies\raylib\raylib 3.8.0 dev\projects\VS2019\build\raylib\bin\x64\Release.DLL;$(VC_LibraryPath_x86);$(WindowsSDK_LibraryPath_x86) $(SolutionDir)bin\$(ProjectName)\$(Platform)\$(Configuration)\ $(SolutionDir)bin\$(ProjectName)\intermediates\$(Platform)\$(Configuration)\ true - C:\Programs\Visual Studio\Ode\ode-0.16.2\include;C:\Programs\Visual Studio\RayLib 3.7.0\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); - C:\Programs\Visual Studio\Ode\build\Debug;C:\Programs\Visual Studio\RayLib 3.7.0\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64) + C:\dev\dependencies\raylib\raylib 3.8.0 dev\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); + C:\dev\dependencies\raylib\raylib 3.8.0 dev\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64) $(SolutionDir)bin\$(ProjectName)\$(Platform)\$(Configuration)\ $(SolutionDir)bin\$(ProjectName)\intermediates\$(Platform)\$(Configuration)\ false - C:\Programs\Visual Studio\Ode\ode-0.16.2\include;C:\Programs\Visual Studio\RayLib 3.7.0\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); - C:\Programs\Visual Studio\Ode\build\Debug;C:\Programs\Visual Studio\RayLib 3.7.0\projects\VS2019\build\raylib\bin\x64\Debug.DLL;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64) + C:\dev\dependencies\raylib\raylib 3.8.0 dev\src;$(VC_IncludePath);$(WindowsSDK_IncludePath); + C:\dev\dependencies\raylib\raylib 3.8.0 dev\projects\VS2019\build\raylib\bin\x64\Release.DLL;$(VC_LibraryPath_x64);$(WindowsSDK_LibraryPath_x64) $(SolutionDir)bin\$(ProjectName)\$(Platform)\$(Configuration)\ $(SolutionDir)bin\$(ProjectName)\intermediates\$(Platform)\$(Configuration)\ - Level3 + Level2 true - GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;dIDEDOUBLE;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp17 - 4996;26812;4068;26495 + 4996;26812;4068;26495;26451 + stdc11 Console true - ode_doubled.lib;raylib.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + raylib.lib;winmm.lib;%(AdditionalDependencies) - Level3 + Level2 true true true - GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;dIDEDOUBLE;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp17 - 4996;26812;4068;26495 + 4996;26812;4068;26495;26451 + stdc11 Console true true - true - ode_doubled.lib;raylib.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + false + raylib.lib;winmm.lib;%(AdditionalDependencies) + /SUBSYSTEM:WINDOWS +/ENTRY:mainCRTStartup %(AdditionalOptions) - Level3 + Level2 true - GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;dIDEDOUBLE;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp17 - 4996;26812;4068;26495 + 4996;26812;4068;26495;26451 + stdc11 Console true - ode_doubled.lib;raylib.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + raylib.lib;winmm.lib;%(AdditionalDependencies) - Level3 + Level2 true true true - GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;dIDEDOUBLE;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + GRAPHICS_API_OPENGL_33;PLATFORM_DESKTOP;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true stdcpp17 - 4996;26812;4068;26495 + 4996;26812;4068;26495;26451 + stdc11 Console true true - true - ode_doubled.lib;raylib.lib;winmm.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies) + false + raylib.lib;winmm.lib;%(AdditionalDependencies) + /SUBSYSTEM:WINDOWS +/ENTRY:mainCRTStartup %(AdditionalOptions) From a32503517ef96657f72d6b10b104d4a5dc75f780 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:31:27 +0300 Subject: [PATCH 07/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BE=D0=BF?= =?UTF-8?q?=D0=B5=D1=80=D0=B0=D1=82=D0=BE=D1=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit В классе Figure --- src/Core/figure.cpp | 5 +++++ src/Core/figure.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/Core/figure.cpp b/src/Core/figure.cpp index b9c18c0..9358194 100644 --- a/src/Core/figure.cpp +++ b/src/Core/figure.cpp @@ -16,6 +16,11 @@ Figure::Figure(std::vector rectangles, FigureEnum type, Color color, Color } Rec& Figure::operator[](unsigned int index) +{ + return recs[index]; +} + +const Rec& Figure::operator[](unsigned int index) const { return recs[index]; } \ No newline at end of file diff --git a/src/Core/figure.h b/src/Core/figure.h index bd635e2..c85612f 100644 --- a/src/Core/figure.h +++ b/src/Core/figure.h @@ -22,4 +22,5 @@ class Figure Figure(std::vector rectangles, FigureEnum type, Color color = BLACK, Color outline_color = BLACK); Rec& operator[](unsigned int index); + const Rec& operator[](unsigned int index) const; }; From 67c9c7216e9b4822ebd258134af5318ed9c05233 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:32:11 +0300 Subject: [PATCH 08/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D0=BD=D0=BE=D0=BC=D0=B5=D1=80=20=D1=81=D0=B0?= =?UTF-8?q?=D0=BC=D0=BE=D0=B3=D0=BE=20=D0=B1=D0=BE=D0=BB=D1=8C=D1=88=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20=D1=8D=D0=BB=D0=B5=D0=BC=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit В перечислении FigureEnum --- src/Core/figure_enum.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/figure_enum.h b/src/Core/figure_enum.h index b8a7b57..15e3ac8 100644 --- a/src/Core/figure_enum.h +++ b/src/Core/figure_enum.h @@ -3,5 +3,5 @@ // Перечисление всех возможных фигур enum class FigureEnum { - O, I, S, Z, L, J, T + O, I, S, Z, L, J, T, MAX_ELEMENT }; \ No newline at end of file From db65f29b88548fb995eec7c62ac5398d80d51479 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:34:57 +0300 Subject: [PATCH 09/11] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81?= =?UTF-8?q?=D0=B0=D1=85=20Player=20=D0=B8=20World?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Изменена система поворота фигуры игроком и способ загрузки фигуры в массив игровой карты --- src/Core/player.cpp | 62 +++++++++++++++++++++++++++++---------------- src/Core/player.h | 5 +++- src/Core/world.cpp | 55 +++++++++++++++++++++++++++++++++++++--- src/Core/world.h | 9 +++++-- 4 files changed, 102 insertions(+), 29 deletions(-) diff --git a/src/Core/player.cpp b/src/Core/player.cpp index 3cd1031..2fcb7ed 100644 --- a/src/Core/player.cpp +++ b/src/Core/player.cpp @@ -1,24 +1,37 @@ #include "player.h" +#include "random_number.h" + +Player::Player(World* world) + : x{ 0 }, y{ 0 }, world{ world } +{ + ChangeFigureRandom(); + ReturnFigureToStartPosition(); +} Player::Player(World* world, FigureEnum figure) : x{ 0 }, y{ 0 }, world{ world } { ChangeFigure(figure); + ReturnFigureToStartPosition(); } -void Player::LoadToWorldArr() +void Player::LoadFigureToWorldArr() { + //TODO: Все не так, надо заменить отправку объектов на отправку данных, наверно // Загружаем фигуру в массив карты for (int i = 0; i < figure.size; ++i) { - Rec rec = Rec(figure[i]); - // Переводим локальные координаты в глобальные лол - rec.x += x; - rec.y += y; - rec.is_occupied = true; - - world->SetElement(rec); + world->SetElementByPosition(figure[i].x + x, figure[i].y + y, figure[i].color); } + + world->ScanForCompleteRows(); +} + +// Возвращаем фигуру в начало и меняем на рандомную +void Player::ReturnFigureToStartPosition() +{ + x = 4; + y = 1; } void Player::ChangeFigure(FigureEnum figures) @@ -39,12 +52,18 @@ void Player::ChangeFigure(FigureEnum figures) figure = Figures::t; } +void Player::ChangeFigureRandom() +{ + RandomNumber::ResetRandomNumber(); + ChangeFigure((FigureEnum)RandomNumber::GetRandomNumber(0, (int)FigureEnum::MAX_ELEMENT - 1)); +} + bool Player::CanMove(const char* direction) { - // Сканируем rec сверху вниз, слева направо + // Сканируем фигуру сверху вниз, слева направо for (int i = 0; i < figure.size; ++i) { - // Координаты квадратов, переведенные в мировые + // Координаты квадратов фигуры, переведенные в мировые int world_x = x + figure[i].x; int world_y = y + figure[i].y; @@ -70,11 +89,9 @@ bool Player::CanMove(const char* direction) { if ((world_y + 1) >= world->bound_y || world->IsElementOccupied(world_x, world_y + 1)) { - LoadToWorldArr(); - - // Возвращаем фигуру в начало - x = 0; - y = 0; + LoadFigureToWorldArr(); + ChangeFigureRandom(); + ReturnFigureToStartPosition(); return false; } @@ -86,18 +103,19 @@ bool Player::CanMove(const char* direction) void Player::RotateFigure() { - for (int i = 0; i < figure.size; ++i) + if (figure.figure_type != FigureEnum::O && (x - 1) > -1 && (x + 1) < world->bound_x) { - if(figure.figure_type != FigureEnum::O) + for (int i = 0; i < figure.size; ++i) { + // Поворот левосторонней системы против часовой // x' = x * cos(90) + y * sin(90) // sin(90) = 1, cos(90) = 0; // y' = -x * sin(90) + y * cos(90) - int x = figure[i].x; - int y = figure[i].y; - - figure[i].x = y; - figure[i].y = -x; + int local_x = figure[i].y; + int local_y = -figure[i].x; + + figure[i].x = local_x; + figure[i].y = local_y; } } } \ No newline at end of file diff --git a/src/Core/player.h b/src/Core/player.h index 32c0b2e..eaa884b 100644 --- a/src/Core/player.h +++ b/src/Core/player.h @@ -13,13 +13,16 @@ class Player World* world; Figure figure; + Player(World* world); Player(World* world, FigureEnum figure); private: - void LoadToWorldArr(); + void LoadFigureToWorldArr(); + void ReturnFigureToStartPosition(); public: void ChangeFigure(FigureEnum figures); + void ChangeFigureRandom(); bool CanMove(const char* direction); void RotateFigure(); }; \ No newline at end of file diff --git a/src/Core/world.cpp b/src/Core/world.cpp index 4b88a22..deb6192 100644 --- a/src/Core/world.cpp +++ b/src/Core/world.cpp @@ -6,14 +6,16 @@ World::World(int bound_x, int bound_y) // TODO: Пофиксить месево, возможно заменить вектор на аррей лол size = bound_x * bound_y; arr.resize(size); + + row_element_count.resize(bound_y); } void World::ClearWorld() { for (auto& elem : arr) { - if(elem.is_occupied) - elem = Rec(); + if (elem.is_occupied) + elem.Clear(); } } @@ -26,7 +28,7 @@ Rec& World::GetElement(int element) throw("Element at this number doesn't exist"); } -void World::SetElement(Rec& element) +void World::SetElement(const Rec &element) { // Переводим двумерный индекс массива в одномерный int element_number = element.x + element.y * bound_x; @@ -37,6 +39,22 @@ void World::SetElement(Rec& element) throw("Element at this number doesn't exist"); } +// Загружает элемент в массив карты +void World::SetElementByPosition(int world_x, int world_y, Color color) +{ + int index = world_x + world_y * bound_x; + + if(index < arr.size() && !arr[index].is_occupied) + { + arr[index].x = world_x; + arr[index].y = world_y; + arr[index].is_occupied = true; + arr[index].color = color; + } + else + throw("Element at this number doesn't exist or already occupied"); +} + // Обращение как к двумерному массиву bool World::IsElementOccupied(int x, int y) { @@ -44,6 +62,35 @@ bool World::IsElementOccupied(int x, int y) return arr[x + y * bound_x].is_occupied; else return false; - +} + +void World::ScanForCompleteRows() +{ + for (int y = 0; y < bound_y; ++y) + { + int num_of_occupied = 0; + for (int x = 0; x < bound_x; ++x) + { + if (arr[x + y * bound_x].is_occupied) + ++num_of_occupied; + } + if (num_of_occupied == bound_x) + ClearRow(y); + } +} + +// Заполняет ряд элементами, распологающимися выше по y на 1 +void World::ClearRow(int row) +{ + if (row - 1 >= 0) + { + for (int x = 0; x < bound_x; ++x) + { + // Копируем параметры верхнего элемента + arr[x + row * bound_x].is_occupied = arr[x + (row - 1) * bound_x].is_occupied; + arr[x + row * bound_x].color = arr[x + (row - 1) * bound_x].color; + } + ClearRow(row - 1); + } } diff --git a/src/Core/world.h b/src/Core/world.h index 0321505..7da2a72 100644 --- a/src/Core/world.h +++ b/src/Core/world.h @@ -3,6 +3,7 @@ #include #include "rectangle.h" +#include "figure.h" // Представляет собой игровой мир class World @@ -14,13 +15,17 @@ class World private: std::vector arr; + std::vector row_element_count; public: World(int bound_x, int bound_y); - void ClearWorld(); - void SetElement(Rec& element); + void SetElement(const Rec &element); + void SetElementByPosition(int world_x, int world_y, Color color); Rec& GetElement(int element); bool IsElementOccupied(int x, int y); + + void ScanForCompleteRows(); + void ClearRow(int row); }; \ No newline at end of file From 063de6d0298c75f59ea02c94569bd8d1eec0a5bd Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:35:37 +0300 Subject: [PATCH 10/11] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=20=D1=81=D0=BF=D0=BE=D1=81=D0=BE=D0=B1=20=D0=B2?= =?UTF-8?q?=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=89=D0=B5=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?Rec=20=D0=BA=20=D0=B8=D0=B7=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=D1=8C?= =?UTF-8?q?=D0=BD=D0=BE=D0=BC=D1=83=20=D1=81=D0=BE=D1=81=D1=82=D0=BE=D1=8F?= =?UTF-8?q?=D0=BD=D0=B8=D1=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Core/rectangle.cpp | 9 +++++++++ src/Core/rectangle.h | 2 ++ 2 files changed, 11 insertions(+) diff --git a/src/Core/rectangle.cpp b/src/Core/rectangle.cpp index 19c0cf5..4266fb7 100644 --- a/src/Core/rectangle.cpp +++ b/src/Core/rectangle.cpp @@ -4,3 +4,12 @@ Rec::Rec(int x, int y, Color color, Color outline_color) // x = 0, y = 0, color = BLACK, outline_color = BLACK : x{ x }, y{ y }, is_occupied{ false }, color{ color }, outline_color{ outline_color } {} + +void Rec::Clear() +{ + x = 0; + y = 0; + is_occupied = false; + color = BLACK; + outline_color = BLACK; +} diff --git a/src/Core/rectangle.h b/src/Core/rectangle.h index 3425423..e82c734 100644 --- a/src/Core/rectangle.h +++ b/src/Core/rectangle.h @@ -12,4 +12,6 @@ class Rec Color outline_color; Rec(int x = 0, int y = 0, Color color = BLACK, Color outline_color = BLACK); + + void Clear(); }; From 8037f4994895f84e55cffdcdb9ae0dafeb697492 Mon Sep 17 00:00:00 2001 From: Grave188 Date: Tue, 13 Jul 2021 12:37:59 +0300 Subject: [PATCH 11/11] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D1=81=D0=B8=D1=81=D1=82=D0=B5?= =?UTF-8?q?=D0=BC=D1=83=20=D1=83=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=B8=D0=B3=D1=80=D0=BE=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Теперь фигура движется со временем сама, игрок имеет возможность передвигать фигуру налево и направо. Так же появилась возможность переключать режим Debug клавишей D --- src/Game.cpp | 100 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 63 insertions(+), 37 deletions(-) diff --git a/src/Game.cpp b/src/Game.cpp index 8703534..cfe84f2 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,6 +1,7 @@ /* -*Эта игра классический тетрис, написана с помощью RayLib 3 -*Rec(значит rectangle) - минимальна¤ единица, из чего состоит фигура и уровень +* Эта игра классический тетрис, написана с помощью RayLib 3.8 +* Rec(значит rectangle) - минимальная единица, из чего состоит фигура и уровень +* Figure(фигура) - то, чем управляет игрок */ #include "Game.h" @@ -10,53 +11,75 @@ const int level_height = 20; const int sector_size = 50; const int scr_width = level_width * sector_size; const int scr_height = level_height * sector_size; -const char* title = "Tetris"; +const char* title = "Tetris"; + +static bool isDebugging = false; int main() { InitWindow(scr_width, scr_height, title); SetTargetFPS(60); - int bound_x = scr_width / sector_size; int bound_y = scr_height / sector_size; World world = World(bound_x, bound_y); - Player player = Player(&world, FigureEnum::I); + Player player = Player(&world); + float pos_y = 0; while (!WindowShouldClose()) { - using namespace std::string_literals; // дл¤ ""s - - // ”правление движением - if (IsKeyPressed(KEY_UP) && player.CanMove("up")) - player.y -= 1; - if (IsKeyPressed(KEY_DOWN) && player.CanMove("down") ) - player.y += 1; + using namespace std::string_literals; // для ""s + + float dt{ GetFrameTime() }; + float speed{ 0.0f }; + + // Управление движением + if (IsKeyDown(KEY_DOWN)) + speed = 10.0f; + else + speed = 2.5f; + + pos_y += speed * dt; + player.y = pos_y; + + if (player.CanMove("down")) + player.y = pos_y; + else + pos_y = 0.0f; + + if (IsKeyPressed(KEY_RIGHT) && player.CanMove("right") ) player.x += 1; if (IsKeyPressed(KEY_LEFT) && player.CanMove("left")) player.x -= 1; - if (IsKeyPressed(KEY_SPACE)) + if (IsKeyPressed(KEY_UP)) player.RotateFigure(); - if (IsKeyPressed(KEY_BACKSPACE)) - world.ClearWorld(); - - if (IsKeyPressed(KEY_O)) - player.ChangeFigure(FigureEnum::O); - if (IsKeyPressed(KEY_I)) - player.ChangeFigure(FigureEnum::I); - if (IsKeyPressed(KEY_S)) - player.ChangeFigure(FigureEnum::S); - if (IsKeyPressed(KEY_Z)) - player.ChangeFigure(FigureEnum::Z); - if (IsKeyPressed(KEY_L)) - player.ChangeFigure(FigureEnum::L); - if (IsKeyPressed(KEY_J)) - player.ChangeFigure(FigureEnum::J); - if (IsKeyPressed(KEY_T)) - player.ChangeFigure(FigureEnum::T); + // Debug управление + if (IsKeyPressed(KEY_D)) + isDebugging = !isDebugging; + + if(isDebugging) + { + if (IsKeyPressed(KEY_O)) + player.ChangeFigure(FigureEnum::O); + else if (IsKeyPressed(KEY_I)) + player.ChangeFigure(FigureEnum::I); + else if (IsKeyPressed(KEY_S)) + player.ChangeFigure(FigureEnum::S); + else if (IsKeyPressed(KEY_Z)) + player.ChangeFigure(FigureEnum::Z); + else if (IsKeyPressed(KEY_L)) + player.ChangeFigure(FigureEnum::L); + else if (IsKeyPressed(KEY_J)) + player.ChangeFigure(FigureEnum::J); + else if (IsKeyPressed(KEY_T)) + player.ChangeFigure(FigureEnum::T); + + if (IsKeyPressed(KEY_BACKSPACE)) + world.ClearWorld(); + } BeginDrawing(); @@ -66,7 +89,7 @@ int main() rec_struct.width = sector_size; rec_struct.height = sector_size; - // ќтрисовка элементов фигуры + // Отрисовка элементов фигуры for (int i = 0; i < player.figure.size; ++i) { int tmp_pos_x = player.x + player.figure[i].x; @@ -79,7 +102,7 @@ int main() DrawRectangleRoundedLines(rec_struct, 0.5f, 1, 3.0f, player.figure[i].outline_color); } - // ќтрисовка элементов уровн¤ + // Отрисовка элементов уровня for (int i = 0; i < world.size; ++i) { if(world.GetElement(i).is_occupied) @@ -95,11 +118,14 @@ int main() } // Debug - std::string debug = "pos "s + std::to_string(player.x) + ", " + std::to_string(player.y) - + "\n left: " + std::to_string(player.CanMove("left")) - + "\n right: " + std::to_string(player.CanMove("right")) - + "\n down: " + std::to_string(player.CanMove("down")); - DrawText(debug.c_str(), 10, 10, 30, RED); + if(isDebugging) + { + std::string debug = "pos "s + std::to_string(player.x) + ", " + std::to_string(player.y) + + "\n left: " + std::to_string(player.CanMove("left")) + + "\n right: " + std::to_string(player.CanMove("right")) + + "\n down: " + std::to_string(player.CanMove("down")); + DrawText(debug.c_str(), 10, 10, 30, RED); + } EndDrawing(); }