- #pragma once
- #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:
- void check(Player &player, Platform &platform)
- {
- if (player.state != Player::stan::isJumping)
- {
- if (player.playerRight.y == platform.platformLeft.y)
- {
- player.state = Player::stan::isOnGround;
- }
- }
- if (player.playerLeft.x < platform.platformRight.x || player.playerRight.x > platform.platformLeft.x)
- {
- player.state = Player::stan::isFalling;
- }
- }
- };
- #pragma once
- #include <SFML/Graphics.hpp>
- using namespace sf;
- class Player
- {
- public:
- enum stan
- {
- isOnGround,
- isJumping,
- isFalling,
- };
- stan state = isOnGround;
- RectangleShape player;
- Vector2f playerLeft, playerRight;
- 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);
- }
- playerLeft = player.getPosition();
- playerRight = playerLeft + player.getSize();
- 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;
- };
- pragma once
- #include <SFML/Graphics.hpp>
- #include "Player.h"
- using namespace sf;
- class Platform
- {
- public:
- RectangleShape platform;
- Vector2f platformLeft, platformRight;
- Platform(Vector2f position, Vector2f size, Color color)
- {
- platform.setPosition(position);
- platform.setSize(size);
- platform.setFillColor(color);
- platformLeft = position;
- platformRight = position + size;
- }
- void update(RenderWindow &window)
- {
- window.draw(platform);
- }
- Vector2f playerPos1, playerPos2;
- Vector2f platformPos1, platformPos2;
- };