Skip to content

Instantly share code, notes, and snippets.

@LaBlazer
Last active September 2, 2019 15:46
Show Gist options
  • Save LaBlazer/389c92740c3bce3183202472ff971365 to your computer and use it in GitHub Desktop.
Save LaBlazer/389c92740c3bce3183202472ff971365 to your computer and use it in GitHub Desktop.
Simple hangman game made in C++ using SFML library
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
std::vector<std::string> wordlist;
srand(time(NULL));
{
std::ifstream infile;
std::string str;
infile.open("wordlist.txt");
while (!infile.eof())
{
std::getline(infile, str);
wordlist.push_back(str);
///std::cout << str << std::endl;
}
infile.close();
}
std::string word = wordlist.at(rand() % wordlist.size());
std::string word_obfuscated(word.size(), '_');
int lives = 5;
const int MAX_LIVES = 5;
const int SCRWIDTH = 500;
const int SCRHEIGHT = 500;
sf::RenderWindow window(sf::VideoMode(SCRWIDTH, SCRHEIGHT), "Hankman");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
sf::SoundBuffer sb;
if (!sb.loadFromFile("yee.wav")) {
std::cout << "FUCK, nemozem nacitat zvuk..." << std::endl;
}
sf::Sound s(sb);
sf::Font font;
if (!font.loadFromFile("sansation.ttf")) {
std::cout << "FUCK, nemozem nacitat font..." << std::endl;
}
sf::Text text(word_obfuscated, font);
text.setFillColor(sf::Color::Black);
text.setCharacterSize(30);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
text.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, SCRHEIGHT - 50));
sf::Texture textures[MAX_LIVES];
for (int i = 0; i < MAX_LIVES; i++) {
std::string filename = std::to_string(i) + ".png";
if (!textures[i].loadFromFile(filename)) {
std::cout << "Nejde nacitat obrazok " << filename << std::endl;
}
}
sf::Sprite hangman;
sf::FloatRect spriteRect = hangman.getLocalBounds();
hangman.setOrigin(spriteRect.left + spriteRect.width / 2.0f,
spriteRect.top + spriteRect.height / 2.0f);
hangman.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, 10));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::TextEntered && event.text.unicode < 128 && event.text.unicode > 32 && lives != 0) {
char pressedchar = static_cast<char>(event.text.unicode);
size_t position = word.find(pressedchar);
if (position != std::string::npos) {
while (position != std::string::npos)
{
word_obfuscated.replace(position, 1, 1, pressedchar);
position = word.find(pressedchar, position + 1);
}
//if the obfuscated string has 0 _'s vi von zulul
if (std::count(word_obfuscated.begin(), word_obfuscated.end(), '_') == 0) {
word_obfuscated += " <";
word_obfuscated.insert(0, "> ");
s.play();
}
text.setString(word_obfuscated);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
text.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, SCRHEIGHT - 50));
}
else {
lives--;
hangman.setTexture(textures[MAX_LIVES - lives - 1]);
//center toho koktoa
sf::FloatRect spriteRect = hangman.getLocalBounds();
hangman.setOrigin(spriteRect.left + spriteRect.width / 2.0f,
spriteRect.top + spriteRect.height / 2.0f);
hangman.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, SCRHEIGHT / 2.0f));
if (lives == 0) {
text.setString("Game Over!!!! Press space to reset");
//vycentruj
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
text.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, SCRHEIGHT - 50));
}
}
}
else if (event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space) {
//resets game
lives = MAX_LIVES;
word = wordlist.at(rand() % wordlist.size());
word_obfuscated = std::string(word.size(), '_');
hangman.setTexture(textures[-1]);
text.setString(word_obfuscated);
sf::FloatRect textRect = text.getLocalBounds();
text.setOrigin(textRect.left + textRect.width / 2.0f,
textRect.top + textRect.height / 2.0f);
text.setPosition(sf::Vector2f(SCRWIDTH / 2.0f, SCRHEIGHT - 50));
}
}
window.clear(sf::Color::White);
window.draw(hangman);
window.draw(text);
window.display();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment