Advertisement
ecifef00

attempt

Feb 17th, 2024 (edited)
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | Software | 0 0
  1. /////////////
  2. //Player.h///
  3. /////////////
  4.  
  5. #pragma once
  6.  
  7. #include "Libs.h"
  8.  
  9. class Player
  10. {
  11. private:
  12.     sf::RectangleShape player;
  13.  
  14.     sf::Clock clock;
  15.     sf::Time passed;
  16.  
  17.     float increment;
  18.     sf::Vector2f plMov;
  19.     sf::Vector2f moving;
  20.  
  21.     void initVar();
  22.     void initPly();
  23.  
  24. public:
  25.  
  26.     Player();
  27.  
  28.     void movUpdate();
  29.  
  30.     sf::Vector2f plPos();
  31.  
  32.     void update();
  33.     void render(sf::RenderTarget* target);
  34. };
  35.  
  36.  
  37.  
  38. //////////////
  39. //Player.cpp//
  40. //////////////
  41.  
  42.  
  43. #include "Player.h"
  44.  
  45. void Player::initVar()
  46. {
  47.     this->increment = 500.f;
  48. }
  49.  
  50. void Player::initPly()
  51. {
  52.     this->player.setSize(sf::Vector2f(50, 50));
  53.     this->player.setFillColor(sf::Color::Green);
  54.     this->player.setPosition(375.f, 275.f);
  55. }
  56.  
  57. Player::Player()
  58. {
  59.     this->initVar();
  60.     this->initPly();
  61. }
  62.  
  63. void Player::movUpdate()
  64. {
  65.     this->passed = this->clock.getElapsedTime();
  66.  
  67.  
  68.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
  69.     {
  70.         this->moving.y -= 1.f;
  71.     }
  72.  
  73.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
  74.     {
  75.         this->moving.y += 1.f;
  76.     }
  77.  
  78.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
  79.     {
  80.         this->moving.x -= 1.f;
  81.     }
  82.  
  83.     if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
  84.     {
  85.         this->moving.x += 1.f;
  86.     }
  87.  
  88.  
  89.  
  90.     this->plMov = this->moving * this->increment * this->passed.asSeconds();
  91.  
  92.     this->player.move(this->plMov);
  93.  
  94.     this->moving = sf::Vector2f(0.f, 0.f);
  95.  
  96.     this->clock.restart();
  97. }
  98.  
  99. sf::Vector2f Player::plPos()
  100. {
  101.     return this->player.getPosition();
  102. }
  103.  
  104. void Player::update()
  105. {
  106.     this->movUpdate();
  107.  
  108. }
  109.  
  110. void Player::render(sf::RenderTarget* target)
  111. {
  112.     target->draw(this->player);
  113. }
  114.  
  115.  
  116. //////////////
  117. //Enemy.h/////
  118. //////////////
  119.  
  120.  
  121. #pragma once
  122.  
  123. #include "Libs.h"
  124.  
  125. #include "Player.h"
  126.  
  127. class Enemy
  128. {
  129. private:
  130.  
  131.     sf::RectangleShape enemy;
  132.  
  133.     sf::Clock clock;
  134.     sf::Time passed;
  135.  
  136.     Player player;
  137.  
  138.     float increment;
  139.     sf::Vector2f enMov;
  140.     sf::Vector2f moving;
  141.  
  142.     void initVar();
  143.     void initEne();
  144.  
  145. public:
  146.  
  147.     Enemy();
  148.  
  149.     void movUpdate();
  150.  
  151.     void update();
  152.     void render(sf::RenderTarget* target);
  153. };
  154.  
  155. //////////////
  156. //Enemy.cpp///
  157. //////////////
  158.  
  159. #include "Enemy.h"
  160.  
  161. void Enemy::initVar()
  162. {
  163.     this->increment = 2.f;
  164. }
  165.  
  166. void Enemy::initEne()
  167. {
  168.     this->enemy.setSize(sf::Vector2f(50, 50));
  169.     this->enemy.setFillColor(sf::Color::Red);
  170. }
  171.  
  172. Enemy::Enemy()
  173. {
  174.     this->initVar();
  175.     this->initEne();
  176. }
  177.  
  178. void Enemy::movUpdate()
  179. {
  180.     this->passed = this->clock.getElapsedTime();
  181.  
  182.     if (this->player.plPos().x > this->enemy.getPosition().x)
  183.     {
  184.         this->moving.x += 1.f;
  185.     }
  186.     else if (this->player.plPos().x < this->enemy.getPosition().x)
  187.     {
  188.         this->moving.x -= 1.f;
  189.     }
  190.     if (this->player.plPos().y > this->enemy.getPosition().y)
  191.     {
  192.         this->moving.y += 1.f;
  193.     }
  194.     else if (this->player.plPos().y < this->enemy.getPosition().y)
  195.     {
  196.         this->moving.y -= 1.f;
  197.     }
  198.  
  199.     this->enMov = this->moving * this->increment * this->passed.asSeconds();
  200.  
  201.     this->enemy.move(this->enMov);
  202.  
  203.     this->clock.restart();
  204. }
  205.  
  206. void Enemy::update()
  207. {
  208.     this->movUpdate();
  209. }
  210.  
  211. void Enemy::render(sf::RenderTarget* target)
  212. {
  213.     target->draw(this->enemy);
  214. }
  215.  
  216.  
  217. //////////////
  218. //Engine.h////
  219. //////////////
  220.  
  221. #pragma once
  222.  
  223. #include "Libs.h"
  224.  
  225. #include "Player.h"
  226. #include "Enemy.h"
  227.  
  228. class Engine
  229. {
  230. private:
  231.     sf::RenderWindow* window;
  232.     sf::VideoMode vMode;
  233.  
  234.     sf::Event event;
  235.  
  236.     Player player;
  237.     Enemy enemy;
  238.  
  239.     void initVar();
  240.     void initWin();
  241.  
  242. public:
  243.  
  244.     Engine();
  245.     virtual ~Engine();
  246.  
  247.     const bool running() const;
  248.  
  249.     void pollEvent();
  250.  
  251.     void update();
  252.     void render();
  253. };
  254.  
  255.  
  256. //////////////
  257. //Engine.cpp//
  258. //////////////
  259.  
  260. #include "Engine.h"
  261.  
  262. void Engine::initVar()
  263. {
  264.     this->window = nullptr;
  265. }
  266.  
  267. void Engine::initWin()
  268. {
  269.     this->vMode.height = 600;
  270.     this->vMode.width = 800;
  271.  
  272.     this->window = new sf::RenderWindow(this->vMode, "Game");
  273. }
  274.  
  275. Engine::Engine()
  276. {
  277.     this->initVar();
  278.     this->initWin();
  279. }
  280.  
  281. Engine::~Engine()
  282. {
  283.     delete this->window;
  284. }
  285.  
  286. const bool Engine::running() const
  287. {
  288.     return this->window->isOpen();
  289. }
  290.  
  291. void Engine::pollEvent()
  292. {
  293.     while (this->window->pollEvent(event))
  294.     {
  295.         switch (this->event.type)
  296.         {
  297.             case sf::Event::Closed:
  298.             {
  299.                 this->window->close();
  300.             }
  301.             case sf::Event::KeyPressed:
  302.             {
  303.                 if (this->event.key.code == sf::Keyboard::Escape)
  304.                 {
  305.                     this->window->close();
  306.                 }
  307.             }
  308.         }
  309.     }
  310. }
  311.  
  312. void Engine::update()
  313. {
  314.     this->pollEvent();
  315.     this->player.update();
  316.     this->enemy.update();
  317. }
  318.  
  319. void Engine::render()
  320. {
  321.     this->window->clear();
  322.  
  323.     this->player.render(this->window);
  324.     this->enemy.render(this->window);
  325.  
  326.     this->window->display();
  327. }
  328.  
  329.  
  330.  
  331. //////////////
  332. //Main.cpp////
  333. //////////////
  334.  
  335.  
  336. #include "Libs.h"
  337.  
  338. #include "Engine.h"
  339.  
  340. int main()
  341. {
  342.     Engine game;
  343.    
  344.     while (game.running())
  345.     {
  346.         game.update();
  347.  
  348.         game.render();
  349.     }
  350.  
  351.  
  352. }
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement