W pełnie kompletna wersja przeszkód
This commit is contained in:
88
main.cpp
88
main.cpp
@@ -8,53 +8,53 @@
|
||||
int main()
|
||||
{
|
||||
std::clog << "Game started\n";
|
||||
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
window.setFramerateLimit(60);
|
||||
Plansza plansza(window.getSize().y, window.getSize().x);
|
||||
sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "My mainWindow");
|
||||
mainWindow.setVerticalSyncEnabled(true);
|
||||
mainWindow.setFramerateLimit(60);
|
||||
Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x);
|
||||
|
||||
sf::Texture backgroundTexture;
|
||||
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
||||
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
|
||||
|
||||
// TODO: Przenieść tworzenie statku wewnątrz klasy Plansza
|
||||
Player ship(window.getSize().x/2,window.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||
Player ship(mainWindow.getSize().x / 2, mainWindow.getSize().y - 100, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||
ship.setMovingSpeed(8);
|
||||
ship.setFirerate(200);
|
||||
|
||||
while (window.isOpen()) {
|
||||
while (mainWindow.isOpen()) {
|
||||
// std::cout << "Liczba: " << RandomNumberGenerator::getRandomNumber(0,499) << std::endl;
|
||||
window.clear();
|
||||
mainWindow.clear();
|
||||
|
||||
window.draw(backgroundSprite); // narysuj tło
|
||||
mainWindow.draw(backgroundSprite); // narysuj tło
|
||||
|
||||
// Tu są handlowane eventy
|
||||
sf::Event event{};
|
||||
while (window.pollEvent(event)) {
|
||||
while (mainWindow.pollEvent(event)) {
|
||||
if(event.type == sf::Event::Closed)
|
||||
window.close();
|
||||
mainWindow.close();
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||
window.close();
|
||||
mainWindow.close();
|
||||
}
|
||||
}
|
||||
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||
if(ship.getPosition().x > -10) {
|
||||
if(ship.getPosition().x > 50) {
|
||||
ship.moveLeft();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
|
||||
if(ship.getPosition().y > 0) {
|
||||
if(ship.getPosition().y > 80) {
|
||||
ship.moveUp();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
|
||||
if(ship.getPosition().y < 700) {
|
||||
if(ship.getPosition().y < 720) {
|
||||
ship.moveDown();
|
||||
}
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
|
||||
if(ship.getPosition().x < 480) {
|
||||
if(ship.getPosition().x < 550) {
|
||||
ship.moveRight();
|
||||
}
|
||||
}
|
||||
@@ -73,37 +73,67 @@ int main()
|
||||
// Update and draw meteors
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
meteor.update();
|
||||
window.draw(meteor.getSprite());
|
||||
mainWindow.draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
window.draw(bullet.getSprite());
|
||||
mainWindow.draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
plansza.update_meteors();
|
||||
ship.updateBullets();
|
||||
window.draw(ship.getSprite());
|
||||
mainWindow.draw(ship.getSprite());
|
||||
|
||||
// trochę dziwny sposób ale jednak działa
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
if(ship.getSprite().getGlobalBounds().intersects(meteor.getSprite().getGlobalBounds())) {
|
||||
std::cout << "You lost the game!\n";
|
||||
// window.close();
|
||||
// exit(-2); // Kod -2 oznacza uderzenie się w meteoryt
|
||||
sf::RenderWindow errorWindow(sf::VideoMode(350, 200), "The end");
|
||||
sf::Font font;
|
||||
if (!font.loadFromFile("../assets/fonts/arial.ttf")) {
|
||||
std::cerr << "Error loading font\n";
|
||||
return -1;
|
||||
}
|
||||
sf::Text text("Your ship is destroyed!", font, 24);
|
||||
text.setFillColor(sf::Color::Red);
|
||||
text.setPosition(50, 80);
|
||||
|
||||
while (errorWindow.isOpen()) {
|
||||
sf::Event event;
|
||||
while (errorWindow.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
|
||||
errorWindow.close();
|
||||
mainWindow.close();
|
||||
exit(-2);
|
||||
}
|
||||
}
|
||||
errorWindow.clear();
|
||||
errorWindow.draw(text);
|
||||
errorWindow.display();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Poprawić kolizję pocisku z meteorem, tak aby był kasowany tylko ten konkretny meteoryt
|
||||
// for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ++meteorIt) {
|
||||
// for (auto& bullet : ship.getBullets()) {
|
||||
// if (meteorIt->getSprite().getGlobalBounds().intersects(bullet.getSprite().getGlobalBounds())) {
|
||||
// meteorIt = plansza.getMeteors().erase(meteorIt);
|
||||
// break; // Exit the inner loop to avoid invalidating the iterator
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
window.display();
|
||||
for (auto meteorIt = plansza.getMeteors().begin(); meteorIt != plansza.getMeteors().end(); ) {
|
||||
bool meteorHit = false;
|
||||
for (auto bulletIt = ship.getBullets().begin(); bulletIt != ship.getBullets().end(); ) {
|
||||
if (meteorIt->getSprite().getGlobalBounds().intersects(bulletIt->getSprite().getGlobalBounds())) {
|
||||
bulletIt = ship.getBullets().erase(bulletIt);
|
||||
meteorIt = plansza.getMeteors().erase(meteorIt);
|
||||
meteorHit = true;
|
||||
break; // Exit the inner loop to avoid invalidating the iterator
|
||||
} else {
|
||||
++bulletIt;
|
||||
}
|
||||
}
|
||||
if (!meteorHit) {
|
||||
++meteorIt;
|
||||
}
|
||||
}
|
||||
|
||||
mainWindow.display();
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user