bool snakeclass::collision() { if (snake[0].x == 0 || snake[0].x == Map.width-2 || snake[0].y == 0 || snake[0].y == Map.height-1) return true; for (unsigned int i=2; i < snake.size(); i++) if (snake[0].x == snake[i].x && snake[0].y == snake[i].y) return true; if (snake[0].x == food.x && snake[0].y == food.y) { get = true; putfood(); Map.points += 10; if ((Map.points % 100) == 0) del -= 10000; } else get = false; return false; }
snakeclass::snakeclass() { // init a few variables partchar = '*'; oldalchar = (char)219; foo = '*'; food.x = 0; food.y = 0; for (int i=0; i < 5; i++) { snake.push_back(Character((5 + i), 8)); Map.map[5 + i][8] = 'X'; } Map.points = 0; del = 110000; get = false; direction = 'l'; srand(time(0)); putfood(); }
bool snakeclass::collision() { //collisuon will walls if(snake[0].x==0 || snake[0].x==width-1 || snake[0].y==0 || snake[0].y==height-3) { return true; } //collsiosn wil itlself for(int i=2; i<snake.size(); i++) { if(snake[0].x== snake[i].x && snake[0].y== snake[i].y) // ================================================= return true; } // collsion with food if(snake[0].x==food.x && snake[0].y == food.y) { get=true; //put another food putfood(); points+=10; char c[5]; sprintf(c, "%d", points); SDL_Surface* text; SDL_Color white = {255, 255, 255}; text = TTF_RenderText_Solid(font, c, white); SDL_Rect tmp = {0, 380}; // black rectangle to dlete points postion draw.drawRect(0, 380, SDL_MapRGB(screen->format, 0, 0, 0), 100, 20); SDL_BlitSurface(text, NULL, screen, &tmp); SDL_FreeSurface(text); if((points%80)==0 && del>0) del-=20; } else get = false; return false; }
snakeclass::snakeclass() { //window width and height width =800; height=400; //init SDL and ttf SDL_Init(SDL_INIT_EVERYTHING); TTF_Init(); SDL_WM_SetCaption ("Snake", NULL); font = TTF_OpenFont("lucida.ttf", 16); screen = SDL_SetVideoMode(width,height, 32, SDL_SWSURFACE); for(int i=0; i<5; i++) { snake.push_back(snakepart(40+i, 10)); } points = 0; del = 80; get = false; direction = 'l'; srand(time(0)); putfood(); // SDL draw rect edges of the game draw.drawRect(0,0, SDL_MapRGB(screen->format, 252, 138, 0),width, 10); draw.drawRect(0,0, SDL_MapRGB(screen->format, 252, 138, 0),10, height-20); draw.drawRect(0,370, SDL_MapRGB(screen->format, 252, 138, 0),width, 10); draw.drawRect(width-10,0, SDL_MapRGB(screen->format, 252, 138, 0),10, height-20); for(auto &i : snake) { draw.drawRect(i.x*10,i.y*10, SDL_MapRGB(screen->format, 39, 198, 247)); cout << typeid ( i ).name() << endl; } } //end snake constructor
bool move() { Seg *aux_seg; bool ret=false; switch(mov) { case 1: if(food.x==x+1 && food.y==y) putfood(); else { aux_seg=init_seg; init_seg=init_seg->next; delete aux_seg; } act_seg->x=++x; if(x>29)act_seg->x=x=0; act_seg->y=y; ret=boom(x,y); act_seg->next=new Seg; act_seg=act_seg->next; act_seg->next=0; break; case 2: if(food.x==x && food.y==y+1) putfood(); else { aux_seg=init_seg; init_seg=init_seg->next; delete aux_seg; } act_seg->x=x; act_seg->y=++y; if(y>19)act_seg->y=y=0; ret=boom(x,y); act_seg->next=new Seg; act_seg=act_seg->next; act_seg->next=0; break; case 3: if(food.x==x-1 && food.y==y) putfood(); else { aux_seg=init_seg; init_seg=init_seg->next; delete aux_seg; } act_seg->x=--x; if(x<0)act_seg->x=x=29; act_seg->y=y; ret=boom(x,y); act_seg->next=new Seg; act_seg=act_seg->next; act_seg->next=0; break; case 4: if(food.x==x && food.y==y-1) putfood(); else { aux_seg=init_seg; init_seg=init_seg->next; delete aux_seg; } act_seg->x=x; act_seg->y=--y; if(y<0)act_seg->y=y=19; ret=boom(x,y); act_seg->next=new Seg; act_seg=act_seg->next; act_seg->next=0; break; } //printf("%i %i\n", x, y); return ret; }