SFML game programming
Nov 25, 2018 at 2:05pm UTC
Hello, just started gusing SFML library to create a game, its not really importat question, but if some1 would find time to help me out it would be apriciated.
The question is, how do i launch a projectile from current player destination, in the direction which it last moved. direction is not that imporatat cause later just gonna add some boolean values to see in wich direction it moved, problem is that i cant figure out how to get current player coordinates in 2d field, and how to launch a projectile, that i would be seen for period of time, (travel disntance)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
//-----main.cpp
#include "SFML/Graphics.hpp"
#include "Player.h"
#include "FireSpell.h"
#include <iostream>
int main()
{
int windowWidth = 800;
int windowHeight = 600;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight), "The game" );
Player player1(windowWidth / 2, windowHeight / 2);
FireSpell fireSpell();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
player1.moveLeft();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
player1.moveRight();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
player1.moveUp();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
player1.moveDown();
}
player1.update();
window.clear(sf::Color::Blue);
window.draw(player1.getShape());
window.display();
}
return 0;
}
//-----player.h
#pragma once
#include "SFML/Graphics.hpp"
class Player
{
private :
sf::Vector2f position;
sf::RectangleShape PlayerSquere;
float playerSpeed = 0.5f;
public :
Player(float startX, float startY);
sf::FloatRect getPosition();
sf::RectangleShape getShape();
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void update();
};
//-----player.cpp
#include "Player.h"
Player::Player(float startX, float startY)
{
position.x = startX;
position.y = startY;
PlayerSquere.setSize(sf::Vector2f(20, 20));
PlayerSquere.setPosition(position);
}
sf::FloatRect Player::getPosition()
{
return PlayerSquere.getGlobalBounds();
}
sf::RectangleShape Player::getShape()
{
return PlayerSquere;
}
////MOVEMENT
void Player::moveLeft()
{
position.x -= playerSpeed;
}
void Player::moveRight()
{
position.x += playerSpeed;
}
void Player::moveUp()
{
position.y -= playerSpeed;
}
void Player::moveDown()
{
position.y += playerSpeed;
}
//Update players position
void Player::update()
{
PlayerSquere.setPosition(position);
}
//getPostition x and y
float getPositionX()
{
return
}
//----firespell.h
pragma once
#include "SFML/Graphics.hpp"
class FireSpell
{
private :
sf::Vector2f direction;
sf::Vector2f speed;
int distance;
bool active;
public :
FireSpell(float x, float y, bool active);
sf::RectangleShape getShape();
bool active();
void moveLeft();
void moveRight();
void moveUp();
void moveDown();
void update();
};
//---firespell.cpp
#include "FireSpell.h"
#include "Player.h"
FireSpell::FireSpell(float x, float y, bool active)
{
if (active == true )
{
}
}
Thank you in advance if someone is bored and can help out
Nov 25, 2018 at 7:15pm UTC
Why don't you use getPosition from the Player class ?
Nov 26, 2018 at 8:14am UTC
For letting a projectile moving for a distinct time you could add a lifetime variable to the projectile, which you could decrease at each time step, until it is <= 0.
Topic archived. No new replies allowed.