Meteoryty lecą w trybie testowym.

This commit is contained in:
2024-11-19 14:42:25 +01:00
parent a69dc434a9
commit cf10a042c0
7 changed files with 112 additions and 21 deletions

View File

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

BIN
assets/img/meteor.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 KiB

View File

@@ -11,7 +11,7 @@ class Bullet {
};
public:
Bullet(float x, float y, sf::Texture &bulletTexture);
Bullet(float x, float y, sf::Texture &texture);
void update();
sf::Sprite& getSprite();
void setSpeed(float speed);

26
headers/Meteor.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef LOTOSTATEK_METEOR_H
#define LOTOSTATEK_METEOR_H
#include "SFML/Graphics/Texture.hpp"
#include "SFML/Graphics/Sprite.hpp"
class Meteor {
struct Position {
int x;
int y;
};
public:
Meteor(int x, int y, sf::Texture &texture);
sf::Sprite & getSprite();
bool getStatus();
void update();
private:
sf::Texture meteorTexture;
sf::Sprite meteorSprite;
Position position;
float meteorSpeed;
bool outOfBounds;
};
#endif //LOTOSTATEK_METEOR_H

View File

@@ -1,9 +1,9 @@
#include <iostream>
#include <chrono>
#include <random>
#include "SFML/Graphics.hpp"
#include "headers/Player.h"
#include "headers/Bullet.h"
#include "headers/Meteor.h"
int main()
{
@@ -12,6 +12,15 @@ int main()
window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
// Ustawienia randomizera
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 599);
// Koniec ustawień randomizera
sf::Texture meteorTexture;
meteorTexture.loadFromFile("../assets/img/meteor.png");
sf::Texture backgroundTexture;
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
@@ -20,6 +29,9 @@ int main()
ship.setMovingSpeed(8);
ship.setFirerate(200);
std::vector<Meteor> meteors;
std::srand(static_cast<unsigned int>(std::time(nullptr)));
while (window.isOpen()) {
window.clear();
@@ -33,16 +45,6 @@ int main()
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
ship.shoot();
}
}
if(event.type == sf::Event::MouseButtonPressed) {
if(event.mouseButton.button == sf::Mouse::Left)
ship.shoot();
else
ship.alternate_shoot();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
@@ -50,11 +52,6 @@ int main()
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) {
ship.moveUp();
@@ -65,6 +62,40 @@ int main()
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();
// TODO: Meteory na jednym poziomie ze statkiem
// TODO: Kolizje
// Generate a new meteor at a random position at the top of the screen
if (sf::Keyboard::isKeyPressed(sf::Keyboard::M)) {
int randomX = dis(gen);
meteors.emplace_back(randomX, -100, meteorTexture);
}
// Update and draw meteors
for (auto& meteor : meteors) {
meteor.update();
window.draw(meteor.getSprite());
}
// Remove meteors that are out of bounds
for (auto& meteor : meteors) {
if(meteor.getStatus()) {
meteors.erase(meteors.begin());
}
}
for (auto& bullet : ship.getBullets()) {
bullet.update();
@@ -73,6 +104,7 @@ int main()
ship.updateBullets();
window.draw(ship.getSprite());
window.display();
}

View File

@@ -1,9 +1,10 @@
#include <iostream>
#include "../headers/Bullet.h"
Bullet::Bullet(float x, float y, sf::Texture &bulletTexture) {
Bullet::Bullet(float x, float y, sf::Texture &texture) {
outOfBounds = false;
bulletSprite.setTexture(bulletTexture);
bulletTexture = texture;
bulletSprite.setTexture(texture);
bulletSprite.setPosition(x, y);
bulletSpeed = -10.0f;
bulletPosition.x = x;

30
sources/Meteor.cpp Normal file
View File

@@ -0,0 +1,30 @@
#include <iostream>
#include "../headers/Meteor.h"
Meteor::Meteor(int x, int y, sf::Texture &texture) {
position.x = x;
position.y = y;
outOfBounds = false;
meteorTexture = texture;
meteorSprite.setTexture(texture);
meteorSpeed = 10.0f;
meteorSprite.setPosition(x, y);
meteorSprite.scale(0.05f, 0.05f);
}
sf::Sprite &Meteor::getSprite() {
return meteorSprite;
}
void Meteor::update() {
meteorSprite.move(0.0f, meteorSpeed);
position.y += int(meteorSpeed);
if(position.y > 900) {
outOfBounds = true;
}
}
bool Meteor::getStatus() {
return outOfBounds;
}