Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////
- //Player.h///
- /////////////
- #pragma once
- #include "Libs.h"
- class Player
- {
- private:
- sf::RectangleShape player;
- sf::Clock clock;
- sf::Time passed;
- float increment;
- sf::Vector2f plMov;
- sf::Vector2f moving;
- void initVar();
- void initPly();
- public:
- Player();
- void movUpdate();
- sf::Vector2f plPos();
- void update();
- void render(sf::RenderTarget* target);
- };
- //////////////
- //Player.cpp//
- //////////////
- #include "Player.h"
- void Player::initVar()
- {
- this->increment = 500.f;
- }
- void Player::initPly()
- {
- this->player.setSize(sf::Vector2f(50, 50));
- this->player.setFillColor(sf::Color::Green);
- this->player.setPosition(375.f, 275.f);
- }
- Player::Player()
- {
- this->initVar();
- this->initPly();
- }
- void Player::movUpdate()
- {
- this->passed = this->clock.getElapsedTime();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
- {
- this->moving.y -= 1.f;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
- {
- this->moving.y += 1.f;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
- {
- this->moving.x -= 1.f;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
- {
- this->moving.x += 1.f;
- }
- this->plMov = this->moving * this->increment * this->passed.asSeconds();
- this->player.move(this->plMov);
- this->moving = sf::Vector2f(0.f, 0.f);
- this->clock.restart();
- }
- sf::Vector2f Player::plPos()
- {
- return this->player.getPosition();
- }
- void Player::update()
- {
- this->movUpdate();
- }
- void Player::render(sf::RenderTarget* target)
- {
- target->draw(this->player);
- }
- //////////////
- //Enemy.h/////
- //////////////
- #pragma once
- #include "Libs.h"
- #include "Player.h"
- class Enemy
- {
- private:
- sf::RectangleShape enemy;
- sf::Clock clock;
- sf::Time passed;
- Player player;
- float increment;
- sf::Vector2f enMov;
- sf::Vector2f moving;
- void initVar();
- void initEne();
- public:
- Enemy();
- void movUpdate();
- void update();
- void render(sf::RenderTarget* target);
- };
- //////////////
- //Enemy.cpp///
- //////////////
- #include "Enemy.h"
- void Enemy::initVar()
- {
- this->increment = 2.f;
- }
- void Enemy::initEne()
- {
- this->enemy.setSize(sf::Vector2f(50, 50));
- this->enemy.setFillColor(sf::Color::Red);
- }
- Enemy::Enemy()
- {
- this->initVar();
- this->initEne();
- }
- void Enemy::movUpdate()
- {
- this->passed = this->clock.getElapsedTime();
- if (this->player.plPos().x > this->enemy.getPosition().x)
- {
- this->moving.x += 1.f;
- }
- else if (this->player.plPos().x < this->enemy.getPosition().x)
- {
- this->moving.x -= 1.f;
- }
- if (this->player.plPos().y > this->enemy.getPosition().y)
- {
- this->moving.y += 1.f;
- }
- else if (this->player.plPos().y < this->enemy.getPosition().y)
- {
- this->moving.y -= 1.f;
- }
- this->enMov = this->moving * this->increment * this->passed.asSeconds();
- this->enemy.move(this->enMov);
- this->clock.restart();
- }
- void Enemy::update()
- {
- this->movUpdate();
- }
- void Enemy::render(sf::RenderTarget* target)
- {
- target->draw(this->enemy);
- }
- //////////////
- //Engine.h////
- //////////////
- #pragma once
- #include "Libs.h"
- #include "Player.h"
- #include "Enemy.h"
- class Engine
- {
- private:
- sf::RenderWindow* window;
- sf::VideoMode vMode;
- sf::Event event;
- Player player;
- Enemy enemy;
- void initVar();
- void initWin();
- public:
- Engine();
- virtual ~Engine();
- const bool running() const;
- void pollEvent();
- void update();
- void render();
- };
- //////////////
- //Engine.cpp//
- //////////////
- #include "Engine.h"
- void Engine::initVar()
- {
- this->window = nullptr;
- }
- void Engine::initWin()
- {
- this->vMode.height = 600;
- this->vMode.width = 800;
- this->window = new sf::RenderWindow(this->vMode, "Game");
- }
- Engine::Engine()
- {
- this->initVar();
- this->initWin();
- }
- Engine::~Engine()
- {
- delete this->window;
- }
- const bool Engine::running() const
- {
- return this->window->isOpen();
- }
- void Engine::pollEvent()
- {
- while (this->window->pollEvent(event))
- {
- switch (this->event.type)
- {
- case sf::Event::Closed:
- {
- this->window->close();
- }
- case sf::Event::KeyPressed:
- {
- if (this->event.key.code == sf::Keyboard::Escape)
- {
- this->window->close();
- }
- }
- }
- }
- }
- void Engine::update()
- {
- this->pollEvent();
- this->player.update();
- this->enemy.update();
- }
- void Engine::render()
- {
- this->window->clear();
- this->player.render(this->window);
- this->enemy.render(this->window);
- this->window->display();
- }
- //////////////
- //Main.cpp////
- //////////////
- #include "Libs.h"
- #include "Engine.h"
- int main()
- {
- Engine game;
- while (game.running())
- {
- game.update();
- game.render();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement