Facebook
From Sweltering Lion, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 241
  1. #include "Screen.h"
  2.  
  3. int main()
  4. {
  5. Screen win;
  6. win.draw();
  7.  
  8. return EXIT_SUCCESS;
  9. }
  10.  
  11. include "Player.h"
  12. #include "Platform.h"
  13. #include <SFML\Graphics.hpp>
  14.  
  15. using namespace sf;
  16.  
  17. class Screen
  18. {
  19. public:
  20.         void show()
  21.         {
  22.                 RenderWindow window(VideoMode(960, 720), "Bix Adventure 0.0.1");
  23.                 Event event;
  24.                 window.setFramerateLimit(60);
  25.                 Player player({ 0,620 }, { 40,40 }, Color::Yellow);
  26.                 Platform ground({ 0,660 }, { 960,60 }, Color::Magenta);
  27.                 Platform platform1({ 650 ,600 }, { 100,25 }, Color::White);
  28.                 while (window.isOpen())
  29.                 {
  30.                         window.clear();
  31.                         while (window.pollEvent(event))
  32.                         {
  33.                                 if (event.type == Event::Closed)
  34.                                 {
  35.                                         window.close();
  36.                                 }
  37.                         }
  38.                         {
  39.                                 ground.update(window);
  40.                                 platform1.update(window);
  41.                                 player.update(window);
  42.  
  43.                                 check(player, ground);
  44.                                 check(player, platform1);
  45.                         }
  46.                         window.display();
  47.                 }
  48.         }
  49.  
  50.         Screen()
  51.         {
  52.  
  53.         }
  54. private:
  55.         Vector2f playerPos1, playerPos2;
  56.         Vector2f platformPos1, platformPos2;
  57.         void check(Player &player, Platform &platform)
  58.         {
  59.                 playerPos1 = player.player.getPosition();
  60.                 playerPos2.x = player.player.getPosition().x + player.player.getSize().x;
  61.                 playerPos2.y = player.player.getPosition().y + player.player.getSize().y;
  62.                 platformPos1 = platform.platform.getPosition();
  63.                 platformPos2.x = platform.platform.getPosition().x + platform.platform.getSize().x;
  64.                 platformPos2.y = platform.platform.getPosition().y + platform.platform.getSize().y;
  65.                 if (player.state != Player::stan::isJumping)
  66.                 {
  67.                         if (playerPos2.y == platformPos1.y)
  68.                         {
  69.                                 player.state = Player::stan::isOnGround;
  70.                         }
  71.                 }
  72.                 if (playerPos1.x < platformPos2.x || playerPos2.x > platformPos1.x)
  73.                 {
  74.                         player.state = Player::stan::isFalling;
  75.                 }
  76.         }
  77. };
  78.  
  79. #include <SFML/Graphics.hpp>
  80.  
  81. using namespace sf;
  82.  
  83. class Platform
  84. {
  85. public:
  86.         RectangleShape platform;
  87.         float p1, p2;
  88.         Platform(Vector2f position, Vector2f size, Color color)
  89.         {
  90.                 platform.setPosition(position);
  91.                 platform.setSize(size);
  92.                 platform.setFillColor(color);
  93.                 p1 = position.x;
  94.                 p2 = size.x;
  95.  
  96.         }
  97.         void update(RenderWindow &window)
  98.         {
  99.                 window.draw(platform);
  100.         }
  101.         Vector2f playerPos1, playerPos2;
  102.         Vector2f platformPos1, platformPos2;
  103. };
  104.  
  105. #include <SFML/Graphics.hpp>
  106.  
  107. using namespace sf;
  108.  
  109. class Player
  110. {
  111. public:
  112.         enum stan
  113.         {
  114.                 isOnGround,
  115.                 isJumping,
  116.                 isFalling,
  117.         };
  118.         stan state = isOnGround;
  119.         RectangleShape player;
  120.         Player(Vector2f position,Vector2f size, Color color)
  121.         {
  122.                 player.setPosition(position);
  123.                 player.setFillColor(color);
  124.                 player.setSize(size);
  125.         }
  126.         void update(RenderWindow &window)
  127.         {
  128.                 control();
  129.                 checkJumping();
  130.                 if (way == Left)
  131.                 {
  132.                         player.move(-5, 0);
  133.                 }
  134.                 else if (way == Right)
  135.                 {
  136.                         player.move(5, 0);
  137.                 }
  138.                 window.draw(player);
  139.         }
  140. private:
  141.         void control()
  142.         {
  143.                 way = None;
  144.                 if (Keyboard::isKeyPressed(Keyboard::Key::D))
  145.                 {
  146.                         way = Right;
  147.                 }
  148.                 else if (Keyboard::isKeyPressed(Keyboard::Key::A))
  149.                 {
  150.                         way = Left;
  151.                 }
  152.  
  153.                 if (Keyboard::isKeyPressed(Keyboard::Key::Space))
  154.                 {
  155.                         state = isJumping;
  156.                 }
  157.         }
  158.         void checkJumping()
  159.         {
  160.                 switch (state)
  161.                 {
  162.                 case isJumping:
  163.                         if (jumpFrame < 30)
  164.                         {
  165.                                 player.move(0, jumpSpeed);
  166.                                 jumpSpeed = jumpSpeed + 0,05;
  167.                                 jumpFrame++;
  168.                         }
  169.                         else
  170.                         {
  171.                                 state = isFalling;
  172.                         }
  173.                         break;
  174.                 case isFalling:
  175.                         player.move(0, gravity);
  176.                         break;
  177.                 case isOnGround:
  178.                         jumpFrame = 0;
  179.                         break;
  180.                 }
  181.         }
  182.         enum kierunek_ruchu
  183.         {
  184.                 None,
  185.                 Left,
  186.                 Right,
  187.         };
  188.         kierunek_ruchu way;
  189.         float jumpSpeed = -5;
  190.         float gravity = -jumpSpeed;
  191.         int jumpFrame = 0;     
  192. };