Advertisement
TermSpar

C++ 2D Platformer

Jul 21st, 2016
6,014
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <SFML/Graphics.hpp>
  3. #include "Player.h"
  4. #include "Coin.h"
  5. #include <sstream>
  6.  
  7. int main() {
  8.     sf::RenderWindow window;
  9.  
  10.     sf::Vector2i centerWindow((sf::VideoMode::getDesktopMode().width / 2) - 445, (sf::VideoMode::getDesktopMode().height / 2) - 480);
  11.  
  12.     window.create(sf::VideoMode(900, 900), "SFML Gravity", sf::Style::Titlebar | sf::Style::Close);
  13.     window.setPosition(centerWindow);
  14.  
  15.     window.setKeyRepeatEnabled(true);
  16.  
  17.     //Player Object:
  18.     Player player({ 40, 40 });
  19.     player.setPos({ 50, 700 });
  20.  
  21.     //Coin Objects:
  22.     std::vector<Coin*> coinVec;
  23.     Coin coin1({ 20, 20 });
  24.     Coin coin2({ 20, 20 });
  25.     coinVec.push_back(&coin1);
  26.     coinVec.push_back(&coin2);
  27.  
  28.     coin1.setPos({ 50, 600 });
  29.     coin2.setPos({ 100, 600 });
  30.  
  31.     //Score Objects:
  32.  
  33.     int score = 0;
  34.  
  35.     sf::Font arial;
  36.     arial.loadFromFile("arial.ttf");
  37.  
  38.     std::ostringstream ssScore;
  39.     ssScore << "Score: " << score;
  40.  
  41.     sf::Text lblScore;
  42.     lblScore.setCharacterSize(30);
  43.     lblScore.setPosition({ 10, 10 });
  44.     lblScore.setFont(arial);
  45.     lblScore.setString(ssScore.str());
  46.  
  47.     //Gravity Variables:
  48.     const int groundHeight = 700;
  49.     const float gravitySpeed = 0.3;
  50.     bool isJumping = false;
  51.  
  52.     //Main Loop:
  53.     while (window.isOpen()) {
  54.  
  55.         sf::Event Event;
  56.  
  57.         const float moveSpeed = 0.2;
  58.  
  59.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
  60.             player.move({ 0, -moveSpeed });
  61.             isJumping = true;
  62.         }
  63.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
  64.             player.move({ moveSpeed, 0 });
  65.         }
  66.         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
  67.             player.move({ -moveSpeed, 0 });
  68.         }
  69.  
  70.         //Event Loop:
  71.         while (window.pollEvent(Event)) {
  72.             switch (Event.type) {
  73.  
  74.             case sf::Event::Closed:
  75.                 window.close();
  76.  
  77.             case sf::Event::KeyReleased:
  78.                 isJumping = false;
  79.             }
  80.         }
  81.  
  82.         //Gravity Logic:
  83.         if (player.getY() < groundHeight && isJumping == false) {
  84.             player.move({ 0, gravitySpeed });
  85.         }
  86.  
  87.         //Coin Logic:
  88.         for (int i = 0; i < coinVec.size(); i++) {
  89.             if (player.isCollidingWithCoin(coinVec[i])) {
  90.                 coinVec[i]->setPos({ 422234, 423432 });
  91.                 score++;
  92.                 ssScore.str("");
  93.                 ssScore << "Score " << score;
  94.                 lblScore.setString(ssScore.str());
  95.             }
  96.         }
  97.  
  98.         window.clear();
  99.         coin1.drawTo(window);
  100.         window.draw(lblScore);
  101.         coin2.drawTo(window);
  102.         player.drawTo(window);
  103.         window.display();
  104.     }
  105. }
  106.  
  107. #pragma once
  108.  
  109. #include <iostream>
  110. #include <SFML\Graphics.hpp>
  111. #include "Coin.h"
  112.  
  113. class Player {
  114. public:
  115.     Player(sf::Vector2f size) {
  116.         player.setSize(size);
  117.         player.setFillColor(sf::Color::Green);
  118.     }
  119.  
  120.     void drawTo(sf::RenderWindow &window) {
  121.         window.draw(player);
  122.     }
  123.  
  124.     void move(sf::Vector2f distance) {
  125.         player.move(distance);
  126.     }
  127.  
  128.     void setPos(sf::Vector2f newPos) {
  129.         player.setPosition(newPos);
  130.     }
  131.  
  132.     int getY() {
  133.         return player.getPosition().y;
  134.     }
  135.  
  136.     bool isCollidingWithCoin(Coin *coin) {
  137.         if (player.getGlobalBounds().intersects(coin->getGlobalBounds())) {
  138.             return true;
  139.         }
  140.         return false;
  141.     }
  142. private:
  143.     sf::RectangleShape player;
  144. };
  145.  
  146. #pragma once
  147.  
  148. #include <iostream>
  149. #include <SFML\Graphics.hpp>
  150.  
  151. class Coin {
  152. public:
  153.     Coin(sf::Vector2f size) {
  154.         coin.setSize(size);
  155.         coin.setFillColor(sf::Color::Yellow);
  156.     }
  157.  
  158.     void drawTo(sf::RenderWindow &window) {
  159.         window.draw(coin);
  160.     }
  161.  
  162.     sf::FloatRect getGlobalBounds() {
  163.         return coin.getGlobalBounds();
  164.     }
  165.  
  166.     void setPos(sf::Vector2f newPos) {
  167.         coin.setPosition(newPos);
  168.     }
  169. private:
  170.     sf::RectangleShape coin;
  171. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement