Upload files to "/"
This commit is contained in:
parent
c898dd8996
commit
9fe18da843
5 changed files with 84 additions and 26 deletions
37
game.cpp
37
game.cpp
|
@ -1,31 +1,50 @@
|
|||
#include <SFML/Graphics.hpp>
|
||||
#include "vokzal.hpp"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
// Створюємо вікно 800x600
|
||||
// Створюємо вікно 1950x1200
|
||||
sf::RenderWindow window(sf::VideoMode(1950, 1200), "Test Game Window");
|
||||
//
|
||||
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));
|
||||
|
||||
Platform ground(100.f, 1050.f, 300.f, 50.f);
|
||||
|
||||
window.setFramerateLimit(90); //вище 200 не рокимендую піднімати частоту після 600 буде чути писк дроселів
|
||||
window.setFramerateLimit(90); //вище 200 не рокимендую піднімати частоту після 600 буде чути писк дроселів
|
||||
|
||||
Player player;
|
||||
// Простий квадрат для гравця
|
||||
|
||||
sf::Clock clock;
|
||||
sf::Clock gameClock;
|
||||
|
||||
while (window.isOpen()) {
|
||||
sf::Event event;
|
||||
while (window.pollEvent(event)) {
|
||||
if (event.type == sf::Event::Closed)
|
||||
window.close();}
|
||||
window.close();
|
||||
}
|
||||
|
||||
//if(event.type== sf::Event::Resized){}
|
||||
float deltaTime = clock.restart().asSeconds();
|
||||
float deltaTime = gameClock.restart().asSeconds();
|
||||
|
||||
player.update(deltaTime, platforms); // логіка гравця
|
||||
|
||||
player.update(deltaTime); // викликаємо логіку гравця
|
||||
window.clear(sf::Color::Black); // очистка вікна
|
||||
player.draw(window); // малюємо квадрат
|
||||
|
||||
// малюємо платформи
|
||||
for (auto& platform : platforms) {
|
||||
platform.draw(window);
|
||||
}
|
||||
ground.draw(window);
|
||||
|
||||
// малюємо гравця
|
||||
player.draw(window);
|
||||
|
||||
window.display(); // відображення
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
15
platform.cpp
Normal file
15
platform.cpp
Normal file
|
@ -0,0 +1,15 @@
|
|||
|
||||
#include "platform.hpp"
|
||||
|
||||
Platform::Platform(float x , float y, float width, float height)
|
||||
:shape(sf::Vector2f(width, height))
|
||||
{
|
||||
shape.setPosition(x ,y);
|
||||
shape.setFillColor(sf::Color::Blue);
|
||||
}
|
||||
|
||||
void Platform::draw(sf::RenderWindow& window){
|
||||
window.draw(shape);
|
||||
}
|
||||
sf::FloatRect Platform::getBounds() const {
|
||||
return shape.getGlobalBounds();}
|
42
player.cpp
42
player.cpp
|
@ -1,30 +1,28 @@
|
|||
#include "player.hpp" // Включаємо заголовок класу Player, щоб компілятор знав про його методи та змінні.
|
||||
|
||||
using namespace std;
|
||||
// Конструктор класу Player. Він автоматично викликається при створенні об'єкта Player.
|
||||
|
||||
Player::Player()
|
||||
// Список ініціалізації: кращий спосіб задати початкові значення змінним.
|
||||
// Тут ініціалізуємо `shape` (прямокутник) з розміром 50x50.
|
||||
: shape(sf::Vector2f(50.f, 50.f)),
|
||||
// Ініціалізуємо змінну `speed` значенням 200.
|
||||
speed(500.f),
|
||||
: shape(sf::Vector2f(50.f, 50.f)), // тут лише "чисте" створення
|
||||
speed(500.f), //швидкість
|
||||
velocityY(0.f),
|
||||
jumpStrength(600.f),
|
||||
jumpStrength(700.f), // пиржок
|
||||
onGround(true),
|
||||
gravity(1500.f),
|
||||
groundLevel(1040.f)
|
||||
{
|
||||
// Тіло конструктора: тут можна задавати додаткові параметри.
|
||||
// Встановлюємо зелений колір для прямокутника.
|
||||
// А тут уже викликаємо методи, які налаштовують shape
|
||||
shape.setFillColor(sf::Color::Green);
|
||||
// Встановлюємо початкову позицію гравця на екрані.
|
||||
shape.setPosition(50.f, 1040.f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Закоментований метод `update` (робота на потім).
|
||||
// Він призначений для оновлення логіки гравця у кожному кадрі гри.
|
||||
// Усередині цього методу, ви будете обробляти рух, зіткнення, анімацію тощо.
|
||||
// `deltaTime` — це час, що минув з попереднього кадру, і він забезпечує плавний рух незалежно від швидкості комп'ютера.
|
||||
void Player::update(float deltaTime){ // Тут буде логіка руху гравця
|
||||
void Player::update(float deltaTime, const std::vector<Platform>& platforms) { // Тут буде логіка руху гравця
|
||||
float moveX = 0.f;
|
||||
|
||||
|
||||
|
@ -52,6 +50,28 @@ void Player::update(float deltaTime){ // Тут буде логіка руху
|
|||
}else{
|
||||
onGround = false;
|
||||
}
|
||||
/////////////////////////////////////////////////////
|
||||
|
||||
sf::FloatRect playerBounds = shape.getGlobalBounds();
|
||||
|
||||
for (const auto& platform : platforms) {
|
||||
sf::FloatRect platformBounds = platform.getBounds();
|
||||
if (playerBounds.intersects(platformBounds)){
|
||||
if (playerBounds.intersects(platformBounds)) {
|
||||
// Додатковий if — перевіряємо, чи гравець зверху
|
||||
if (playerBounds.top + playerBounds.height <= platformBounds.top + 20.f) {
|
||||
shape.setPosition(shape.getPosition().x, platformBounds.top - shape.getSize().y);
|
||||
velocityY = 0.f;
|
||||
onGround = true;
|
||||
}else if(playerBounds.top> platformBounds.top){shape.setPosition(shape.getPosition().x, platformBounds.top +platformBounds.height);
|
||||
velocityY =0.f;}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <vector>
|
||||
#include "platform.hpp"
|
||||
|
||||
|
||||
//float velocityY;
|
||||
//float gravity;
|
||||
|
@ -24,7 +26,7 @@ private:
|
|||
|
||||
public:
|
||||
Player(); // порожній конструктор
|
||||
void update(float deltaTime);
|
||||
void update(float deltaTime, const std::vector<Platform>& platforms);
|
||||
|
||||
void draw(sf::RenderWindow& window);
|
||||
|
||||
|
|
|
@ -4,3 +4,5 @@
|
|||
#include "enemy.hpp"
|
||||
#include "platform.hpp"
|
||||
#include "bullet.hpp"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue