Statek + Strzelanie (do naprawienia białe kwadraty)

This commit is contained in:
2024-11-15 14:22:52 +01:00
parent 8ecd82eb4e
commit ce2924e635
12 changed files with 203 additions and 25 deletions

View File

@@ -1,51 +1,62 @@
#include <iostream>
#include "SFML/Graphics.hpp"
#include "headers/Actor.h"
#include "headers/Player.h"
#include "headers/Bullet.h"
int main()
{
std::cout << "Game started\n";
std::clog << "Game started\n";
sf::RenderWindow window(sf::VideoMode(600, 800), "My window");
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
sf::RectangleShape rectangle(sf::Vector2f(120.0f, 50.0f));
float sizeIncrement = 1.0f;
sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("../assets/img/background.jpg"); // wczytywanie tła
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
while (window.isOpen()) {
window.clear();
if(rectangle.getSize().x > 5 && rectangle.getSize().y > 5 && rectangle.getSize().x < 300 && rectangle.getSize().y < 300) {
rectangle.setSize(rectangle.getSize() + sf::Vector2f(sizeIncrement, sizeIncrement));
rectangle.setPosition(
static_cast<float>(window.getSize().x) / 2 - rectangle.getSize().x / 2,
static_cast<float>(window.getSize().y) / 2 - rectangle.getSize().y / 2
);
} else {
rectangle.setSize(sf::Vector2f(120.0f, 50.0f));
rectangle.setPosition(
static_cast<float>(window.getSize().x) / 2 - rectangle.getSize().x / 2,
static_cast<float>(window.getSize().y) / 2 - rectangle.getSize().y / 2
);
}
window.draw(backgroundSprite); // narysuj tło
// Tu są handlowane eventy
sf::Event event{};
while (window.pollEvent(event)) {
if(event.type == sf::Event::MouseWheelScrolled) {
if(event.mouseWheelScroll.delta > 0) {
sizeIncrement = 1.0f;
} else {
sizeIncrement = -1.0f;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
if(ship.getPosition().x > -10) {
ship.moveLeft();
}
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
if(ship.getPosition().x < 480) {
ship.moveRight();
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
ship.shoot();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
if (event.type == sf::Event::Closed)
window.close();
}
window.draw(rectangle);
for (auto& bullet : ship.getBullets()) {
bullet.update();
window.draw(bullet.getSprite());
}
ship.updateBullets();
window.draw(ship.getSprite());
window.display();
}