Merge remote-tracking branch 'refs/remotes/origin/przeszkoda'
# Conflicts: # CMakeLists.txt # main.cpp
This commit is contained in:
118
main.cpp
118
main.cpp
@@ -1,18 +1,20 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
#include "SFML/Graphics.hpp"
|
||||
#include "headers/Player.h"
|
||||
#include "headers/Bullet.h"
|
||||
#include "headers/Background.h"
|
||||
#include "headers/AudioManager.h"
|
||||
#include "headers/Meteor.h"
|
||||
#include "headers/Plansza.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
std::clog << "Game started\n";
|
||||
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
|
||||
window.setVerticalSyncEnabled(true);
|
||||
window.setFramerateLimit(60);
|
||||
sf::RenderWindow mainWindow(sf::VideoMode(600, 800), "My mainWindow");
|
||||
mainWindow.setVerticalSyncEnabled(true);
|
||||
mainWindow.setFramerateLimit(60);
|
||||
Plansza plansza(mainWindow.getSize().y, mainWindow.getSize().x);
|
||||
|
||||
Background background("../assets/img/space.png", 2.0f); //tutaj predkosc tla, mozna zwiekszyc jak za wolno
|
||||
|
||||
@@ -27,20 +29,22 @@ int main()
|
||||
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
|
||||
|
||||
|
||||
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||
// TODO: Przenieść tworzenie statku wewnątrz klasy Plansza
|
||||
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()) {
|
||||
window.clear();
|
||||
while (mainWindow.isOpen()) {
|
||||
// std::cout << "Liczba: " << RandomNumberGenerator::getRandomNumber(0,499) << std::endl;
|
||||
mainWindow.clear();
|
||||
|
||||
// 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::Space)) {
|
||||
ship.shoot();
|
||||
@@ -60,43 +64,105 @@ int main()
|
||||
|
||||
|
||||
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::D)) {
|
||||
if(ship.getPosition().x < 480) {
|
||||
ship.moveRight();
|
||||
}
|
||||
}
|
||||
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 < 550) {
|
||||
ship.moveRight();
|
||||
}
|
||||
}
|
||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
||||
ship.shoot();
|
||||
}
|
||||
|
||||
|
||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) ship.shoot();
|
||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) ship.alternate_shoot();
|
||||
|
||||
// Generate a new meteor at a random position at the top of the screen
|
||||
plansza.spawn_meteor();
|
||||
|
||||
|
||||
// Update and draw meteors
|
||||
for (auto& meteor : plansza.getMeteors()) {
|
||||
meteor.update();
|
||||
mainWindow.draw(meteor.getSprite());
|
||||
}
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
window.draw(bullet.getSprite());
|
||||
mainWindow.draw(bullet.getSprite());
|
||||
}
|
||||
|
||||
// tło
|
||||
background.update();
|
||||
background.draw(window);
|
||||
background.draw(mainWindow);
|
||||
|
||||
for (auto& bullet : ship.getBullets()) {
|
||||
bullet.update();
|
||||
window.draw(bullet.getSprite());
|
||||
|
||||
plansza.update_meteors();
|
||||
ship.updateBullets();
|
||||
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";
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ship.updateBullets();
|
||||
window.draw(ship.getSprite());
|
||||
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