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

Pages: [1]
1
I'm a beginner in SFML and I've been trying to create a small game where two cars can have a race. My intention is to loop the background(the street image) so that it looks like a continuous road. My issue with this thing is that whenever the background moves, it has to wait in order for the screen to go black and only will it upload the image again, while I need it to flow. The car having problems with the restrictions I gave has to do with the incorrect loop and it gets caught in the image that goes down.
(Sorry for possible bad explaining, English isn't my main language)

Quote
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
using namespace std;
const unsigned window_width = 800;
const unsigned window_height = 600;
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Fun Game!");
    sf::View View(window.getDefaultView());
    sf::FloatRect fBounds(0.f, 0.f, 1000.f, 1000.f); // arbitrary > view height

    sf::Texture texture, tc1, tc2;
    texture.loadFromFile("image/road.jpg");
    tc1.loadFromFile("image/redcar.png");
    tc2.loadFromFile("image/car.png");

    sf::IntRect iBounds(fBounds);

    sf::Sprite sprite(texture, iBounds), sc1, sc2;
    sc1.setTexture(tc1);
    sc2.setTexture(tc2);
    sprite.setTexture(texture);

    sc1.setScale(sf::Vector2f(0.03, 0.05));
    sc1.setPosition(sf::Vector2f(500, 450));

    /*sf::Music back;
    back.openFromFile("image/nyan_cat.wav");
    back.setVolume(50);
    back.play();*/

    float viewOffSetY = 0;
    float spriteOffSetY = 0;
    float restView = 0;
    unsigned int textureHeight = texture.getSize().y;

    while (window.isOpen())
    {
        sf::Event event;

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

        }//Event handling done

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            sc1.move(sf::Vector2f(-0.1, 0));
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            sc1.move(sf::Vector2f(0.1, 0));
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            sc1.move(sf::Vector2f(0, -0.2));
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            sc1.move(sf::Vector2f(0, 0.2));
        else
            sc1.move(sf::Vector2f(0, 0));

        //Top collision
        if (sc1.getPosition().y < 0)
            sc1.setPosition(sf::Vector2f(sc1.getPosition().x, 0));
        //Bottom collision
        if (sc1.getPosition().y > window_height)
            sc1.setPosition(sf::Vector2f(sc1.getPosition().x, window_height));
        //Left collision
        if (sc1.getPosition().x < 0)
            sc1.setPosition(sf::Vector2f(0.f, sc1.getPosition().y));
        //Right collision
        if (sc1.getPosition().x + sc1.getGlobalBounds().width > window_width)
            sc1.setPosition(sf::Vector2f(window_width - sc1.getGlobalBounds().width, sc1.getPosition().y));

        View.setCenter(400.f, 500.f - viewOffSetY); // negative y to move 'up' along sprite height
        viewOffSetY += 0.02f; // speed of view movement
        spriteOffSetY = floor(viewOffSetY / textureHeight) * textureHeight;
        sprite.setPosition(fBounds.left, fBounds.top /* - 1000.f + View.getSize().y */ - spriteOffSetY);

        window.clear();
        window.setView(View);
        window.draw(sprite);
        window.draw(sc1);
        window.display();
    }
    return 0;
}

Pages: [1]
anything