Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - seb_thoms

Pages: [1]
1
General / Is this good beginner code?
« on: July 18, 2019, 11:08:54 pm »
I recently started programming withh C++.
What do you thing about my code?
Do you have any ideas to improve it?

surce.cpp

#include <SFML/Graphics.hpp>
#include <iostream>
#include "movePlayer.h"

//global variables
float speed = 5.0f;

int main()
{
        //game objects
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
        sf::RectangleShape shape(sf::Vector2f(10.0f, 10.0f));
        shape.setFillColor(sf::Color::Cyan);
        window.setFramerateLimit(50);

        while (window.isOpen())
        {
                //close window if closed
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                //move player
                shape.move(inputMoveVector());

                //draw window
                window.clear();
                window.draw(shape);
                window.display();
        }

        return 0;
}
 
movePlayer.h

#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>

extern float speed;

//give out a Vector2f
sf::Vector2f inputMoveVector() {

        float x = 0.0f;
        float y = 0.0f;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                x = speed;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                x = -speed;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                y = -speed;

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                y = speed;

        return sf::Vector2f(x, y);

}
 

Pages: [1]
anything