Skip to content

Instantly share code, notes, and snippets.

@Flaze07
Created July 11, 2017 16:32
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Flaze07/f008e65c0c85bcf44f0c46037447f7b5 to your computer and use it in GitHub Desktop.
hi...somehow menu.draw() has a problem
#include <TGUI/TGUI.hpp>
#include "include/apple.hpp"
#include "include/snake1.hpp"
#include "include/snake2.hpp"
int main()
{
sf::RenderWindow app_win{sf::VideoMode{450, 450}, "snake vs snake"};
sf::Vector2f win_size{(float) app_win.getSize().x, (float) app_win.getSize().y};
enum class Condition {start, single, multi, over};
Condition condition = Condition::start;
Condition oldcon;
const sf::Time snake_speed = sf::milliseconds(100);
const float size = 10;
Snake1 snake1{win_size, size, snake_speed};
Snake2 snake2{win_size, size, snake_speed};
tgui::Gui menu;
tgui::Gui over;
try
{
tgui::Button::Ptr single = tgui::Button::create();
single->setSize(100, 100);
single->setText("single player");
single->setPosition(225 - 50, 250 - 50);
single->connect("pressed", [&]{
condition = Condition::single;
oldcon = condition;
});
menu.add(single, "single");
tgui::Button::Ptr multi = tgui::Button::create();
multi->setSize(100, 100);
multi->setText("multi player");
multi->setPosition(225 - 50, 375 - 50);
multi->connect("pressed", [&]{
condition = Condition::multi;
oldcon = condition;
});
menu.add(multi, "multi");
tgui::Button::Ptr exit = tgui::Button::create();
exit->setSize(50, 50);
exit->setText("exit");
exit->setPosition(0, 0);
exit->connect("pressed", [&]{app_win.close();});
menu.add(exit, "exit");
}
catch (const tgui::Exception& e)
{
std::cerr << "TGUI Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
try
{
tgui::Button::Ptr goto_menu = tgui::Button::create();
goto_menu->setSize(100, 100);
goto_menu->setText("return to menu");
goto_menu->setPosition(225 - 50, 250 - 50);
goto_menu->connect("pressed", [&]{condition = Condition::start;});
over.add(goto_menu);
tgui::Button::Ptr restart = tgui::Button::create();
restart->setSize(100, 100);
restart->setText("restart the game");
restart->setPosition(225 - 50, 375 - 50);
restart->connect("pressed", [&]{condition = oldcon;});
over.add(restart);
tgui::Button::Ptr exit = tgui::Button::create();
exit->setSize(50, 50);
exit->setText("exit");
exit->setPosition(0, 0);
exit->connect("pressed", [&]{app_win.close();});
over.add(exit, "exit");
}
catch (const tgui::Exception& e)
{
std::cerr << "TGUI Exception: " << e.what() << std::endl;
return EXIT_FAILURE;
}
Apple apple{app_win, size};
sf::Clock clock;
while (app_win.isOpen())
{
sf::Event event;
if (condition == Condition::start)
{
while (app_win.pollEvent(event))
{
if (event.type == sf::Event::Closed) app_win.close();
menu.handleEvent(event);
}
app_win.clear();
menu.draw();
app_win.display();
}
else if (condition == Condition::single)
{
sf::Time elapsed = clock.restart();
snake1.update(elapsed);
snake1.action();
snake1.check_apple(apple);
if (snake1.collision(app_win))
{
condition = Condition::over;
}
app_win.clear();
app_win.draw(apple.getShape());
for (auto& a : snake1.getSnake())
{
app_win.draw(a);
}
app_win.display();
}
else if (condition == Condition::multi)
{
sf::Time elapsed = clock.restart();
snake1.update(elapsed);
snake1.action();
snake1.check_apple(apple);
if (snake1.check_snake(snake2))
{
condition = Condition::over;
}
if (snake1.collision(app_win))
{
condition = Condition::over;
}
snake2.update(elapsed);
snake2.action();
snake2.check_apple(apple);
if (snake2.check_snake(snake1))
{
condition = Condition::over;
}
if (snake2.collision(app_win))
{
condition = Condition::over;
}
app_win.clear();
app_win.draw(apple.getShape());
for (auto& a : snake1.getSnake())
{
app_win.draw(a);
}
for (auto& a : snake2.getSnake())
{
app_win.draw(a);
}
app_win.display();
}
else if (condition == Condition::over)
{
while (app_win.pollEvent(event))
{
if (event.type == sf::Event::Closed) app_win.close();
over.handleEvent(event);
}
if (oldcon == Condition::multi);
app_win.clear();
over.draw();
app_win.display();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment