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