diff --git a/sem2/BlagodarenkoAM/Tamagochi/arial.ttf b/sem2/BlagodarenkoAM/Tamagochi/arial.ttf new file mode 100644 index 00000000..ff0815cd Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/arial.ttf differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/cat.cpp b/sem2/BlagodarenkoAM/Tamagochi/cat.cpp new file mode 100644 index 00000000..c0de6f09 --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/cat.cpp @@ -0,0 +1,48 @@ +#include "cat.h" +#include + +void Cat::live() +{ + catGrow(); + decreaseFull(0.1); // Замедляем скорость падения характеристик + decreaseHydr(0.1); + decreaseClean(0.1); +} + +void Cat::catGrow() +{ + if (full >= maxFull) { + full = 50; + increaseSize(); + } + else if (full <= 0) { + full = 99; + decreaseSize(); + } + else if (hydr <= 0) { + hydr = 99; + decreaseSize(); + } + else if (clean <= 0) { + clean = 99; + decreaseSize(); + } +} + +void Cat::increaseFull(int value) +{ + if (full + value < maxFull) { full += value; } + else {full = maxFull;} +} + +void Cat::increaseHydr(int value) +{ + if (hydr + value < maxHydr) { hydr += value; } + else {hydr = maxHydr;} +} + +void Cat::increaseClean(int value) +{ + if (clean + value < maxClean) { clean += value; } + else {clean = maxClean;} +} diff --git a/sem2/BlagodarenkoAM/Tamagochi/cat.h b/sem2/BlagodarenkoAM/Tamagochi/cat.h new file mode 100644 index 00000000..206598dc --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/cat.h @@ -0,0 +1,55 @@ +#pragma once + +class Cat { +private: + int size; + const int maxSize; + float speedGrowth; + float full; + const int maxFull; + float hydr; + const int maxHydr; + float clean; + const int maxClean; + +public: + Cat(float _speedGrowth, float _full, float _hydr, float _clean) + : size{ 10 }, speedGrowth{ _speedGrowth }, full{ _full }, hydr{ _hydr }, clean{ _clean }, maxSize{ 20 }, maxFull{ 200 }, maxHydr{ 200 }, maxClean{ 200 } {} + + void live(); + + bool isDead() {return size == 0} + + bool isSuccess() {return size >= maxSize;} + + void catGrow(); + + void increaseSize() {if (size < maxSize) size += speedGrowth;} + + void decreaseSize() {if (size > 0) size -= speedGrowth;} + + void increaseFull(int value); + + void decreaseFull(float value) { if (full > 0) full -= value; } + + void increaseHydr(int value); + + void decreaseHydr(float value) {if (hydr > 0) hydr -= value;} + + void increaseClean(int value); + + void decreaseClean(float value) {if (clean > 0) clean -= value;} + + int getSize() const { return size; } + int getMaxSize() const { return maxSize; } + float getFull() const { return full; } + int getMaxFull() const { return maxFull; } + float getHydr() const { return hydr; } + int getMaxHydr() const { return maxHydr; } + float getClean() const { return clean; } + int getMaxClean() const { return maxClean; } +}; + + + + diff --git a/sem2/BlagodarenkoAM/Tamagochi/cat1.png b/sem2/BlagodarenkoAM/Tamagochi/cat1.png new file mode 100644 index 00000000..51b2f427 Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/cat1.png differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/cat2.png b/sem2/BlagodarenkoAM/Tamagochi/cat2.png new file mode 100644 index 00000000..1298554c Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/cat2.png differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/cle.png b/sem2/BlagodarenkoAM/Tamagochi/cle.png new file mode 100644 index 00000000..126c4e3b Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/cle.png differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/elephant2.png b/sem2/BlagodarenkoAM/Tamagochi/elephant2.png new file mode 100644 index 00000000..49de29b5 Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/elephant2.png differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/endscr.cpp b/sem2/BlagodarenkoAM/Tamagochi/endscr.cpp new file mode 100644 index 00000000..3533a9c3 --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/endscr.cpp @@ -0,0 +1,39 @@ +#include "endscr.h" + +int endscr(sf::RenderWindow& window, int result) +{ + { + sf::Font font; + if (!font.loadFromFile("arial.ttf")) { + std::cerr << "Error loading font\n"; + return 1; + } + + sf::Text endText; + endText.setFont(font); + endText.setCharacterSize(50); + endText.setFillColor(sf::Color::Red); + + if (result == 1) { + endText.setString("Game Over"); + endText.setFillColor(sf::Color::Red); + } + else { + endText.setString("Success!"); + endText.setFillColor(sf::Color::Green); + } + + endText.setPosition(window.getSize().x / 2 - endText.getGlobalBounds().width / 2, window.getSize().y / 2 - endText.getGlobalBounds().height / 2); + + window.clear(); + window.draw(endText); + window.display(); + + sf::Clock clock; + while (clock.getElapsedTime().asSeconds() < 3) { + // Wait for 3 seconds + } + + return 0; + } +} diff --git a/sem2/BlagodarenkoAM/Tamagochi/endscr.h b/sem2/BlagodarenkoAM/Tamagochi/endscr.h new file mode 100644 index 00000000..61415f59 --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/endscr.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +int endscr(sf::RenderWindow& window, int result); + + + diff --git a/sem2/BlagodarenkoAM/Tamagochi/info.cpp b/sem2/BlagodarenkoAM/Tamagochi/info.cpp new file mode 100644 index 00000000..50d6e33e --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/info.cpp @@ -0,0 +1,18 @@ +#include "info.h" + +Info::Info(float full, int maxFull, float hydr, int maxHydr, float clean, int maxClean) +{ + values.push_back(std::make_pair(full, maxFull)); + values.push_back(std::make_pair(hydr, maxHydr)); + values.push_back(std::make_pair(clean, maxClean)); +} + +std::string Info::getStr() const +{ + std::string str; + for (const auto& value : values) { + int filled = static_cast((value.first / value.second) * 10); + str += std::string(filled, '|') + std::string(10 - filled, '.') + "\t\t\t\t"; + } + return str; +} diff --git a/sem2/BlagodarenkoAM/Tamagochi/info.h b/sem2/BlagodarenkoAM/Tamagochi/info.h new file mode 100644 index 00000000..463df9e5 --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/info.h @@ -0,0 +1,17 @@ +#pragma once + +#include +#include +#include + +class Info { +private: + std::vector> values; + +public: + Info(float full, int maxFull, float hydr, int maxHydr, float clean, int maxClean); + + std::string getStr() const; +}; + + diff --git a/sem2/BlagodarenkoAM/Tamagochi/main.cpp b/sem2/BlagodarenkoAM/Tamagochi/main.cpp new file mode 100644 index 00000000..48cbde76 --- /dev/null +++ b/sem2/BlagodarenkoAM/Tamagochi/main.cpp @@ -0,0 +1,188 @@ +#include +#include +#include +#include +#include +#include "cat.h" +#include "info.h" +#include "endscr.h" + +void updatePetPicture(const sf::Texture& texture, sf::Sprite& sprite) { + sprite.setTexture(texture); +} + +void handleButtonClick(Cat& cat, const std::string& action, const sf::Texture& texture, sf::Sprite& sprite) { + if (action == "AM-AM") { + cat.increaseFull(50); + } + else if (action == "WATER") { + cat.increaseHydr(50); + } + else if (action == "CLEAN") { + cat.increaseClean(50); + } + updatePetPicture(texture, sprite); +} + +void drawButtons(sf::RenderWindow& window, const std::vector& buttons) { + for (const auto& button : buttons) { + window.draw(button); + } +} + +int main() { + sf::RenderWindow window(sf::VideoMode(800, 600), "Tamagotchi"); + window.setFramerateLimit(144); + + sf::Font font; + if (!font.loadFromFile("arial.ttf")) { + std::cerr << "Error loading font\n"; + return 1; + } + + sf::Texture catTexture1, catTexture2, pigTexture1, pigTexture2, elephantTexture1, elephantTexture2; + if (!catTexture1.loadFromFile("cat1.png") || !catTexture2.loadFromFile("cat2.png") || + !pigTexture1.loadFromFile("pig1.png") || !pigTexture2.loadFromFile("pig2.png") || + !elephantTexture1.loadFromFile("elephant1.png") || !elephantTexture2.loadFromFile("elephant2.png")) { + std::cerr << "Error loading textures\n"; + return 1; + } + + sf::Sprite sprite; + sprite.setScale(sf::Vector2f(.01f, .01f)); + + std::string petChoice; + bool petChosen = false; + + sf::RectangleShape logoCat(sf::Vector2f(200, 200)), logopig(sf::Vector2f(200, 200)), logoelephant(sf::Vector2f(200, 200)); + logoCat.setTexture(&catTexture1); + logoCat.setPosition(30, 200); + logopig.setTexture(&pigTexture1); + logopig.setPosition(280, 200); + logoelephant.setTexture(&elephantTexture1); + logoelephant.setPosition(530, 200); + + while (window.isOpen() && !petChosen) { + sf::Vector2i mousePos = sf::Mouse::getPosition(window); + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) window.close(); + + if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { + if (logoCat.getGlobalBounds().contains(mousePos.x, mousePos.y)) { + petChoice = "cat"; + sprite.setTexture(catTexture1); + petChosen = true; + } + else if (logopig.getGlobalBounds().contains(mousePos.x, mousePos.y)) { + petChoice = "pig"; + sprite.setTexture(pigTexture1); + petChosen = true; + } + else if (logoelephant.getGlobalBounds().contains(mousePos.x, mousePos.y)) { + petChoice = "elephant"; + sprite.setTexture(elephantTexture1); + petChosen = true; + } + } + } + + window.clear(sf::Color::White); + window.draw(logoCat); + window.draw(logopig); + window.draw(logoelephant); + window.display(); + } + + Cat cat(1, 100, 100, 100); + Info info(cat.getFull(), cat.getMaxFull(), cat.getHydr(), cat.getMaxHydr(), cat.getClean(), cat.getMaxClean()); + + sf::Text text; + text.setFont(font); + text.setFillColor(sf::Color::Black); + text.setCharacterSize(20); + text.setPosition(window.getSize().x / 11, window.getSize().y / 10 * 8); + + sf::Text feedButton, waterButton, cleanButton; + feedButton.setFont(font); + feedButton.setString("AM-AM"); + feedButton.setCharacterSize(30); + feedButton.setFillColor(sf::Color::Black); + feedButton.setPosition(50, window.getSize().y - 100); + + waterButton.setFont(font); + waterButton.setString("WATER"); + waterButton.setCharacterSize(30); + waterButton.setFillColor(sf::Color::Black); + waterButton.setPosition(200, window.getSize().y - 100); + + cleanButton.setFont(font); + cleanButton.setString("CLEAN"); + cleanButton.setCharacterSize(30); + cleanButton.setFillColor(sf::Color::Black); + cleanButton.setPosition(350, window.getSize().y - 100); + + std::vector buttons = { feedButton, waterButton, cleanButton }; + auto startTime = std::chrono::high_resolution_clock::now(); + bool isPaused = false; + + while (window.isOpen()) { + sf::Event event; + if (!isPaused) { + if (cat.isDead()) { + endscr(window, 1); + } + if (cat.isSuccess()) { + endscr(window, 0); + window.clear(sf::Color::Green); + } + cat.live(); + sprite.setScale(sf::Vector2f(float(cat.getSize()) / cat.getMaxSize() / 10, float(cat.getSize()) / cat.getMaxSize() / 10)); + + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) window.close(); + + if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { + if (feedButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) { + handleButtonClick(cat, "AM-AM", petChoice == "cat" ? catTexture2 : (petChoice == "pig" ? pigTexture2 : elephantTexture2), sprite); + } + else if (waterButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) { + handleButtonClick(cat, "WATER", petChoice == "cat" ? catTexture2 : (petChoice == "pig" ? pigTexture2 : elephantTexture2), sprite); + } + else if (cleanButton.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y)) { + handleButtonClick(cat, "CLEAN", petChoice == "cat" ? catTexture2 : (petChoice == "pig" ? pigTexture2 : elephantTexture2), sprite); + } + } + + if (event.type == sf::Event::KeyPressed) { + if (event.key.code == sf::Keyboard::P) { + isPaused = true; + } + } + } + + Info info(cat.getFull(), cat.getMaxFull(), cat.getHydr(), cat.getMaxHydr(), cat.getClean(), cat.getMaxClean()); + text.setString(info.getStr()); + + window.clear(sf::Color::White); + window.draw(sprite); + window.draw(text); + drawButtons(window, buttons); + window.display(); + } + else { + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) window.close(); + + if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::O) { + isPaused = false; + } + } + } + } + + return 0; +} + + \ No newline at end of file diff --git a/sem2/BlagodarenkoAM/Tamagochi/pig1.png b/sem2/BlagodarenkoAM/Tamagochi/pig1.png new file mode 100644 index 00000000..d68ec0b8 Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/pig1.png differ diff --git a/sem2/BlagodarenkoAM/Tamagochi/pig2.png b/sem2/BlagodarenkoAM/Tamagochi/pig2.png new file mode 100644 index 00000000..c71dd724 Binary files /dev/null and b/sem2/BlagodarenkoAM/Tamagochi/pig2.png differ