Upload files to "/"
This commit is contained in:
parent
43739633b8
commit
3a493c1485
4 changed files with 246 additions and 17 deletions
143
game.cpp
143
game.cpp
|
|
@ -1,23 +1,52 @@
|
|||
#include <SFML/Graphics.hpp>
|
||||
#include "vokzal.hpp"
|
||||
#include "enemy.hpp"
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// Створюємо вікно 1950x1200
|
||||
sf::RenderWindow window(sf::VideoMode(1950, 1200), "Test Game Window");
|
||||
//
|
||||
// Завантаження фону
|
||||
sf::Texture backgroundTexture;
|
||||
if (!backgroundTexture.loadFromFile("img/background.png")) {
|
||||
cout << "ERROR: Cannot load background.png\n";
|
||||
}
|
||||
sf::Sprite backgroundSprite;
|
||||
backgroundSprite.setTexture(backgroundTexture);
|
||||
float scaleX = 1950.f / backgroundTexture.getSize().x;
|
||||
float scaleY = 1200.f / backgroundTexture.getSize().y;
|
||||
backgroundSprite.setScale(scaleX, scaleY);
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode(1950, 1200), "2D Platformer");
|
||||
window.setFramerateLimit(65);
|
||||
|
||||
// Платформи
|
||||
vector<Platform> platforms;
|
||||
platforms.push_back(Platform(299.f, 900.f, 200.f, 40.f));
|
||||
platforms.push_back(Platform(700.f, 725.f, 200.f, 40.f));
|
||||
platforms.push_back(Platform(1300.f, 700.f, 300.f, 50.f));
|
||||
platforms.push_back(Platform(400.f, 600.f, 250.f, 30.f));
|
||||
|
||||
platforms.push_back(Platform(700.f, 900.f, 400.f, 30.f));
|
||||
Platform ground(100.f, 1050.f, 300.f, 50.f);
|
||||
|
||||
window.setFramerateLimit(90); //вище 200 не рокимендую піднімати частоту після 600 буде чути писк дроселів
|
||||
|
||||
// Гравець
|
||||
Player player;
|
||||
|
||||
// Вороги
|
||||
vector<Enemy> enemies;
|
||||
for (auto& platform : platforms) {
|
||||
float left = platform.getBounds().left;
|
||||
float top = platform.getBounds().top;
|
||||
float width = platform.getBounds().width;
|
||||
enemies.push_back(Enemy(left + 10.f, top - 50.f, left, left + width));
|
||||
}
|
||||
|
||||
// Кулі
|
||||
vector<Bullet> bullets;
|
||||
sf::Clock fireClock;
|
||||
float fireCooldown = 0.3f;
|
||||
|
||||
sf::Clock gameClock;
|
||||
|
||||
while (window.isOpen()) {
|
||||
|
|
@ -29,22 +58,102 @@ int main() {
|
|||
|
||||
float deltaTime = gameClock.restart().asSeconds();
|
||||
|
||||
player.update(deltaTime, platforms); // логіка гравця
|
||||
// Рух гравця
|
||||
player.update(deltaTime, platforms);
|
||||
|
||||
window.clear(sf::Color::Black); // очистка вікна
|
||||
|
||||
// малюємо платформи
|
||||
for (auto& platform : platforms) {
|
||||
platform.draw(window);
|
||||
// Створення кулі при натиску пробілу
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space) &&
|
||||
fireClock.getElapsedTime().asSeconds() >= fireCooldown)
|
||||
{
|
||||
fireClock.restart();
|
||||
sf::Vector2f playerPos(
|
||||
player.getShape().getPosition().x + player.getShape().getSize().x / 2,
|
||||
player.getShape().getPosition().y + player.getShape().getSize().y / 2
|
||||
);
|
||||
bullets.push_back(Bullet(playerPos, player.isFacingRight()));
|
||||
}
|
||||
|
||||
// Оновлення і видалення куль
|
||||
for (auto it = bullets.begin(); it != bullets.end(); ) {
|
||||
it->update(deltaTime);
|
||||
if (it->isOffScreen() || it->collidesWithPlatform(platforms)) {
|
||||
it = bullets.erase(it);
|
||||
} else ++it;
|
||||
}
|
||||
|
||||
// Оновлення ворогів (агро на гравця)
|
||||
sf::Vector2f playerPosVec(player.getShape().getPosition().x, player.getShape().getPosition().y);
|
||||
for (auto& enemy : enemies) {
|
||||
float playerBottom = player.getShape().getPosition().y + player.getShape().getSize().y;
|
||||
float platformY = enemy.getPlatformY();
|
||||
float tolerance = 10.f;
|
||||
|
||||
// якщо гравець на тій же платформі
|
||||
if (playerBottom >= platformY - tolerance && playerBottom <= platformY + tolerance) {
|
||||
enemy.setAggro(true);
|
||||
} else {
|
||||
enemy.setAggro(false);
|
||||
}
|
||||
|
||||
enemy.update(deltaTime, playerPosVec);
|
||||
}
|
||||
|
||||
// Колізія куль з ворогами
|
||||
for (auto it = bullets.begin(); it != bullets.end(); ) {
|
||||
bool bulletRemoved = false;
|
||||
for (auto enemyIt = enemies.begin(); enemyIt != enemies.end(); ++enemyIt) {
|
||||
if (it->getBounds().intersects(enemyIt->getBounds())) {
|
||||
it = bullets.erase(it);
|
||||
enemies.erase(enemyIt);
|
||||
bulletRemoved = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!bulletRemoved) ++it;
|
||||
}
|
||||
|
||||
// Кінець гри, якщо всі вороги вбиті
|
||||
if (enemies.empty()) {
|
||||
cout << "Вітаємо! Ви перемогли всіх ворогів!\n";
|
||||
window.close();
|
||||
}
|
||||
|
||||
// Колізія ворогів з гравцем
|
||||
bool playerHit = false;
|
||||
for (auto& enemy : enemies) {
|
||||
if (enemy.getBounds().intersects(sf::FloatRect(player.getShape().getPosition(), player.getShape().getSize()))) {
|
||||
playerHit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Ресет гри
|
||||
if (playerHit) {
|
||||
player.setPosition(sf::Vector2f(50.f, 1040.f));
|
||||
player.setVelocityY(0.f);
|
||||
player.setOnGround(true);
|
||||
|
||||
enemies.clear();
|
||||
for (auto& platform : platforms) {
|
||||
float left = platform.getBounds().left;
|
||||
float top = platform.getBounds().top;
|
||||
float width = platform.getBounds().width;
|
||||
enemies.push_back(Enemy(left + 10.f, top - 50.f, left, left + width));
|
||||
}
|
||||
|
||||
bullets.clear();
|
||||
}
|
||||
|
||||
// Відмалювання
|
||||
window.clear(sf::Color::Black);
|
||||
window.draw(backgroundSprite);
|
||||
for (auto& platform : platforms) platform.draw(window);
|
||||
ground.draw(window);
|
||||
|
||||
// малюємо гравця
|
||||
player.draw(window);
|
||||
|
||||
window.display(); // відображення
|
||||
for (auto& enemy : enemies) enemy.draw(window);
|
||||
for (auto& bullet : bullets) bullet.draw(window);
|
||||
window.display();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue