Я использовал другой алгоритм: требуется только знать про первый сегмент и последний. При передвижении первый передвигается(new coord += direct*width, где direct -1, 0 или 1), а последний сегмент затирается. В итоге происходит движение. Единственно, что следует сохранять - точки поворота головы, чтобы хвост знал, где повернуть. Движок:
| Код | void check_for_reverce(); const int width = 60; //width of the each segment of the snake Snake first = Snake(180, 0, 1, 0); //first part of the snake Snake lost = Snake(60,0, 1, 0); //the lost part of the snake std::vector<Snake>point_of_reverce(500); int i=0; //count_of_points; int curr_point=0; //the nearest point of reverce /*============================================================================================= check vor reverce, make direction of moving of the lost part */ void check_for_reverce(){ // check vor reverce, make direction of moving of the lost part if(lost.xpos == point_of_reverce[curr_point].xpos && lost.ypos == point_of_reverce[curr_point].ypos){ lost.x_direct = point_of_reverce[curr_point].x_direct; lost.y_direct = point_of_reverce[curr_point].y_direct; curr_point++; //std::cout<<"Reverced!\n"; } } ////////////////////////////////////////////////////////////////////////////////////////////////// case SDLK_UP:{ first.y_direct = -1; first.x_direct = 0; point_of_reverce[i] = Snake(first.xpos, first.ypos, 0, -1); i++; break;} case SDLK_DOWN:{ first.y_direct = 1; first.x_direct = 0; point_of_reverce[i] = Snake(first.xpos, first.ypos, 0, 1); i++; break;} case SDLK_LEFT:{ first.x_direct = -1; first.y_direct = 0; point_of_reverce[i] = Snake(first.xpos, first.ypos, -1, 0); i++; break;} case SDLK_RIGHT:{ first.x_direct = 1; first.y_direct = 0; point_of_reverce[i] = Snake(first.xpos, first.ypos, 1, 0); i++; break;} first.xpos += first.x_direct*width; first.ypos += first.y_direct*width; DrawMove(&first,&lost); check_for_reverce(); lost.xpos += lost.x_direct*width; lost.ypos += lost.y_direct*width;
| |