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 - KaliAhmed

Pages: [1]
1
G, do you want to get married? :P Thanks so much!

2
I gave it 0.0f value, but now it wont move..

3
Graphics / Circle not moving correctly, despite following tutorial?
« on: April 04, 2015, 01:06:54 am »
I'm currently reading a PDF on SFML game coding and despite following everything, my code won't work. It compiles and shows a circle, but if I move it, it disappears!! What am I doing wrong?

#include <SFML\Graphics.hpp>

class Game
{
public:
        Game();
        void run();
private:
        void processEvents();
        void update(sf::Time);
        void render();
        void handlePlayerInput(sf::Keyboard::Key, bool);
private:
        sf::RenderWindow mWindow;
        sf::CircleShape mPlayer;
        float PlayerSpeed;
        bool mIsMovingUp, mIsMovingDown, mIsMovingLeft, mIsMovingRight;
        sf::Time deltaTime;
        const sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
};

Game::Game():
mWindow(sf::VideoMode(640, 480), "SFML Application"), mPlayer()
{
        mPlayer.setRadius(40.f);
        mPlayer.setPosition(100.f, 100.f);
        mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
        sf::Clock clock;
        sf::Time timeSinceLastUpdate = sf::Time::Zero;
        while (mWindow.isOpen())
        {
                processEvents();
                timeSinceLastUpdate += clock.restart();
                while (timeSinceLastUpdate > TimePerFrame)
                {
                        timeSinceLastUpdate -= TimePerFrame;
                        processEvents();
                        update(TimePerFrame);
                }
                render();
        }
}

void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
{
        if (key == sf::Keyboard::W)
                mIsMovingUp = isPressed;
        else if (key == sf::Keyboard::S)
                mIsMovingDown = isPressed;
        else if (key == sf::Keyboard::A)
                mIsMovingLeft = isPressed;
        else if (key == sf::Keyboard::D)
                mIsMovingRight = isPressed;
}

void Game::processEvents()
{
        sf::Event event;
        while (mWindow.pollEvent(event))
        {
                switch (event.type)
                {
                case sf::Event::KeyPressed:
                        handlePlayerInput(event.key.code, true);
                        break;
                case sf::Event::KeyReleased:
                        handlePlayerInput(event.key.code, false);
                        break;
                case sf::Event::Closed:
                        mWindow.close();
                        break;
                }
        }
}

void Game::update(sf::Time deltaTime)
{
        sf::Vector2f movement(0.f, 0.f);
        if (mIsMovingUp)
                movement.y -= PlayerSpeed;
        if (mIsMovingDown)
                movement.y += PlayerSpeed;
        if (mIsMovingLeft)
                movement.x -= PlayerSpeed;
        if (mIsMovingRight)
                movement.x += PlayerSpeed;
        mPlayer.move(movement * deltaTime.asSeconds());
}

void Game::render()
{
        mWindow.clear();
        mWindow.draw(mPlayer);
        mWindow.display();
}


int main()
{
        Game game;
        game.run();
}

4
Graphics / Making a slow moving sprite bullet?
« on: March 30, 2015, 11:23:16 am »
Hi again, sorry to be spamming but I am completely new to SFML and struggling quite a bit. How does one make a slow moving bullet sprite in SFML C++ code? I have a basis done that works but it obviously moves waaaaayyy too fast. WOuld like to know how to implement a more slow moving bullet as I intend to create a very basic super mario like game.

Here is my code:

#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
int main()
{
        enum Direction{ Down , Left, Right, Up};
        sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");
        sf::Texture playerTexture;
        sf::Texture bulletTexture;
        sf::Sprite gameCharacter;
        sf::Sprite bullet;
        sf::Vector2i source(1, 32);
       
        if (!playerTexture.loadFromFile("Player.png"))
                window.setTitle("Error!");
        if (!bulletTexture.loadFromFile("Bullet.png"))
                window.setTitle("Error!");
        bullet.setTexture(bulletTexture);
        gameCharacter.setTexture(playerTexture);

        while (window.isOpen())
        {
                window.clear(sf::Color::Blue);
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Escape)
                                        window.close();
                                else if (event.key.code == sf::Keyboard::LShift)
                                {
                                       
                                        for (float i = gameCharacter.getPosition().x; i <= window.getSize().x; i+=.02){
                                                bullet.setPosition(i, gameCharacter.getPosition().y);
                                                window.draw(bullet);
                                        }
                                }


                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        gameCharacter.move(0, -1);
                        if (gameCharacter.getPosition().y < 0)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x, 0));
                        source.y = Up;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        gameCharacter.move(0, 1);
                        if (gameCharacter.getPosition().y > window.getSize().y - gameCharacter.getGlobalBounds().height)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x ,window.getSize().y - gameCharacter.getGlobalBounds().height));
                        source.y = Down;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        gameCharacter.move(1, 0);
                        if (gameCharacter.getPosition().x > window.getSize().x - gameCharacter.getGlobalBounds().width)
                                gameCharacter.setPosition(sf::Vector2f(window.getSize().x - gameCharacter.getGlobalBounds().width, gameCharacter.getPosition().y));
                        source.y = Right;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        gameCharacter.move(-1,0);
                        if (gameCharacter.getPosition().x < 0)
                                gameCharacter.setPosition(sf::Vector2f(0, gameCharacter.getPosition().y));
                        source.y = Left;
                }


                source.x++;
                if (source.x * 32 >= playerTexture.getSize().x)
                        source.x = 0;
                gameCharacter.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                window.draw(gameCharacter);
                window.display();
                window.clear();
        }
}

5
I understand!! Thank you G.!  ;D

6
Thank you very much and I have implemented that but he still seems to jump around when I get him to move to the right of the screen or bottom. How can I make this smoother like the top and left?

7
I am brand new to SFML and no idea how to use anything. I am a noob to C++ too. However, I want to make a sprite that I have stay within the boundaries of the window, as currently he is not stopping on the screen if I direct him too(naughty sprite!)
Thank you :) At the minute I have it so he stays on screen but it's not as fluid at the ones with left and top. My current implementation leads to him "jumping" back and forth when he goes to the right and left.
Here is my code
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
int main()
{
        enum Direction{ Down , Left, Right, Up};
        sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");
        sf::Texture playerTexture;
        sf::Sprite gameCharacter;
        sf::Vector2i source(1, 32);
       
        if (!playerTexture.loadFromFile("Player.png"))
                window.setTitle("My Window");
        gameCharacter.setTexture(playerTexture);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Escape)
                                        window.close();
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        gameCharacter.move(0, -1);
                        if (gameCharacter.getPosition().y < 0)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x, 0));
                        source.y = Up;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        gameCharacter.move(0, 1);
                        if (gameCharacter.getPosition().y > window.getSize().y)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x ,window.getSize().y - gameCharacter.getGlobalBounds().height));
                        source.y = Down;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        gameCharacter.move(1, 0);
                        if (gameCharacter.getPosition().x > window.getSize().x)
                                gameCharacter.setPosition(sf::Vector2f(window.getSize().x-gameCharacter.getGlobalBounds().width, gameCharacter.getPosition().y));
                        source.y = Right;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        gameCharacter.move(-1,0);
                        if (gameCharacter.getPosition().x < 0)
                                gameCharacter.setPosition(sf::Vector2f(0, gameCharacter.getPosition().y));
                        source.y = Left;
                }


                source.x++;
                if (source.x * 32 >= playerTexture.getSize().x)
                        source.x = 0;
                gameCharacter.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                window.draw(gameCharacter);
                window.display();
                window.clear();
        }
}

Pages: [1]