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

Pages: [1]
1
Audio / Re: [Linux][SFML 2.4] Unexpected Linker Error
« on: September 05, 2016, 06:54:39 pm »
You probably updated the header but are still linking to the 2.1 binary, try uninstalling everything, make sure there is not trace SFML to be found then reinstall SFML 2.4

2
General / Re: Snake Clone apples
« on: September 04, 2016, 05:10:09 pm »
You're nearly there! Here's a couple of error that I spotted.

  • You are drawing the apple near the end of the main loop anyways, there is no need to draw it elsewhere

    so you can remove all the window.draw(apple); apart from the one at the end
  • You declare apple in the loop which mean it will be deleted and recreated each time the loop is executed.

    this code should go before the while loop

srand(time(0));

int pos_x = rectangle.getPosition().x;
int pos_y = rectangle.getPosition().y;

RectangleShape apple;
apple.setSize(Vector2f(16,16));
apple.setFillColor(Color(250,0,0));
apple.setPosition(400,400);

bool eatenApple = false;
[/list]
  • You don't need to calculate a new position for the apple every frame, only when you collide with the apple

    so I would move it in the collision check condition just like that:
if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
{
    int apple_x = (rand() +32)%752;
    int apple_y = (rand() +32)%552;

    apple.setPosition(apple_x, apple_y);
    window.draw(apple);
}
    it is also to note that srand and rand are old and there is multiple reasons to try to avoid them, I would advise to make research in C++11 random.
    [/li]

Here's the final code:

#include <iostream>
#include <SFML/Graphics.hpp>
#include <ctime>

using namespace std;
using namespace sf;

int main()
{
    RenderWindow window;
    window.create(VideoMode(800,600), "Snake Game");
    window.setFramerateLimit(300);

    RectangleShape rectangle;
    rectangle.setSize(Vector2f(16,16));
    rectangle.setFillColor(Color(250,250,250));
    rectangle.setPosition(100,100);

    Texture wTexture;
    Sprite wallImage;

    if(!wTexture.loadFromFile("Wall.png", IntRect(0, 0, 32, 32)))
        return 1;

    wallImage.setTexture(wTexture);

    Font font;

    if(!font.loadFromFile("arial.ttf"))
        return 1;

    Text text("Game Over", font, 100);
    text.setFillColor(Color(255,255,255));
    text.setPosition(150,200);

    bool turnRight = false;
    bool turnLeft = false;
    bool turnDown = false;
    bool turnUp = false;

    srand(time(0));

    RectangleShape apple;
    apple.setSize(Vector2f(16,16));
    apple.setFillColor(Color(250,0,0));
    apple.setPosition(400,400);

    bool eatenApple = false;

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

            else if(event.type == Event::KeyPressed)
                if(event.key.code == Keyboard::Escape)
                    window.close();
        }

        //MOVEMENT

        if(Keyboard::isKeyPressed(Keyboard::Up))
        {
            turnRight = false;
            turnLeft = false;
            turnDown = false;
            turnUp = true;
        }
        if(Keyboard::isKeyPressed(Keyboard::Down))
        {
            turnRight = false;
            turnLeft = false;
            turnDown = true;
            turnUp = false;
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))
        {
            turnRight = false;
            turnLeft = true;
            turnDown = false;
            turnUp = false;
        }
        if(Keyboard::isKeyPressed(Keyboard::Right))
        {
            turnRight = true;
            turnLeft = false;
            turnDown = false;
            turnUp = false;
        }

        if(turnUp == true)
            rectangle.move(0,-1);
        else if(turnDown == true)
            rectangle.move(0,1);
        else if(turnLeft == true)
            rectangle.move(-1,0);
        else if(turnRight == true)
            rectangle.move(1,0);

        //WALL

        for(int x = 0; x<25; x++)
        {
            wallImage.setPosition(x*32,0);
            window.draw(wallImage);
            wallImage.setPosition(x*32,32*17.75);
            window.draw(wallImage);
            wallImage.setPosition(0,x*32);
            window.draw(wallImage);
            wallImage.setPosition(24*32,x*32);
            window.draw(wallImage);
        }

        //FRUIT

        int pos_x = rectangle.getPosition().x;
        int pos_y = rectangle.getPosition().y;

        if(rectangle.getGlobalBounds().intersects(apple.getGlobalBounds()))
        {
            int apple_x = (rand() +32)%752;
            int apple_y = (rand() +32)%552;

            apple.setPosition(apple_x, apple_y);
        }

        //INTERSECTION WITH WALL

        if(pos_x <= 32 || pos_y <= 32 || pos_y + 16 >= 568 || pos_x + 16 >= 768)
        {
            turnRight = false;
            turnLeft = false;
            turnDown = false;
            turnUp = false;
            window.draw(text);
        }

        window.draw(apple);
        window.draw(rectangle);
        window.display();
        window.clear();
    }
}
 

3
Graphics / Re: Isometric Maps Pathfinding
« on: September 04, 2016, 01:19:55 am »
You could use one array of offset for the odd pos and another for the normal one:

const sf::Vector2i oddOffset[] = {
                                  {-1, 0},
                                  {-1, -1}, {-1, 1},
                                  {-2, 0}, {2, 0},
                                  {1, -1}, {1, 1},
                                  {1, 0}
                                 }
                                 
const sf::Vector2i normalOffset[] = {
                                     {0, 0},
                                     {0, -1}, {0, 1},
                                     {-2, 0}, {2, 0},
                                     {1, -1}, {1, 1},
                                     {1, 0}
                                    }

const bool isOdd = mapTile->getPositionOnMap().y & 1;


for(const auto& pos : isOdd ? oddOffset : normalOffset)
{
    int checkX = mapTile->getPositionOnMap().x + pos.x;
    int checkY = mapTile->getPositionOnMap().y + pos.y;

    if (checkX >= 0 && checkX < mWidth && checkY >= 0 && checkY < mHeight)
    {
        neighbours.push_back(&mMap[checkY][checkX]);
    }
}
 

4
Graphics / Re: Isometric Maps Pathfinding
« on: September 04, 2016, 12:55:47 am »
First I think your first for loop should be

for (int x = -1; x <= 1; x++)

And then the loops go over the 9 tiles but you only want to skip one, the center one so 0,0

if (x == 0 && y == 0)
    continue;
 

Pages: [1]