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

Pages: [1]
1
Graphics / Re: Moving rectangle
« on: November 02, 2021, 02:38:39 pm »
Thank you very much, i used move function to solve te problem. It toke me lot of time trying to solve it!!

2
Graphics / Moving rectangle
« on: November 01, 2021, 10:05:56 pm »
Hello to everyone! I'm learning sfml and I'm trying to move a rectangleShape based on input. The problem is that when i press a key for change direction, it starts moving from top, and not from last reached position. It has to be a grid-like movement. Can someone help me with this? How should I change the logic to achive that?
#include <iostream>
#include <cstdlib>
#include "game.h"
using namespace std;

int main()
{
        sf::RenderWindow window(sf::VideoMode(600, 600), "Move shape");
        sf::RectangleShape shape(sf::Vector2f(20, 20));
        shape.setFillColor(sf::Color::Red);
        shape.setPosition(sf::Vector2f(20, 20));

        int speed = 15;
        Clock clock;
        float felapsed = 0;
        sf::Vector2f dir(1, 0);
        int x = 9;
        int y = 0;

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

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

                if (sf::Keyboard::isKeyPressed(Keyboard::Up)) dir = sf::Vector2f(0, -1);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Down)) dir = sf::Vector2f(0, 1);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Right)) dir = sf::Vector2f(1, 0);

                else if (sf::Keyboard::isKeyPressed(Keyboard::Left)) dir = sf::Vector2f(-1, 0);


                float frameTime = 1.0f / speed;

                window.clear();

                if (felapsed >= frameTime)
                {
                        if (dir == sf::Vector2f(1, 0))
                                x++;
                        else if (dir == sf::Vector2f(-1, 0))
                                x--;
                        else if (dir == sf::Vector2f(0, 1))
                                y++;
                        else if (dir == sf::Vector2f(0, -1))
                                y--;

                        shape.setPosition(dir.x*x*16, dir.y * y * 16);

                        window.draw(shape);

                        window.display();

                        felapsed -= frameTime;
                       
                }

                felapsed += clock.restart().asSeconds();
        }

        return 0;
}
 

3
Graphics / Re: Snake creation not renders
« on: October 26, 2021, 09:28:25 am »
Thank you so musch! :D

4
Graphics / Snake creation not renders
« on: October 25, 2021, 09:51:53 pm »
Hello to everyone. I'm trying to create step by ste a snake, but i'm struggling with simple body creation. can you explain me why my code does not renders thee rectangle shapes as I want.
Code: [Select]
int main(int argc, char **argv)
{
    sf::RenderWindow window(sf::VideoMode(600, 600), "Snake");
    std::vector<sf::RectangleShape> body(3);


    for (int i = 0; i < 3; i++)
    {
        body.push_back(sf::RectangleShape(sf::Vector2f(100, 100)));
        body[i].setFillColor(sf::Color::Green);
     }
   
    body[0].setPosition(100, 200);
    body[1].setPosition(205, 200);
    body[2].setPosition(310, 200);

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

        window.clear();

        window.draw(body[0]);
        window.draw(body[1]);
        window.draw(body[2]);
        window.display();
    }

return 0;
}

Pages: [1]