Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <SFML/Graphics.hpp>
- #include <SFML/Window.hpp>
- using namespace std;
- int width = 800;
- int height = 600;
- class Player
- {
- public:
- sf::RectangleShape body;
- sf::Vector2f velocity;
- float gravity_ = .3;
- //Constructor
- Player(int width, int height, int xinitPos, int yinitPos, sf::Color a)
- {
- body.setSize(sf::Vector2f(width, height));
- body.setPosition(xinitPos, yinitPos);
- body.setFillColor(a);
- }
- void moveCharacter(int xPos, int yPos)
- {
- body.move(xPos, yPos);
- }
- void setCharacterPosition(int xPos, int yPos)
- {
- body.setPosition(xPos, yPos);
- }
- //Parameter takes in the height at wich the invisible plane lies.
- void gravity(int height_of_plane)
- {
- if (body.getPosition().y >= height_of_plane)
- {
- body.setPosition(body.getPosition().x, height_of_plane);
- velocity.y = 0;
- }
- else if (body.getPosition().y <= height_of_plane)
- {
- body.move(velocity);
- velocity.y += gravity_;
- }
- }
- };
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(width, height), "In Development");
- window.setFramerateLimit(70);
- window.setVerticalSyncEnabled(true);
- Player character(40, 40, width / 2, height / 2, sf::Color::Black);
- sf::Event event;
- while (window.isOpen())
- {
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- character.moveCharacter(2, 0);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- character.moveCharacter(-2, 0);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- character.moveCharacter(0, -2);
- }
- else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- character.moveCharacter(0, 2);
- }
- }
- character.gravity(500);
- window.clear(sf::Color::White); //Clear
- window.draw(character.body);
- window.display(); //Display
- }
- int pause; cin >> pause; //Pause the program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement