void snake_auto_move(char temp)//实现蛇的自动移动 { int x,y,speed; do{ clear_snake_tail(); drow_snake(1);//画蛇身 x = snake.rear->x; y = snake.rear->y; switch(temp){ case 'w': x--;break; case 's': x++;break; case 'a': y--;break; case 'd': y++; } enqueue(x,y);//新蛇头入队 if(snake.front->x == x && snake.front->y == y){//蛇捕捉到食物后,食物重新随机化 game_info(); show_food(); } else{ dequeue(); }//旧蛇尾出队 drow_snake(0);//画蛇头 speed = -2.3 * snake.length + 157; Sleep(speed);//程序暂停speed个单位毫秒 }while(!_kbhit() && judge());//_kbhit为检测键盘输入 }
int snake_move(void) { int sx[100] = {250, 200, 150, 100, 50}; int sy[100] = {300, 300, 300, 300, 300}; int fx = 0; int fy = 0; int f_count = 100; int step = 50; int x_offset = step; int y_offset = 0; int s_len = 5; char dict = 'd'; char new_dict = 0; char flag = 0; char eat_state = 0; barriar(); system("stty raw -echo"); while(flag == 0) { show_food(&fx, &fy, sx, sy, s_len, &f_count); if(move_one_step(sx, sy, x_offset,y_offset, s_len, eat_state) == -1) { restart_game(sx, sy, s_len); dict = 'd'; new_dict = 0; x_offset = step; y_offset = 0; s_len = 5; } eat_state = eat_food(&fx, &fy, sx, sy, &s_len,&f_count); new_dict = get_key(); switch(new_dict) { case 'a' : if(dict != 'd') dict = new_dict;break; case 'd' : if(dict != 'a') dict = new_dict;break; case 'w' : if(dict != 's') dict = new_dict;break; case 's' : if(dict != 'w') dict = new_dict;break; case 'q' : dict = new_dict; default : break; } switch(dict) { case 'a' : x_offset=-step; y_offset=0;break; case 'd' : x_offset=step; y_offset=0;break; case 'w' : x_offset=0; y_offset=-step;break; case 's' : x_offset=0; y_offset=step;break; case 'q' : flag = 1;break; default : break; } usleep(1000); } system("stty cooked echo"); return 0; }
int main() { help_info(); snake_init(); show_food(); drow_wall(); game_info(); while(judge()){ snake_move(); } return 0; }
void snake::move_snake() { if(direction!=0) { if(direction==UP){node n('*',head->Pos_x,head->Pos_y-1); insert_body(n);} else if(direction==DOWN){node n2('*',head->Pos_x,head->Pos_y+1); insert_body(n2);} else if(direction==LEFT){node n3('*',head->Pos_x-2,head->Pos_y); insert_body(n3);} else if(direction==RIGHT){node n4('*',head->Pos_x+2,head->Pos_y); insert_body(n4);} if(head->Pos_x!=apple_x||head->Pos_y!=apple_y)//判断是否吃苹果 delete_tail(); else creat_food(); show_food(); } }
void design_cp2() { cp=2; srand((unsigned)time(NULL)); food.flag=1; mine.flag=1; direct=4; score=0; poigrass.cor[0].x=240; poigrass.cor[0].y=40; poigrass.cor[1].x=340; poigrass.cor[1].y=180; poigrass.cor[2].x=240; poigrass.cor[2].y=500; poigrass.cor[3].x=60; poigrass.cor[3].y=240; poigrass.cor[4].x=500; poigrass.cor[4].y=300; p0=(struct Snake*)malloc(LEN); head=(struct Snake*)malloc(LEN); tail=(struct Snake*)malloc(LEN); p1=(struct Snake*)malloc(LEN); head->x=2*20; head->y=5*20; tail->x=20; tail->y=5*20; p0=tail; map(); tail->x=head->x-20; tail->y=head->y; head->next=tail; head->previous=NULL; tail->next=NULL; tail->previous=head; tim1=(unsigned)time(NULL); while(1) { while(!kbhit()) { set_poigrass(); set_poigrass1(); if(food.flag==1) set_food(); if(mine.flag==1) set_mine(); show_food(); show_mine(); snake_move(); ju_eat_food(); if(node==16) { initgraph(500,400); setbkcolor(GREEN); cleardevice(); setcolor(RED); setfont(40,0,"华文行楷"); outtextxy(70,160,"恭喜你,进入下一关!"); Sleep(3000); design_cp3(); } ju_meet_mine(); ju_eat_poigrass(); while(p0->next!=NULL) { if((p0->next->x==head->x)&&(p0->next->y==head->y)) life=0; p0=p0->next; } if(life==0) gameover(); if(head->x>=680) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x-20,tail->y,tail->x,tail->y+20); gameover(); } if(head->x<20) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x+20,tail->y,tail->x+40,tail->y+20); gameover(); } if(head->y>=580) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y-20,tail->x+20,tail->y); gameover(); } if(head->y<20) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y+20,tail->x+20,tail->y+40); gameover(); } if(direct==4) if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x-20,tail->y,tail->x,tail->y+20); gameover(); } if(direct==3) if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x+20,tail->y,tail->x+40,tail->y+20); gameover(); } if(direct==1) if(head->x==200&&head->y==380||head->x==420&&head->y==420) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y+20,tail->x+20,tail->y+40); gameover(); } if(direct==2) if(head->x==200&&head->y==160||head->x==420&&head->y==280) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y-20,tail->x+20,tail->y); gameover(); } show_snake(); tim2=(unsigned)time(NULL); if(tim2-tim1>=50) no_poigrass(); } change(); } }
void design_cp3() { cp=3; srand((unsigned)time(NULL)); food.flag=1; mine.flag=1; direct=4; poigrass.cor[0].x=240; poigrass.cor[0].y=40; poigrass.cor[1].x=340; poigrass.cor[1].y=180; poigrass.cor[2].x=240; poigrass.cor[2].y=500; poigrass.cor[3].x=60; poigrass.cor[3].y=240; poigrass.cor[4].x=500; poigrass.cor[4].y=300; poigrass.cor[5].x=600; poigrass.cor[5].y=120; poigrass.cor[6].x=520; poigrass.cor[6].y=540; p0=(struct Snake*)malloc(LEN); head=(struct Snake*)malloc(LEN); tail=(struct Snake*)malloc(LEN); p1=(struct Snake*)malloc(LEN); head->x=2*20; head->y=5*20; tail->x=20; tail->y=5*20; p0=tail; map(); tail->x=head->x-20; tail->y=head->y; head->next=tail; head->previous=NULL; tail->next=NULL; tail->previous=head; tim1=(unsigned)time(NULL); while(1) { while(!kbhit()) { set_poigrass(); set_poigrass1(); if(food.flag==1) set_food(); if(mine.flag==1) set_mine(); show_food(); show_mine(); snake_move(); ju_eat_food(); ju_meet_mine(); ju_eat_poigrass(); while(p0->next!=NULL) { if((p0->next->x==head->x)&&(p0->next->y==head->y)) life=0; p0=p0->next; } if(life==0) gameover(); if(head->x>=680) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x-20,tail->y,tail->x,tail->y+20); gameover(); } if(head->x<20) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x+20,tail->y,tail->x+40,tail->y+20); gameover(); } if(head->y>=580) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y-20,tail->x+20,tail->y); gameover(); } if(head->y<20) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y+20,tail->x+20,tail->y+40); gameover(); } if(direct==4) if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)|| head->y==200&&head->x==300) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x-20,tail->y,tail->x,tail->y+20); gameover(); } if(direct==3) if(head->x==200&&(head->y>=160&&head->y<=380)||head->x==420&&(head->y>=280&&head->y<=420)|| head->y==200&&head->x==580) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x+20,tail->y,tail->x+40,tail->y+20); gameover(); } if(direct==1) if(head->x==200&&head->y==380||head->x==420&&head->y==420|| head->y==200&&(head->x>=300&&head->x<=580)) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y+20,tail->x+20,tail->y+40); gameover(); } if(direct==2) if(head->x==200&&head->y==160||head->x==420&&head->y==280|| head->y==200&&(head->x>=300&&head->x<=580)) { setfillcolor(YELLOW); setcolor(YELLOW); bar(tail->x,tail->y-20,tail->x+20,tail->y); gameover(); tim2=(unsigned)time(NULL); if(tim2-tim1>=80) no_poigrass(); } show_snake(); } change(); } }