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.


Topics - paupav

Pages: 1 2 [3]
31
General / automatically create new bodypart in snake game
« on: July 08, 2014, 12:02:33 pm »
So, I'm developing simple snakegame in SFML, and I've just wanted to ask you if there is way to automate creating bodyparts and drawing them. Curently I have code that looks like this:

// defining bodyparts
    sf::RectangleShape square (sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart1(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart2(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart3(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart4(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart5(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart6(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart7(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart8(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart9(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart10(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart11(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart12(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart13(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart14(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart15(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart16(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart17(sf::Vector2f(res.x/grid, res.x/grid));
    sf::RectangleShape bodyPart18(sf::Vector2f(res.x/grid, res.x/grid));
//etc

//pos of bodyyparts

    sf::Vector2i part1Pos(snakePos.x + grid, snakePos.y);
    sf::Vector2i part2Pos(part1Pos.x + grid, part1Pos.y);
    sf::Vector2i part3Pos(part2Pos.x + grid, part2Pos.y);
    sf::Vector2i part4Pos(part3Pos.x + grid, part3Pos.y);
    sf::Vector2i part5Pos(part4Pos.x + grid, part4Pos.y);
    sf::Vector2i part6Pos(part5Pos.x + grid, part5Pos.y);
//etc

//set position of body
    square.setPosition(snakePos.x, snakePos.y);
    bodyPart1.setPosition(part1Pos.x, part1Pos.y);
    bodyPart2.setPosition(part2Pos.x, part2Pos.y);
    bodyPart3.setPosition(part3Pos.x, part3Pos.y);
    bodyPart4.setPosition(part4Pos.x, part4Pos.y);
    bodyPart5.setPosition(part5Pos.x, part5Pos.y);
    bodyPart6.setPosition(part6Pos.x, part6Pos.y);
       
    bodyPart7.setPosition(part1Pos.x, part1Pos.y);
    bodyPart8.setPosition(part2Pos.x, part2Pos.y);
    bodyPart9.setPosition(part3Pos.x, part3Pos.y);
    bodyPart10.setPosition(part4Pos.x, part4Pos.y);
    bodyPart11.setPosition(part5Pos.x, part5Pos.y);
    bodyPart12.setPosition(part6Pos.x, part6Pos.y);
//etc
And then to draw it:
if(snakeSize >= 1)
    window.draw(bodyPart1);
    if(snakeSize >= 2)
    window.draw(bodyPart2);
    if(snakeSize >= 3)
    window.draw(bodyPart3);
    if(snakeSize >= 4)
    window.draw(bodyPart4);
    if(snakeSize >= 5)
    window.draw(bodyPart5);
    if(snakeSize >= 6)
    window.draw(bodyPart6);
//etc

It basically works, but it doesn't look nice.



32
General / player speed
« on: July 05, 2014, 12:29:00 pm »
Ok, I wanna create simple snake game, and I'm stuck.
And I've wanted to ask you how do you create variable that defines player's speed.
E.g I wanna make my snake moves 15 blocks every second. Do you use clock somehow?

This is how I did it before, but that's messy way of doing that:
http://en.sfml-dev.org/forums/index.php?topic=15009.msg105920#msg105920

33
Audio / SFML not playing sound
« on: April 22, 2014, 09:13:28 pm »
So I have problem with SFML sound. 1 file
 plays normally and second one wont play while inside "while" function.

So this is before while function
 //sound
        wonb.loadFromFile("win.wav");
        wons.setBuffer(wonb);
        collectedb.loadFromFile("collected.wav");
        collecteds.setBuffer(collectedb);

and this is inside while function
collecteds.play();
wons.play();

For some reason collecteds play's normally and wons doesn't. Both are .wav formats downloaded from Soundbible.
This is the one (wonb/wons) doesn't work:
http://soundbible.com/1964-Small-Crowd-Applause.html

i've tryed few other with same result.

This is full code if anyone is interested:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>

int main()
{
    pocetak:
    sf::ContextSettings settings;
    sf::Clock clock, clock2, clock3, clock4;


    settings.antialiasingLevel = 16;
    sf::RenderWindow window(sf::VideoMode(800, 600), "DogePav", sf::Style::Default, settings);

    //sound thingy
    sf::SoundBuffer wonb, collectedb;
    sf::Sound wons, collecteds;


    struct randomposition
    {
        int x;
        int y;
    }trir, octr, sqr;


    srand(time(0));

    trir.x = (rand() % 580)+ 40;
    trir.y = (rand() % 320) + 99;

    octr.x = (rand() % 260) + 40;
    octr.y = (rand() % 320) + 99;

    sqr.x = (rand() % 200) + 400;
    sqr.y = (rand() % 320) + 200;

    std::cout << " octr.x " << octr.x << " octr.y" << octr.y << std::endl;
    std::cout << " tri.x " << trir.x << " tri.y" << trir.y << std::endl;
    std::cout << " sqr.x " << sqr.x << " sqr.y" << sqr.y << std::endl;


    //objects
    sf::Vector2i tripos(trir.x, trir.y);
    sf::Vector2i octpos(octr.x, octr.y);
    sf::Vector2i sqpos(sqr.x, sqr.y);
    sf::Vector2i collected(760, 20);

    //bouncing
    bool returning = false;
    bool userControls = true;
    float positionx = 10;
    float positiony = 16;

        //defining objects
        sf::CircleShape triangle(15, 3);
        sf::CircleShape square(15, 4);
        sf::CircleShape octagon(15, 8);
        sf::CircleShape shape(30, 999);
        sf::Texture texture, texture2;

        if(!texture.loadFromFile("texture.png"))
            std::cout << "Could not load ball texture" << std::endl;

        shape.setTexture(&texture);
        //floor
        sf::RectangleShape floor(sf::Vector2f(800, 60));

        if(!texture2.loadFromFile("brick.png"))
            std::cout << "Could not load floor texture" << std::endl;

        floor.setTexture(&texture2);
        floor.setPosition(0, 540);

        octagon.setFillColor(sf::Color::Blue);
        triangle.setFillColor(sf::Color::Yellow);
        square.setFillColor(sf::Color::Magenta);

        //sound
        wonb.loadFromFile("win.wav");
        wons.setBuffer(wonb);
        collectedb.loadFromFile("collected.wav");
        collecteds.setBuffer(collectedb);


    while(window.isOpen())
    {


    wons.play();


        triangle.setPosition(tripos.x, tripos.y);
        octagon.setPosition(octpos.x,octpos.y);
        square.setPosition(sqpos.x,sqpos.y);

        //time
        sf::Time borderBounce = clock3.getElapsedTime();
        sf::Time time = clock.getElapsedTime();
        sf::Time physic = clock2.getElapsedTime();


        shape.setPosition(positionx, positiony);
        if(!returning)
        positiony = time.asMicroseconds() / 2319 + physic.asMilliseconds()/ 22;

        while(positionx < 2)
            positionx++;
        while(positionx > 650)
            positionx--;



       if(positiony > 480)
            returning = true;

        if(returning )
        {
            positiony -=8;
            clock.restart();
        }
        if(positiony < 15 + physic.asMilliseconds()/ 22)
        returning = false;


        if(physic.asSeconds() > 13)
        {
            goto pocetak;
        }

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

            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
            }
       //collect triangle


        //std::cout << "Triangle pos: " << positionx << " " << positiony <<std::endl;
        }


        if(tripos.x + 30 > positionx && positionx > tripos.x -30 && tripos.y + 30 > positiony && positiony > tripos.y - 30)
        {
            tripos.x = collected.x;
            tripos.y = collected.y;
            collecteds.play();
        }

        //collect oct
        if(octpos.x + 30 > positionx && positionx > octpos.x -30 && octpos.y + 30 > positiony && positiony > octpos.y - 30)
        {
            octpos.x = collected.x;
            octpos.y = collected.y * 6 - 20;
            collecteds.play();
        }
        //collect square
        if(sqpos.x + 30 > positionx && positionx > sqpos.x -30 && sqpos.y + 30 > positiony && positiony > sqpos.y - 30)
        {
            sqpos.x = collected.x;
            sqpos.y = collected.y * 3;
            collecteds.play();
        }



        //CONTROLLS
        if(userControls)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                positionx += 5;
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                positionx -= 5;
            std::cout.flush();

        }




    window.clear(sf::Color::White);
    window.draw(shape);
    window.draw(floor);
    window.draw(octagon);
    window.draw(triangle);
    window.draw(square);
    window.display();
    window.setFramerateLimit(60);

    }
}
 

34
is it possible? I'm kinda new to SFML.

35
General / Delay in SFML
« on: March 29, 2014, 12:07:49 pm »
This is my code:
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Paupav ball game");
    sf::Clock clock;
    sf::Clock clock2;
    sf::Clock clock3;
    sf::Time borderBounce = clock3.getElapsedTime();
    bool returning = false;
    bool userControls = true;
    float positionx = 10;
    float positiony = 16;
        sf::CircleShape shape(30, 999);
        sf::Texture texture, texture2;

        if(!texture.loadFromFile("texture.jpg"))
        {
            std::cout << "Could not load ball texture" << std::endl;
        }
        shape.setTexture(&texture);
        //floor
        sf::RectangleShape floor(sf::Vector2f(800, 60));

        if(!texture2.loadFromFile("brick.png"))
        {
            std::cout << "Could not load floor texture" << std::endl;
        }
        floor.setTexture(&texture2);
        floor.setPosition(0, 540);

    while(window.isOpen())
    {

        sf::Time time = clock.getElapsedTime();
        sf::Time physic = clock2.getElapsedTime();
        shape.setPosition(positionx, positiony);
        if(!returning)
        positiony = time.asMicroseconds() / 2319 + physic.asMilliseconds()/ 30;


       if(positiony > 480)
            returning = true;

        if(returning )
        {
            positiony -=8;
            clock.restart();
        }
        if(positiony < 15 + physic.asMilliseconds()/ 30)
        returning = false;


        if(physic.asMilliseconds() > 17000.00)
        {
            clock.restart();
            clock2.restart();
        }

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

            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
            }

        }

        if(userControls)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                positionx += 10;
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                positionx -= 10;

        }
        if(positionx < 9)
        {
            while(positionx < 40)
            positionx += 0.001;
        }




    std::cout << "time: " << borderBounce.asMilliseconds() << std::endl;

    window.clear(sf::Color::White);
    window.draw(shape);
    window.draw(floor);
    window.display();
    }
}
 

I know it's a bit messy, those booleans aren't needed etc. I made it using only SFML documentation.  So I want to create that when ball hits borders it bounces back. I wanted to do it like this:
while(positionx < 40)
{increase positionx
delay}
Any suggestions, except to burn this code. The best way of doing this that I can remember is to set positionx every few milli seconds

36
Graphics / in SFML
« on: February 24, 2014, 01:47:49 pm »
Is there any way to import objects made in blender to SFML project? I mean I already completed 50% of the SFML tutorial and in the future I want to make 3d games in it. I know that you can use OpenGl with SFML.

37
General / Hello! I'm new to SFML
« on: July 26, 2013, 03:45:26 pm »
and I wonder is it recommended to use SFML for making 2.5D game such as 2029 online:
http://www.youtube.com/watch?v=KiJaNo1d-6g

Pages: 1 2 [3]