forked from jakehorsfield/sfSnake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenuScreen.cpp
More file actions
76 lines (62 loc) · 1.73 KB
/
MenuScreen.cpp
File metadata and controls
76 lines (62 loc) · 1.73 KB
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
#include <SFML/Graphics.hpp>
#include <memory>
#include "GameScreen.h"
#include "MenuScreen.h"
#include "Game.h"
using namespace sfSnake;
MenuScreen::MenuScreen()
{
font_.loadFromFile("Fonts/game_over.ttf");
text_.setFont(font_);
text_.setString(
"\n\n\n\n\n\n\n\n\nPress [SPACE] to play"
"\n\nPress [ESC] to quit");
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::Space))
Game::Screen = std::make_shared<GameScreen>();
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
}
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_);
}