Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualparinho7

Posted 10 December 2012 - 06:14 PM

I am in the process of making a classic snake game but I have a problem with the movement.

If the snake goes up it normally won't react if you press down. But if it goes up and then I hit right(or left) and then quickly hit down the program isn't able to render it but apparently it recognizes the change of direction so the snake goes down. Same goes for every other direction.

What can I do to fix this?

Here is my code help me please. Thanks in advance!

void CSnake::Event(SDL_Event &event){
if (SDL_KEYDOWN)
switch (event.key.keysym.sym){
  case SDLK_UP:
   if (dir!=DOWN){
	yVel = -20;
	xVel = 0;
	dir = UP;
   }
   break;
  case SDLK_DOWN:
   if (dir!=UP){
	yVel = 20;
	xVel = 0;
	dir = DOWN;
   }
   break;
  case SDLK_LEFT:
   if (dir!=RIGHT){
	xVel = -20;
	yVel = 0;
	dir=LEFT;
   }
   break;
  case SDLK_RIGHT:
   if (dir!=LEFT){
	xVel = 20;
	yVel = 0;
	dir=RIGHT;
   }
   break;
}
}
void CSnake::movement(){
if (t_MoveTime <= (SDL_GetTicks()-t_SnakeStarted)){
  SDL_Rect front;
  iSnake = snake.begin();
  front.x = (*iSnake).x + xVel;
  front.y = (*iSnake).y + yVel;
  snake.push_front(front);
  snake.pop_back();
  t_SnakeStarted = SDL_GetTicks();
}
}

#2parinho7

Posted 10 December 2012 - 06:13 PM

I am in the process of making a classic snake game but I have a problem with the movement.

If the snake goes up it normally won't react if you press down. But if it goes up and then I hit right(or left) and then quickly hit down the program isn't able to render it but apparently it recognizes the change of direction so the snake goes down. Same goes for every other direction.

What can I do to fix this?

Here is my code help me please. Thanks in advance!

void CSnake::Event(SDL_Event &amp;event){
if (SDL_KEYDOWN)
switch (event.key.keysym.sym){
  case SDLK_UP:
   if (dir!=DOWN){
	yVel = -20;
	xVel = 0;
	dir = UP;
   }
   break;
  case SDLK_DOWN:
   if (dir!=UP){
	yVel = 20;
	xVel = 0;
	dir = DOWN;
   }
   break;
  case SDLK_LEFT:
   if (dir!=RIGHT){
	xVel = -20;
	yVel = 0;
	dir=LEFT;
   }
   break;
  case SDLK_RIGHT:
   if (dir!=LEFT){
	xVel = 20;
	yVel = 0;
	dir=RIGHT;
   }
   break;
}
}
void CSnake::movement(){
if (t_MoveTime <= (SDL_GetTicks()-t_SnakeStarted)){
  SDL_Rect front;
  iSnake = snake.begin();
  front.x = (*iSnake).x + xVel;
  front.y = (*iSnake).y + yVel;
  snake.push_front(front);
  snake.pop_back();
  t_SnakeStarted = SDL_GetTicks();
}
}

#1parinho7

Posted 10 December 2012 - 06:12 PM

I am in the process of making a classic snake game but I have a problem with the movement.

If the snake goes up it won't react if you press down, same goes with left and right. But if it goes up and then I hit right(or left) and then quickly hit down the program isn't able to render it but apparently it recognizes the change of direction so the snake goes down. Same goes for every other direction.

What can I do to fix this?

Here is my code help me please. Thanks in advance!

void CSnake::Event(SDL_Event &event){
if (SDL_KEYDOWN)
switch (event.key.keysym.sym){
  case SDLK_UP:
   if (dir!=DOWN){
	yVel = -20;
	xVel = 0;
	dir = UP;
   }
   break;
  case SDLK_DOWN:
   if (dir!=UP){
	yVel = 20;
	xVel = 0;
	dir = DOWN;
   }
   break;
  case SDLK_LEFT:
   if (dir!=RIGHT){
	xVel = -20;
	yVel = 0;
	dir=LEFT;
   }
   break;
  case SDLK_RIGHT:
   if (dir!=LEFT){
	xVel = 20;
	yVel = 0;
	dir=RIGHT;
   }
   break;
}
}
void CSnake::movement(){
if (t_MoveTime <= (SDL_GetTicks()-t_SnakeStarted)){
  SDL_Rect front;
  iSnake = snake.begin();
  front.x = (*iSnake).x + xVel;
  front.y = (*iSnake).y + yVel;
  snake.push_front(front);
  snake.pop_back();
  t_SnakeStarted = SDL_GetTicks();
}
}

PARTNERS