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

@@ -7,7 +7,11 @@ add_executable(LotoStatek main.cpp
headers/Actor.h
headers/Plansza.h
sources/Actor.cpp
sources/Plansza.cpp)
sources/Plansza.cpp
sources/Player.cpp
headers/Player.h
headers/Bullet.h
sources/Bullet.cpp)
if(WIN32)
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")

BIN
assets/img/background.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 98 KiB

BIN
assets/img/bullet.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 773 B

View File

@@ -1,9 +1,52 @@
#ifndef ACTOR_H
#define ACTOR_H
#include "SFML/Graphics/Sprite.hpp"
#include "SFML/Graphics/Texture.hpp"
#include "Bullet.h"
struct Position {
int x;
int y;
};
class Actor {
public:
Actor(int x, int y, std::string path);
void loadTexture(std::string path);
sf::Sprite& getSprite();
Position getPosition();
void move(float deltaX, float deltaY);
void moveLeft();
void moveRight();
void shoot();
std::vector<Bullet>& getBullets();
void updateBullets() {
for (auto& bullet : bullets) {
if(bullet.getStatus()) {
bullets.erase(bullets.begin());
}
}
}
protected:
sf::Sprite actorSprite;
sf::Texture actorTexture;
Position position;
unsigned int hp;
unsigned int damage;
unsigned int firerate;
float moving_speed;
std::vector<Bullet> bullets;
};
#endif //ACTOR_H

30
headers/Bullet.h Normal file
View File

@@ -0,0 +1,30 @@
#ifndef LOTOSTATEK_BULLET_H
#define LOTOSTATEK_BULLET_H
#include "SFML/Graphics/Sprite.hpp"
#include "SFML/Graphics/Texture.hpp"
class Bullet {
struct Position {
int x;
int y;
};
public:
Bullet(float x, float y);
void update();
sf::Sprite& getSprite();
void setSpeed(float speed);
bool getStatus() const {
return outOfBounds;
}
private:
sf::Sprite bulletSprite;
sf::Texture bulletTexture;
float bulletSpeed;
Position bulletPosition;
bool outOfBounds;
};
#endif //LOTOSTATEK_BULLET_H

15
headers/Player.h Normal file
View File

@@ -0,0 +1,15 @@
#ifndef LOTOSTATEK_PLAYER_H
#define LOTOSTATEK_PLAYER_H
#include "Actor.h"
class Player : public Actor {
public:
Player(int x, int y, std::string path);
private:
};
#endif //LOTOSTATEK_PLAYER_H

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();
}

View File

@@ -1 +1,44 @@
#include "../headers/actor.h"
#include "../headers/Actor.h"
Actor::Actor(int x, int y, std::string path) {
loadTexture(path);
position.x = x;
position.y = y;
actorSprite.setPosition(x, y);
}
void Actor::loadTexture(std::string path) {
actorTexture.loadFromFile(path);
actorSprite.setTexture(actorTexture);
}
sf::Sprite &Actor::getSprite() {
return actorSprite;
}
void Actor::move(float deltaX, float deltaY) {
actorSprite.move(deltaX, deltaY);
}
void Actor::moveLeft() {
move(-10.0f, 0.0f);
position.x -= 10;
}
void Actor::moveRight() {
move(10.0f, 0.0f);
position.x += 10;
}
Position Actor::getPosition() {
return {position.x, position.y};
}
void Actor::shoot() {
bullets.emplace_back(position.x + actorSprite.getGlobalBounds().width / 2-62, position.y);
}
std::vector<Bullet> &Actor::getBullets() {
return bullets;
}

29
sources/Bullet.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <iostream>
#include "../headers/Bullet.h"
Bullet::Bullet(float x, float y) {
outOfBounds = false;
bulletTexture.loadFromFile("../assets/img/bullet.png");
bulletSprite.setTexture(bulletTexture);
bulletSprite.setPosition(x, y);
bulletSpeed = -10.0f;
bulletPosition.x = x;
bulletPosition.y = y;
}
void Bullet::setSpeed(float speed) {
bulletSpeed = speed;
}
sf::Sprite &Bullet::getSprite() {
return bulletSprite;
}
void Bullet::update() {
bulletSprite.move(0.0f, bulletSpeed);
bulletPosition.y += bulletSpeed;
if(bulletPosition.y < -100) {
outOfBounds = true;
std::cout << "Bullet out of bounds\n";
}
}

3
sources/Player.cpp Normal file
View File

@@ -0,0 +1,3 @@
#include "../headers/Player.h"
Player::Player(int x, int y, std::string path) : Actor(x, y, path) {};