- #include "Screen.h"
- int main()
- {
- Screen win;
- win.draw();
- return EXIT_SUCCESS;
- }
- include "Player.h"
- #include "Platform.h"
- #include <SFML\Graphics.hpp>
- using namespace sf;
- class Screen
- {
- public:
- void show()
- {
- RenderWindow window(VideoMode(960, 720), "Bix Adventure 0.0.1");
- Event event;
- window.setFramerateLimit(60);
- Player player({ 0,620 }, { 40,40 }, Color::Yellow);
- Platform ground({ 0,660 }, { 960,60 }, Color::Magenta);
- Platform platform1({ 650 ,600 }, { 100,25 }, Color::White);
- while (window.isOpen())
- {
- window.clear();
- while (window.pollEvent(event))
- {
- if (event.type == Event::Closed)
- {
- window.close();
- }
- }
- {
- ground.update(window);
- platform1.update(window);
- player.update(window);
- check(player, ground);
- check(player, platform1);
- }
- window.display();
- }
- }
- Screen()
- {
- }
- private:
- Vector2f playerPos1, playerPos2;
- Vector2f platformPos1, platformPos2;
- void check(Player &player, Platform &platform)
- {
- playerPos1 = player.player.getPosition();
- playerPos2.x = player.player.getPosition().x + player.player.getSize().x;
- playerPos2.y = player.player.getPosition().y + player.player.getSize().y;
- platformPos1 = platform.platform.getPosition();
- platformPos2.x = platform.platform.getPosition().x + platform.platform.getSize().x;
- platformPos2.y = platform.platform.getPosition().y + platform.platform.getSize().y;
- if (player.state != Player::stan::isJumping)
- {
- if (playerPos2.y == platformPos1.y)
- {
- player.state = Player::stan::isOnGround;
- }
- }
- if (playerPos1.x < platformPos2.x || playerPos2.x > platformPos1.x)
- {
- player.state = Player::stan::isFalling;
- }
- }
- };
- #include <SFML/Graphics.hpp>
- using namespace sf;
- class Platform
- {
- public:
- RectangleShape platform;
- float p1, p2;
- Platform(Vector2f position, Vector2f size, Color color)
- {
- platform.setPosition(position);
- platform.setSize(size);
- platform.setFillColor(color);
- p1 = position.x;
- p2 = size.x;
- }
- void update(RenderWindow &window)
- {
- window.draw(platform);
- }
- Vector2f playerPos1, playerPos2;
- Vector2f platformPos1, platformPos2;
- };
- #include <SFML/Graphics.hpp>
- using namespace sf;
- class Player
- {
- public:
- enum stan
- {
- isOnGround,
- isJumping,
- isFalling,
- };
- stan state = isOnGround;
- RectangleShape player;
- Player(Vector2f position,Vector2f size, Color color)
- {
- player.setPosition(position);
- player.setFillColor(color);
- player.setSize(size);
- }
- void update(RenderWindow &window)
- {
- control();
- checkJumping();
- if (way == Left)
- {
- player.move(-5, 0);
- }
- else if (way == Right)
- {
- player.move(5, 0);
- }
- window.draw(player);
- }
- private:
- void control()
- {
- way = None;
- if (Keyboard::isKeyPressed(Keyboard::Key::D))
- {
- way = Right;
- }
- else if (Keyboard::isKeyPressed(Keyboard::Key::A))
- {
- way = Left;
- }
- if (Keyboard::isKeyPressed(Keyboard::Key::Space))
- {
- state = isJumping;
- }
- }
- void checkJumping()
- {
- switch (state)
- {
- case isJumping:
- if (jumpFrame < 30)
- {
- player.move(0, jumpSpeed);
- jumpSpeed = jumpSpeed + 0,05;
- jumpFrame++;
- }
- else
- {
- state = isFalling;
- }
- break;
- case isFalling:
- player.move(0, gravity);
- break;
- case isOnGround:
- jumpFrame = 0;
- break;
- }
- }
- enum kierunek_ruchu
- {
- None,
- Left,
- Right,
- };
- kierunek_ruchu way;
- float jumpSpeed = -5;
- float gravity = -jumpSpeed;
- int jumpFrame = 0;
- };