• Create Account

# ViX3LG0N

Member Since 10 Aug 2013
Offline Last Active Apr 19 2015 11:11 AM

### In Topic: beginning a snake SFML

21 August 2013 - 04:34 AM

I also thought it would be complicated to make the snake move like in the original snake game, but i came up with this easy idea:

for (unsigned int i = snake.size()-1; i > 0; i--) // start with the last element(body part)
{
//snake[i] = snake[i-1]; // works for my code
//you would have to use       snake[i].lastX = snake[i-1].lastX;   and     snake[i].lastY = snake[i-1].lastY;
}

Its important that your "snake-head" is the 0th element of the snake vector. I have just a vector<vec2> snake (vec2 is just x and y). So every time you move your snake-head in any direction the x and y of the next body-block just becomes the old head position(or the position of the block in the vector before it).

### In Topic: C++ with SDL, help on how to create a 2D map

17 August 2013 - 03:54 PM

1. The feature you are trying to implement is called "Automapping".

2. SDL is used for opening a window with a context to display stuff on it and handles also the input. You have to think about if you want to use the SDL integrated 2D draw library or if you want to use OpenGL to draw your stuff. I can recommend you used OpenGL as it is easy to begin with, but also has endless level of complexity if you want to. Also there are a lot of tutorials for SDL+OpenGL 2D

3. The next problem would be you need to draw text in an SDL context, with OpenGL it could be hard for you to do text rendering. If you pick the SDL drawing library just use SDL_ttf.

4. I would do automapping with std::vector, so you have "dynamic arrays" and your level can grow

struct Location
{
int x;
int y;
};

vector<Location> automap;

void moveTo(int xPos, int yPos)
{
//Check if you already exploared this location
for (unsigned int i = 0; i < automap.size; i++)
{
if (automap[i].x == xPos)
{
if (automap[i].y == yPos)
{
}
}
}
{
Location tmpPos;
tmpPos.x = xPos;
tmpPos.y = yPos;
automap.push_back(tmpPos);
}
}

Lets say the location "middle in the forrest" is on position x:0 y:0 and if you start the game you call "moveTo(0,0);"

Your Player moves to the north then call "moveTo(0,1);".

Also its better if you make variables for the player-position and just call "moveTo(player.x,player.y)" every time you move to another location and also change the postion so if you go to the north just do "player.y++;" and call "moveTo(player.x,player.y);".

The only thing you need to do now is draw the actual map(which should be correctly automapped with my theorie of automapping).

But this is your decision on how to do that, if you choose OpenGL, i can help you again.

Hope this helps you a bit

PARTNERS