-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojeto-yuno.cpp
More file actions
213 lines (187 loc) · 7.62 KB
/
projeto-yuno.cpp
File metadata and controls
213 lines (187 loc) · 7.62 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
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <cmath>
#include <sstream>
// inimigos e projeteis
struct Projetil {
sf::RectangleShape shape;
sf::Vector2f direction;
float speed;
};
struct Inimigo {
sf::CircleShape shape;
float speed;
};
int main() {
srand(time(0));
sf::RenderWindow window(sf::VideoMode(800, 600), "Piu Piu Bang");
window.setFramerateLimit(60);
bool jogoIniciado = false;
sf::CircleShape bolaInicio(10);
bolaInicio.setFillColor(sf::Color::White);
sf::Vector2f bolaVelocidade(3, 3);
bolaInicio.setPosition(400, 300);
sf::Font font;
font.loadFromFile("arial.ttf");
sf::Text startText("Pressione Enter para Iniciar", font, 24);
startText.setFillColor(sf::Color::White);
startText.setPosition(250, 280);
// boneco
sf::CircleShape boneco(20);
boneco.setFillColor(sf::Color::Blue);
boneco.setPosition(400, 300);
// arma
sf::RectangleShape arma(sf::Vector2f(30, 5));
arma.setFillColor(sf::Color::Black);
arma.setOrigin(0, 2.5f);
std::vector<Projetil> projeteis;
std::vector<Inimigo> inimigos;
sf::Clock inimigoTimer;
float inimigoSpeed = 2.0f;
bool vivo = true;
int score = 0;
sf::Text scoreText;
scoreText.setFont(font);
scoreText.setCharacterSize(24);
scoreText.setFillColor(sf::Color::White);
scoreText.setPosition(10, 10);
// som de tiro
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("tiro.wav")) {
return -1; // Erro ao carregar
}
sf::Sound sound;
sound.setBuffer(buffer);
// som de vitória
sf::SoundBuffer victoryBuffer;
if (!victoryBuffer.loadFromFile("vitoria.wav")) {
return -1; // Erro ao carregar
}
sf::Sound victorySound;
victorySound.setBuffer(victoryBuffer);
// som ao matar inimigo
sf::SoundBuffer killBuffer;
if (!killBuffer.loadFromFile("mato.wav")) {
return -1; // Erro ao carregar
}
sf::Sound killSound;
killSound.setBuffer(killBuffer);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (!jogoIniciado && event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter)
jogoIniciado = true;
if (!vivo && event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Enter) {
vivo = true;
score = 0;
inimigoSpeed = 2.0f;
projeteis.clear();
inimigos.clear();
boneco.setPosition(390, 280);
}
}
window.clear();
if (!jogoIniciado) {
bolaInicio.move(bolaVelocidade);
if (bolaInicio.getPosition().x <= 0 || bolaInicio.getPosition().x >= 790)
bolaVelocidade.x *= -1;
if (bolaInicio.getPosition().y <= 0 || bolaInicio.getPosition().y >= 590)
bolaVelocidade.y *= -1;
window.draw(startText);
window.draw(bolaInicio);
} else {
if (vivo) {
sf::Vector2f mousePos = sf::Vector2f(sf::Mouse::getPosition(window));
sf::Vector2f center = boneco.getPosition() + sf::Vector2f(20, 20);
sf::Vector2f direction = mousePos - center;
float angle = atan2(direction.y, direction.x) * 180 / 3.14159f;
arma.setPosition(center);
arma.setRotation(angle);
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
Projetil p;
p.shape.setSize(sf::Vector2f(10, 4));
p.shape.setFillColor(sf::Color(rand() % 256, rand() % 256, rand() % 256));
p.shape.setPosition(center);
p.direction = direction / sqrt(direction.x * direction.x + direction.y * direction.y);
p.speed = 6.0f;
projeteis.push_back(p);
sound.play(); // Tocar som de tiro
}
if (inimigoTimer.getElapsedTime().asSeconds() > 1.0f) {
Inimigo i;
i.shape.setRadius(15);
i.speed = inimigoSpeed;
i.shape.setPosition(rand() % 800, rand() % 600);
if (i.speed < 3.0f)
i.shape.setFillColor(sf::Color::Green);
else if (i.speed < 5.0f)
i.shape.setFillColor(sf::Color::Yellow);
else
i.shape.setFillColor(sf::Color::Red);
inimigos.push_back(i);
inimigoTimer.restart();
}
for (size_t i = 0; i < projeteis.size(); i++) {
projeteis[i].shape.move(projeteis[i].direction * projeteis[i].speed);
if (projeteis[i].shape.getPosition().x < 0 || projeteis[i].shape.getPosition().x > 800 ||
projeteis[i].shape.getPosition().y < 0 || projeteis[i].shape.getPosition().y > 600) {
projeteis.erase(projeteis.begin() + i);
}
}
for (size_t i = 0; i < inimigos.size(); i++) {
sf::Vector2f dir = center - inimigos[i].shape.getPosition();
float length = sqrt(dir.x * dir.x + dir.y * dir.y);
dir /= length;
inimigos[i].shape.move(dir * inimigos[i].speed);
if (inimigos[i].shape.getGlobalBounds().intersects(boneco.getGlobalBounds())) {
vivo = false;
if (score > 100) {
victorySound.play();
}
}
}
for (size_t i = 0; i < projeteis.size(); i++) {
for (size_t j = 0; j < inimigos.size(); j++) {
if (projeteis[i].shape.getGlobalBounds().intersects(inimigos[j].shape.getGlobalBounds())) {
projeteis.erase(projeteis.begin() + i);
inimigos.erase(inimigos.begin() + j);
score++;
inimigoSpeed += 0.1f;
killSound.play();
break;
}
}
}
std::stringstream ss;
ss << "Score: " << score;
scoreText.setString(ss.str());
window.draw(boneco);
window.draw(arma);
for (auto& p : projeteis)
window.draw(p.shape);
for (auto& i : inimigos)
window.draw(i.shape);
window.draw(scoreText);
} else if (score == 69) {
sf::Text text("Criado por YunoDev, projeto de faculdade", font, 24);
text.setFillColor(sf::Color::White);
text.setPosition(150, 280);
window.draw(text);
} else {
sf::Text gameOverText("Game Over! Pressione Enter para reiniciar", font, 24);
gameOverText.setFillColor(sf::Color::White);
gameOverText.setPosition(200, 280);
window.draw(gameOverText);
}
}
window.display();
}
return 0;
}