-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fruit.cpp
86 lines (73 loc) · 1.84 KB
/
Fruit.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
#include <algorithm>
#include "Fruit.hpp"
#include "State.hpp"
#include "Snake.hpp"
#include "Graphics.hpp"
Fruit::Fruit(int fruantity):
fruantity(fruantity)
{
PushBack(fruantity);
for(int i = 0; i < NO_ICONSETS; ++i)
fruitsGallery[i].load(FOLDER + "textures/" + std::to_string(i) + "_FRUIT.tga");
}
void Fruit::DeleteFruit(const Ball &fruit) {
objects.erase(
std::remove(
objects.begin(),
objects.end(),
fruit
),
objects.end()
);
}
void Fruit::PushBack(int new_fruantity) {
for(int i = 0; i < new_fruantity; ++i) {
if(objects.size() >= WIDTH * HEIGHT - SNAKE->GetObjects().size() - (WALLS->GetObjects().size() >> 1) + 3)
return;
Ball new_fruit;
do {
new_fruit = Ball(
rand() % (int(WIDTH) - 1) + 1,
rand() % (int(HEIGHT) - 1) + 1
);
} while (
Ball::InSegment(new_fruit, WALLS->GetObjects())
|| Ball::InSegment(new_fruit, SNAKE->GetObjects())
|| Ball::InSegment(new_fruit, GetObjects())
);
objects.push_back(new_fruit);
}
}
void Fruit::EatFruit() {
DeleteFruit(SNAKE->GetObjects().front());
SSNAKE->PushBack();
PushBack(1);
++frufru;
}
void Fruit::Keyboard(char key) {
switch(key) {
case 'm' :
++(fru_delta = (fru_delta < 0) ? 0 : fru_delta);
PushBack((fru_delta >> 2) + 1);
break;
case 'n' :
--(fru_delta = (fru_delta > 0) ? 0 : fru_delta);
for(int i = 0; i < (abs(fru_delta) >> 2) + 1; ++i) {
DeleteFruit(objects.front());
}
break;
}
}
void Fruit::Display() {
char text[20];
glColor3f(0.3f, 1.0f, 0.0f);
sprintf(text, "Points: %d..", frufru);
Graphics::DisplayText(WIDTH + 1, HEIGHT * 0.97, text);
glColor3f(1.0f, 0.0f, 0.0f);
sprintf(text, "Fruits: %lu", objects.size());
Graphics::DisplayText(WIDTH + 1, HEIGHT * 0.93, text);
for(const auto &fruit : objects) {
double degree = 90;
Graphics::DisplayObject(fruit, fruitsGallery[State::skin_id].id, degree);
}
}