Проблема не с вектором. Вот код:
Код: Выделить всё
#include<iostream>
#include"snake.h"
#include<vector>
#include<cstdlib>
SDL_TimerID move_timer_id;
SDL_TimerID clock_timer_id;
void* move_callback_param;
void* clock_callback_param;
Uint32 move(Uint32 interval, void *param); //is called by SDL timer
Uint32 clock(Uint32 clock_interval, void *clock_param);
int one_second = 1000;
void check_for_reverce();
void gameover();
const int width = 50; //width of the each segment of the snake
Snake first = Snake(150, 0, 1, 0,0); //first part of the snake
Snake second = Snake(100,0,1,0,0);
Snake lost = Snake(50,0, 1, 0,0);
//to do: lost is not checked for reverse, line 81
std::vector<Snake>snakeparts(10000); /*= {&lost, &second }*/
int newpart =2;
Point food;
int speed = 250;
int scores = 0;
int screen_width = 800;
int screen_high = 600;
std::vector<Snake>point_of_reverce(10000);
int current_reverce=0; //count_of_points;
int curr_point=0; //the nearest point of reverce
int main(int argc, char *argv[]){
snakeparts[0] = lost;
snakeparts[1] = second;
initsystem(); //inits sdl, draws background,starts sound,opens ttf
SDL_TimerID move_timer_id = SDL_AddTimer(speed, move, move_callback_param);
SDL_TimerID clock_timer_id= SDL_AddTimer(one_second,clock,clock_callback_param);
for(;;);
SDL_RemoveTimer(move_timer_id);
Mix_CloseAudio();
return 0;
}
/*===============================================================================
==========
checks vor reverce, makes direction of moving of the lost part
================================================================================
=========*/
void check_for_reverce(){
for(int i =0;i<newpart;i++)
if(snakeparts[i].xpos == point_of_reverce[snakeparts[i].curr_point].xpos && snakeparts[i].ypos == point_of_reverce[snakeparts[i].curr_point].ypos){
snakeparts[i].x_direct = point_of_reverce[snakeparts[i].curr_point].x_direct;
snakeparts[i].y_direct = point_of_reverce[snakeparts[i].curr_point].y_direct;
snakeparts[i].curr_point++;
std::cout<<i<<"::"<<snakeparts[i].xpos<<" "<<snakeparts[i].ypos<<" "<<snakeparts[i].x_direct<<" "<<snakeparts[i].y_direct<<'\n';
}
}
void randomize_food(){
srand(snakeparts[0].xpos*first.ypos*newpart*scores);
food.xpos = width*(rand()%(screen_width-150)/width);
food.ypos = width*(rand()%screen_high/width);
}
void makefood(){
randomize_food();
for(int i=0;i<newpart;i++)
if(food.xpos==snakeparts[i].xpos && food.ypos ==snakeparts[i].ypos){
i=-1;
randomize_food();}
Draw_food(food.xpos,food.ypos);
}
int check_for_food(){
if(first.xpos == food.xpos && first.ypos == food.ypos){
scores +=10;
snakeparts[newpart] = Snake(first.xpos,first.ypos,first.x_direct,first.y_direct,0);
newpart++;
print_ttf();
makefood();
return 1;
}
else return 0;
}
Uint32 move(Uint32 interval, void *param) {
SDL_Event event;
if(SDL_PollEvent(&event) != 0)
switch(event.type){
case SDL_QUIT:{
gameover();
break;}
case SDL_KEYDOWN:{
if ( event.key.keysym.sym == SDLK_ESCAPE ){
gameover();
}
else switch(event.key.keysym.sym) {
case SDLK_UP:{
if(first.y_direct != 1){
first.y_direct = -1;
first.x_direct = 0;
point_of_reverce[current_reverce] = Snake(first.xpos, first.ypos, 0, -1,0);
current_reverce++;}
break;}
case SDLK_DOWN:{
if(first.y_direct != -1){
first.y_direct = 1;
first.x_direct = 0;
point_of_reverce[current_reverce] = Snake(first.xpos, first.ypos, 0, 1,0);
current_reverce++;}
break;}
case SDLK_LEFT:{
if(first.x_direct != 1){
first.x_direct = -1;
first.y_direct = 0;
point_of_reverce[current_reverce] = Snake(first.xpos, first.ypos, -1, 0,0);
current_reverce++;}
break;}
case SDLK_RIGHT:{
if(first.x_direct != -1){
first.x_direct = 1;
first.y_direct = 0;
point_of_reverce[current_reverce] = Snake(first.xpos, first.ypos, 1, 0,0);
current_reverce++;}
break;}
}
break;}
}
snakeparts[newpart-1].xpos = first.xpos;
snakeparts[newpart-1].ypos = first.ypos;
first.xpos += first.x_direct*width;
first.ypos += first.y_direct*width;
if(first.xpos>=(screen_width-100)/*100-width of ttf panel*/ || first.xpos<0 || first.ypos>screen_high-width || first.ypos<0)
gameover();
//for(int i =0; i<newpart;i++)
//if(first.xpos==snakeparts[i].xpos && first.ypos==snakeparts[i].ypos)
//gameover();
//DrawMove must take old cords of lost segment of the snake to clear old position
DrawMove(&first,&snakeparts[0]);
check_for_reverce();
if(!check_for_food() ){
snakeparts[0].xpos += snakeparts[0].x_direct*width;
snakeparts[0].ypos += snakeparts[0].y_direct*width;
}
else makefood();
for(int i =1;i<newpart;i++){
snakeparts[i].xpos += snakeparts[i].x_direct*width;
snakeparts[i].ypos += snakeparts[i].y_direct*width;
}
//std::cout<<"First:"<<first.xpos<<" "<<first.ypos<<" "<<first.x_direct<<" "<<first.y_direct<<'\n';
//std::cout<<"Second:"<<snakeparts[newpart-1].xpos<<" "<<snakeparts[newpart-1].ypos<<" "<<snakeparts[newpart-1].x_direct<<" "<<snakeparts[newpart-1].y_direct<<'\n';
return speed;
}
void gameover()
{
std::cout<<"GAME OVER\nYour scores = " << scores<<"\n";
SDL_RemoveTimer(move_timer_id);
CD_close();
Mix_CloseAudio();
TTF_Quit();
exit(0);
}
int m1=0;
int m2=0;
int s1=0;
int s2=0;
Uint32 clock(Uint32 clock_interval, void *clock_param){
s2++;
if(s2>9){
s2=0;
s1++;}
if(s1>=6){
s1=0;
m2++;}
if(m2>9){
m2=0;
m1++;}
print_time();
return one_second;
}
А вот вывод:
Код: Выделить всё
1::200 0 0 1
0::200 0 0 1
1::200 100 1 0
0::200 100 1 0
1::450 100 0 1
0::450 100 0 1
1::450 250 1 0
1::550 250 0 1
0::450 250 1 0
1::550 350 -1 0
0::550 250 0 1
0::550 350 -1 0
1::400 350 0 1
1::400 450 -1 0
0::400 350 0 1
0::400 450 -1 0
1::50 450 0 -1
1::50 350 1 0
0::50 450 0 -1
1::150 350 0 -1
0::50 350 1 0
1::150 250 -1 0
0::150 350 0 -1
1::50 250 0 -1
0::150 250 -1 0
0::50 250 0 -1
1::50 50 1 0
1::150 50 0 1
0::50 50 1 0
1::150 150 -1 0
0::150 50 0 1
1::0 150 0 -1
0::150 150 -1 0
1::0 0 1 0
0::0 150 0 -1
1::100 0 0 1
1::100 100 1 0
0::0 0 1 0
1::200 100 0 1
0::100 0 0 1
1::200 200 1 0
0::100 100 1 0
0::200 100 0 1
0::200 200 1 0
1::500 200 0 1
1::500 300 -1 0
1::350 300 0 -1
0::500 200 0 1
0::500 300 -1 0
1::350 150 -1 0
0::350 300 0 -1
1::200 150 0 1
0::350 150 -1 0
0::200 150 0 1
1::200 500 -1 0
1::100 500 0 -1
0::200 500 -1 0
GAME OVER
Your scores = 50
Странно для цикла(check_for_reverce() ), учитывая, что условия для счётчмка меняеется.