Beispiel #1
0
void init_field(field_t *new_field) {

    int width = new_field->width;
    int height = new_field->height;

    long int x, y;
    
    int i;
    srandom(time(0));
    for (i = 0; i < new_field->mine_count; i++) {
        x = random() % width;
        y = random() % height;
#ifdef DEBUG
        fprintf(stderr, "Mine at (%lu, %lu)\n", x, y);
#endif
        set_mine(new_field, (int) x,  (int) y);
    }
}
Beispiel #2
0
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();
	}
}
Beispiel #3
0
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();
	}
}