This repository has been archived on 2025-06-06. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
LotoStatek/main.cpp

90 lines
2.5 KiB
C++

#include <iostream>
#include "SFML/Graphics.hpp"
#include "headers/Player.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);
Plansza plansza(window.getSize().y, window.getSize().x);
sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
ship.setMovingSpeed(8);
ship.setFirerate(200);
while (window.isOpen()) {
window.clear();
window.draw(backgroundSprite); // narysuj tło
// Tu są handlowane eventy
sf::Event event{};
while (window.pollEvent(event)) {
if(event.type == sf::Event::Closed)
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
if(ship.getPosition().x > -10) {
ship.moveLeft();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
if(ship.getPosition().y > 0) {
ship.moveUp();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
if(ship.getPosition().y < 700) {
ship.moveDown();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
if(ship.getPosition().x < 480) {
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();
window.draw(meteor.getSprite());
}
for (auto& bullet : ship.getBullets()) {
bullet.update();
window.draw(bullet.getSprite());
}
plansza.update_meteors();
ship.updateBullets();
window.draw(ship.getSprite());
window.display();
}
return 0;
}