Advertisement
Felanpro

Platform Model

Apr 7th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 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 main()
  9. {
  10.     sf::RenderWindow window(sf::VideoMode(800, 600), "In Development");
  11.     window.setFramerateLimit(70);
  12.     window.setVerticalSyncEnabled(true);
  13.  
  14.     sf::RectangleShape square;
  15.     square.setSize(sf::Vector2f(50, 50));
  16.     square.setFillColor(sf::Color::Blue);
  17.     square.setPosition(400, 300);
  18.  
  19.     sf::Vector2f velocity(0, 0);
  20.  
  21.     int jumpSpeed = 10, gravity = 1;
  22.  
  23.     sf::RectangleShape goalpost;
  24.     goalpost.setSize(sf::Vector2f(10, 10));
  25.     goalpost.setFillColor(sf::Color::Red);
  26.  
  27.     sf::RectangleShape platforms[3];
  28.     for (int x = 0; x < 3; x++)
  29.     {
  30.         platforms[x].setSize(sf::Vector2f(80, 30));
  31.         platforms[x].setFillColor(sf::Color::Cyan);
  32.     }
  33.  
  34.     platforms[0].setPosition(100, 200);
  35.     platforms[1].setPosition(200, 300);
  36.     platforms[2].setPosition(400, 400);
  37.  
  38.     sf::Event event;
  39.     while (window.isOpen())
  40.     {
  41.         while (window.pollEvent(event))
  42.         {
  43.             if (event.type == sf::Event::Closed)
  44.                 window.close();
  45.         }
  46.  
  47.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  48.         {
  49.             velocity.y = -jumpSpeed;
  50.         }
  51.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  52.         {
  53.             velocity.x = jumpSpeed;
  54.         }
  55.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  56.         {
  57.             velocity.x = -jumpSpeed;
  58.         }
  59.         else
  60.         {
  61.             velocity.y += gravity;
  62.             velocity.x = 0;
  63.         }
  64.  
  65.         square.move(velocity);
  66.  
  67.         if (square.getPosition().y >= 500)
  68.         {
  69.             square.setPosition(square.getPosition().x, 500);
  70.         }
  71.  
  72.         for (int x = 0; x < 3; x++)
  73.         {
  74.             if (square.getGlobalBounds().intersects(platforms[x].getGlobalBounds()))
  75.             {
  76.                 if ((square.getPosition().y) <= platforms[x].getPosition().y)
  77.                 {
  78.                     square.setPosition(square.getPosition().x, platforms[x].getPosition().y - 50);
  79.                     velocity.y = 0;
  80.                 }
  81.             }
  82.             else
  83.             {
  84.                 ;
  85.             }
  86.         }
  87.  
  88.         window.clear(sf::Color::White); //Clear
  89.  
  90.         window.draw(square);
  91.         window.draw(platforms[0]);
  92.         window.draw(platforms[1]);
  93.         window.draw(platforms[2]);
  94.  
  95.         window.display(); //Display
  96.     }
  97.  
  98.     int pause; cin >> pause; //Pause the program
  99.     return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement