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
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#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;
}
#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);
}
#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);
}