-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game.cpp
192 lines (165 loc) · 5.1 KB
/
Game.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include "Game.hpp"
#include "Core.hpp"
#include <iostream>
#include <cstring>
#include <random>
namespace rg {
Wall::Wall(BaseData data) {
this->m_outgame_size = data.outgame_size;
this->m_ingame_width = data.ingame_width;
this->m_ingame_height = data.ingame_height;
this->m_snake_size = data.snake_size;
this->m_color = sf::Color::White;
}
void Wall::setColor(sf::Color new_color) {
m_color = new_color;
}
void Wall::draw(sf::RenderWindow& window) {
int col = m_ingame_width / m_snake_size;
int row = m_ingame_height / m_snake_size;
for (int i : {0, col}) {
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(m_outgame_size + m_snake_size * i, m_outgame_size), m_color),
sf::Vertex(sf::Vector2f(m_outgame_size + m_snake_size * i, m_outgame_size + m_ingame_height), m_color)
};
window.draw(line, 2, sf::Lines);
}
for (int i : {0, row}) {
sf::Vertex line[] =
{
sf::Vertex(sf::Vector2f(m_outgame_size, m_outgame_size + m_snake_size * i), m_color),
sf::Vertex(sf::Vector2f(m_outgame_size + m_ingame_width, m_outgame_size + m_snake_size * i), m_color)
};
window.draw(line, 2, sf::Lines);
}
}
Background::Background(BaseData data) {
int width = data.outgame_size * 2 + data.ingame_width, height = data.outgame_size * 2 + data.ingame_height;
grap.setSize(sf::Vector2f(width, height));
grap.setFillColor(Global::settings.getNewColor());//sf::Color::Color(178, 255, 102)
}
void Background::setColor(sf::Color new_color) {
grap.setFillColor(new_color);
}
sf::Color Background::getColor() {
return grap.getFillColor();
}
void Background::FadeBlack(int _color) {
sf::Color now_color = this->getColor();
int color[] = { now_color.r, now_color.g , now_color.b };
int max = 0;
for (int i = 0; i < 3; i++) {
if ((color[i] -= 2) < 0)
color[i] = 0;
if (color[i] > max)
max = color[i];
}
if(max + 10 > _color)
this->setColor(sf::Color(color[0], color[1], color[2]));
}
void Background::draw(sf::RenderWindow& window) {
window.draw(grap);
}
Game::Game(sf::RenderWindow& _window, renderManager& render, BaseData data, float game_speed) {
this->window = &_window;
this->m_renderManager = &render;
this->m_outgame_size = data.outgame_size;
this->m_ingame_width = data.ingame_width;
this->m_ingame_height = data.ingame_height;
this->m_snake_size = data.snake_size;
this->m_game_speed = game_speed;
int width = m_outgame_size * 2 + m_ingame_width, height = m_outgame_size * 2 + m_ingame_height;
this->window->setSize(sf::Vector2u(width, height));
this->window->setView(sf::View(sf::FloatRect(0, 0, width, height)));
this->window->setTitle("Snake Game");
this->m_game_snake = new Snake(data);
this->m_game_food = new Food(data);
this->m_wall = new Wall(data);
this->m_background = new Background(data);
this->m_renderManager->clearAllGraphics();
this->m_renderManager->addGraphics(this->m_background);
this->m_renderManager->addGraphics(this->m_wall);
this->m_renderManager->addGraphics(this->m_game_food);
this->m_renderManager->addGraphics(this->m_game_snake);
this->m_renderManager->setSanke(m_game_snake);
this->A1 = new AnimationSnake(_window, render, m_game_snake);
this->A2 = new AnimationFade(_window, render, m_wall, m_game_food, m_background);
Global::G_changeGMode(GMode::Gaming);
}
void Game::handleEvent() {
sf::Event e;
while (window->pollEvent(e)) {
switch (e.type)
{
case sf::Event::Closed:
window->close();
return;
case sf::Event::KeyPressed:
if (e.key.code == sf::Keyboard::P)//invisable
pause = !pause;
if (Global::G_getMode() == GMode::Gaming)
m_game_snake->detectWayKeys(e.key.code);
break;
}
}
}
void Game::display() {
this->handleEvent();
switch (Global::G_getMode()) {
case GMode::Gaming:
displayGaming();
break;
case GMode::A1:
displayA1();
break;
case GMode::A2:
displayA2();
break;
}
}
void Game::displayGaming() {
if (!pause) {
this->gameTime += gameclock.restart().asSeconds();
if (this->gameTime > this->move_per_time)
{
this->gameTime = 0.f;
if (!m_game_snake->move()) {
Global::G_changeGMode(GMode::A1);
return;
}
if (this->isInFood()) {
this->AteFood();
this->move_per_time -= m_game_speed;
}
}
m_renderManager->Render();
}
else
gameclock.restart();
}
void Game::displayA1() {
A1->display();
}
void Game::displayA2() {
A2->display();
}
bool Game::isInFood() {
return (m_game_snake->getHeadX() == m_game_food->getX() && m_game_snake->getHeadY() == m_game_food->getY());
}
void Game::AteFood() {
score++;
this->genFood();
m_game_snake->gainFood();
if(score % Global::settings.getCCperFood() == 0)
m_background->setColor(Global::settings.getNewColor());
}
void Game::genFood() {
do {
m_game_food->generateNewPosition();
} while (m_game_snake->posInBody(m_game_food->getX(), m_game_food->getY()));
}
int Game::getScore() {
return this->score;
}
}