I am just starting out trying to make games in C++. I have some experience with the language but I have never made games or used SDL too much. As a start I took a motion tutorial from http://lazyfoo.net/SDL_tutorials/ and made it into a two player game where random red dots appear and the players race to collect 10. It has collision detection and such which I figure out how to do. After doing this I'm trying to expand it into snakes now not just dots. However I am having problems get the snakes to move correctly. I was able to hack it together somewhat but when changing direction from say right to up the whole snake flips at once instead of moving a section at a time. I think I am missing something conceptually about this. I could do it on a matrix but I dont want to restrict motion to a square matrix
Dot::Dot()
{
//Initialize the offsets
x = 0;
y = 0;
//Initialize the velocity
xVel = 0;
yVel = 0;
radius = DOT_WIDTH/2;
}
Dot::Dot(int xStart, int yStart)
{
//Initialize the offsets
x = xStart;
y = yStart;
//Initialize the velocity
xVel = 0;
yVel = 0;
radius = DOT_WIDTH/2;
}
Dot::Dot(int xStart, int yStart, int xvel, int yvel)
{
//Initialize the offsets
x = xStart;
y = yStart;
//Initialize the velocity
xVel = xvel;
yVel = yvel;
radius = DOT_WIDTH/2;
}
void Dot::handle_input(int player)
{
if(player == 0)
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel += DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel += DOT_WIDTH / 2; break;
}
}
//If a key was released
elseif( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += DOT_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= DOT_HEIGHT / 2; break;
case SDLK_LEFT: xVel += DOT_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= DOT_WIDTH / 2; break;
}
}
}
//other color stuff ommitted
}
void Dot::move()
{
//Move the dot left or right
x+=xVel;
//If the dot went too far to the left or right
if(x + DOT_WIDTH > SCREEN_WIDTH)
x = 0;
if(x < 0)
x = SCREEN_WIDTH-DOT_WIDTH;
//Move the dot up or down
y += yVel;
//If the dot went too far up or down
if(y + DOT_HEIGHT > SCREEN_HEIGHT)
y = 0;
if(y < 0)
y = SCREEN_HEIGHT-DOT_HEIGHT;
}
void Dot::move(int x_velocity, int y_velocity)
{
//Move the dot left or right
xVel = x_velocity;
x+=x_velocity;
//If the dot went too far to the left or right
if(x + DOT_WIDTH > SCREEN_WIDTH)
x = 0;
if(x < 0)
x = SCREEN_WIDTH-DOT_WIDTH;
//Move the dot up or down
yVel = y_velocity;
y += y_velocity;
//If the dot went too far up or down
if(y + DOT_HEIGHT > SCREEN_HEIGHT)
y = 0;
if(y < 0)
y = SCREEN_HEIGHT-DOT_HEIGHT;
}
void Dot::show(int color)
{
//Show the dot
if(color == 0)
apply_surface( x, y, dot, screen );
else
apply_surface( x, y, blueDot, screen );
}
Rather than having speed for the snake, depending on what direction the user is pressing take the "tail" of the snake (the last tile) and place it accordingly in whatever direction from the head of the snake.