-
Notifications
You must be signed in to change notification settings - Fork 0
/
MenuScreen.cpp
90 lines (73 loc) · 2.02 KB
/
MenuScreen.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <SFML/Graphics.hpp>
#include <memory>
#include "GameScreen.h"
#include "MenuScreen.h"
#include "Game.h"
#include "SettingScreen.h"
#include "stopwatch.h"
using namespace sfSnake;
Stopwatch sw;
MenuScreen::MenuScreen()
{
font_.loadFromFile("Fonts/game_over.ttf");
text_.setFont(font_);
text_.setString(
"\n\n\n\n\n\n\n\nPress [SPACE] to play"
"\n\nPress [ESC] to quit"
"\n\nPress [Tab] to setup");
text_.setColor(sf::Color::Blue);
text_.setCharacterSize(32);
snakeText_.setFont(font_);
snakeText_.setString("Snake!");
snakeText_.setColor(sf::Color::Green);
snakeText_.setCharacterSize(64);
snakeText_.setStyle(sf::Text::Bold);
sf::FloatRect textBounds = text_.getLocalBounds();
text_.setOrigin(textBounds.left + textBounds.width / 2,
textBounds.top + textBounds.height / 2);
text_.setPosition(Game::Width / 2, Game::Height / 2);
sf::FloatRect snakeTextBounds = snakeText_.getLocalBounds();
snakeText_.setOrigin(snakeTextBounds.left + snakeTextBounds.width / 2,
snakeTextBounds.top + snakeTextBounds.height / 2);
snakeText_.setPosition(Game::Width / 2, Game::Height / 4);
}
void MenuScreen::handleInput(sf::RenderWindow& window)
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Tab))
Game::Screen = std::make_shared<SettingScreen>();
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
sw.start();
Game::Screen = std::make_shared<GameScreen>();
}
}
void MenuScreen::update(sf::Time delta)
{
static bool movingLeft = false;
static bool movingRight = true;
if (movingRight)
{
snakeText_.rotate(delta.asSeconds());
if (static_cast<int>(snakeText_.getRotation()) == 10)
{
movingRight = false;
movingLeft = true;
}
}
if (movingLeft)
{
snakeText_.rotate(-delta.asSeconds());
if (static_cast<int>(snakeText_.getRotation()) == (360 - 10))
{
movingLeft = false;
movingRight = true;
}
}
}
void MenuScreen::render(sf::RenderWindow& window)
{
window.draw(text_);
window.draw(snakeText_);
}