Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tamagochi #123

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added sem2/BlagodarenkoAM/Tamagochi/arial.ttf
Binary file not shown.
48 changes: 48 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/cat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "cat.h"
#include <iostream>

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;}
}
55 changes: 55 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/cat.h
Original file line number Diff line number Diff line change
@@ -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; }
};




Binary file added sem2/BlagodarenkoAM/Tamagochi/cat1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sem2/BlagodarenkoAM/Tamagochi/cat2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sem2/BlagodarenkoAM/Tamagochi/cle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sem2/BlagodarenkoAM/Tamagochi/elephant2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/endscr.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
9 changes: 9 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/endscr.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <SFML/Graphics.hpp>
#include <iostream>

int endscr(sf::RenderWindow& window, int result);



18 changes: 18 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/info.cpp
Original file line number Diff line number Diff line change
@@ -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<int>((value.first / value.second) * 10);
str += std::string(filled, '|') + std::string(10 - filled, '.') + "\t\t\t\t";
}
return str;
}
17 changes: 17 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/info.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <vector>
#include <string>
#include <cstdarg>

class Info {
private:
std::vector<std::pair<float, int>> values;

public:
Info(float full, int maxFull, float hydr, int maxHydr, float clean, int maxClean);

std::string getStr() const;
};


188 changes: 188 additions & 0 deletions sem2/BlagodarenkoAM/Tamagochi/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <chrono>
#include <vector>
#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<sf::Text>& 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<sf::Text> 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;
}


Binary file added sem2/BlagodarenkoAM/Tamagochi/pig1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added sem2/BlagodarenkoAM/Tamagochi/pig2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.