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

Pages: [1]
1
General / Window Boundaries
« on: June 06, 2016, 07:21:46 pm »
So I'm new to SFML and I want to start learning to code to eventually build myself up to creating my own games. But for now, I want to create a version of Space Invaders. So far, it's fine, however, I can't find a way around one problem: Window Boundaries. This is my code so far (Quite messy I know. Not quite got to using classes yet):

#include <SFML/Graphics.hpp>

int main()
{
        int INV_HORIZONTAL_POSITION = 330;
        int INV_VERTICAL_POSITION = 440;
        //Renders the Window
        sf::RenderWindow window(sf::VideoMode(720, 480), "Space Invaders!");
        //Initializes the texture for the sprite - so the image can be loaded.
        sf::Texture texture;
        //Loads the Image
        texture.loadFromFile("sprites.PNG");
        //Sets the location to load from on the sprite sheet
        sf::IntRect invader;
        invader.left = 23;
        invader.top = 110;
        invader.width = 106;
        invader.height = 73;
        sf::Sprite sprite(texture, invader);

        //Sets the position of the sprite on the screen
        sprite.setPosition(INV_HORIZONTAL_POSITION, INV_VERTICAL_POSITION);
        //Sets the Scale of the image. (1,1) Native Size.
        sprite.setScale(.5, .5);

        while (window.isOpen())
        {
               

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

                window.clear();
                window.draw(sprite);
                window.display();


                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        sprite.move(-0.1, 0);

                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        sprite.move(0.1, 0);
                }


        }

        return 0;
}


Where in this code do I set any window boundaries to stop sprites straying outside them, and how do I add them?


Pages: [1]
anything