Advertisement
Felanpro

Using your own shape classes

Apr 17th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include <SFML/Window.hpp>
  5.  
  6. using namespace std;
  7.  
  8. int width = 800;
  9. int height = 600;
  10.  
  11. class Player
  12. {
  13. public:
  14.     sf::RectangleShape body;
  15.     sf::Vector2f velocity;
  16.     float gravity_ = .3;
  17.  
  18.     //Constructor
  19.     Player(int width, int height, int xinitPos, int yinitPos, sf::Color a)
  20.     {
  21.         body.setSize(sf::Vector2f(width, height));
  22.         body.setPosition(xinitPos, yinitPos);
  23.         body.setFillColor(a);
  24.     }
  25.  
  26.     void moveCharacter(int xPos, int yPos)
  27.     {
  28.         body.move(xPos, yPos);
  29.     }
  30.  
  31.     void setCharacterPosition(int xPos, int yPos)
  32.     {
  33.         body.setPosition(xPos, yPos);
  34.     }
  35.  
  36.     //Parameter takes in the height at wich the invisible plane lies.
  37.     void gravity(int height_of_plane)
  38.     {
  39.         if (body.getPosition().y >= height_of_plane)
  40.         {
  41.             body.setPosition(body.getPosition().x, height_of_plane);
  42.             velocity.y = 0;
  43.         }
  44.         else if (body.getPosition().y <= height_of_plane)
  45.         {
  46.             body.move(velocity);
  47.             velocity.y += gravity_;
  48.         }
  49.     }
  50. };
  51.  
  52. int main()
  53. {
  54.     sf::RenderWindow window(sf::VideoMode(width, height), "In Development");
  55.     window.setFramerateLimit(70);
  56.     window.setVerticalSyncEnabled(true);
  57.  
  58.     Player character(40, 40, width / 2, height / 2, sf::Color::Black);
  59.  
  60.     sf::Event event;
  61.     while (window.isOpen())
  62.     {
  63.         while (window.pollEvent(event))
  64.         {
  65.             if (event.type == sf::Event::Closed)
  66.                 window.close();
  67.  
  68.             if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  69.             {
  70.                 character.moveCharacter(2, 0);
  71.             }
  72.             else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  73.             {
  74.                 character.moveCharacter(-2, 0);
  75.             }
  76.             else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  77.             {
  78.                 character.moveCharacter(0, -2);
  79.             }
  80.             else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  81.             {
  82.                 character.moveCharacter(0, 2);
  83.             }
  84.         }
  85.  
  86.         character.gravity(500);
  87.  
  88.         window.clear(sf::Color::White); //Clear
  89.  
  90.         window.draw(character.body);
  91.  
  92.         window.display(); //Display
  93.     }
  94.  
  95.     int pause; cin >> pause; //Pause the program
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement