⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 goldenbugs.c

📁 一个C做的蛇蛇游戏阿
💻 C
📖 第 1 页 / 共 3 页
字号:
/******************************************/
/*       goldenbugs.c of goldenbugs       */
/*                                        */
/*            the main source             */
/*                                        */
/*           Cat Tom 3.28.2003            */
/******************************************/


#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include "d:\win-tc\my pro\showgbp.c"
#include "d:\win-tc\my pro\showmmp.c"
#include "d:\win-tc\my pro\timer.c"
#include "d:\win-tc\my pro\logo.c"

#define UP    18432
#define DOWN  20480
#define LEFT  19200
#define RIGHT 19712

#define ESC   283
#define ENTER 7181
#define TRUE 1
#define FALSE 0

#define GAMERUN 0
#define GAMEWIN 1
#define GAMELOSE 2
#define GAMEQUIT 3

#define SNAKE_HEAD 0
#define SNAKE_BODY 1


void credits1();
void credits2();


struct BODY
{
 int xx;
 int yy;
 int dir;
 struct BODY *next;
}Chain[100],*P_head,*P_tail;

struct BEANTYPE
{
 int scorechange;
 int speedchange;
 int lifechange;
 int breakwall;
 int beantime;
 char *beanpic;
}Beans[10]={
            { 200, 0, 0,0,150},
            { 150,-1, 0,0,150},
            { 150, 1, 0,0,100},
            {3500, 0, 0,0,100},
            {-2500, 0, 0,0,200},
            {-500, 0, 1,0,100},
            {1000, 0,-1,0,150},
            { 100, 0, 0,1,100},
            {1500, 0, 0,0,150},
            {1500, 0, 0,0,-1}
           };


int Beanx,Beany,Beantype;/*location of the bean*/
int Tempx,Tempy;/*the next position of snakehead*/
int Now_body_len,Max_body_len=3;/*the maximum of the snake body*/
int Bodydir;/*the direction of the snake body*/
char Run=GAMERUN;
char Gamecol=BLUE,Gamefil=11;/*the color of gamepart*/
char Snakestyle=0,*Snakebody[8];/*snake style and the pic pointers*/
char Map[39][27];
long Score;
char Speed=2;
char Breakwall;/*if the snake can break the wall*/
char Life;
char Difficulty=1;
char Facenum;/*the face of the main menu*/
char *P_Bug;/*a pic pointer of the dancing bug in menu*/


/*function to draw windows.mode 1 is a bar,mode 2 is a window.*/
void drawwindow(int xx1,int yy1,int xx2,int yy2,int mode)
{
 switch(mode)
 {
    case 0:setcolor(LIGHTGRAY);
           line(xx2+1,yy1-1,xx2+1,yy2+1);
           line(xx1-1,yy2+1,xx2,yy2+1);
           setcolor(WHITE);
           line(xx2+2,yy1-2,xx2+2,yy2+2);
           line(xx1-2,yy2+2,xx2+1,yy2+2);
           setcolor(BLACK);
           line(xx1-1,yy1-1,xx1-1,yy2);
           line(xx1,yy1-1,xx2,yy1-1);
           setcolor(DARKGRAY);
           line(xx1-2,yy1-2,xx1-2,yy2+1);
           line(xx1-1,yy1-2,xx2+1,yy1-2);
           break;
    case 1:setcolor(BLACK);
           line(xx1,yy2,xx2-1,yy2);
           line(xx2,yy1,xx2,yy2);
           setcolor(DARKGRAY);
           line(xx1+1,yy2-1,xx2-2,yy2-1);
           line(xx2-1,yy1+1,xx2-1,yy2-1);
           setcolor(LIGHTGRAY);
           line(xx1,yy1,xx1,yy2-1);
           line(xx1+1,yy1,xx2-1,yy1);
           setcolor(WHITE);
           line(xx1+1,yy1+1,xx1+1,yy2-2);
           line(xx1+2,yy1+1,xx2-2,yy1+1);
           break;
    default:closegraph();printf("draw window error!\n");exit(0);
 }
}


/*to clear the little green screen*/
void clrgreenscr(int scrnum)
{
 setfillstyle(1,LIGHTGREEN);
 bar(53+180*scrnum,435,223+180*scrnum,455);
}


/*change the num mode*/
void changenum(char *ten,char *one,char num)
{
 *ten=num/10;
 *one=num-*ten*10;
}


/*to draw the face of the game*/
void init()
{
 int i;

 /*draw the surface of programme*/
 setfillstyle(1,BLUE);
 bar(0,0,639,479);
 setcolor(LIGHTBLUE);
 for(i=1;i<480;i+=2)
   line(0,i,639,i);

 setfillstyle(1,LIGHTGRAY);
 bar(3,3,636,476);
 drawwindow(3,3,636,476,1);
 drawwindow(43,40,597,414,0);

 setfillstyle(1,LIGHTGREEN);
 for(i=0;i<3;i++)
 {
  drawwindow(53+180*i,435,223+180*i,455,0);
  bar(53+180*i,435,223+180*i,455);
 }
}


/*to show the date*/
void showdate()
{
 struct date today;
 char str[11],ten_day,one_day,ten_mon,one_mon;

 getdate(&today);
 changenum(&ten_day,&one_day,today.da_day);
 changenum(&ten_mon,&one_mon,today.da_mon);
 settextjustify(0,2);
 settextstyle(0,HORIZ_DIR,1);
 setfillstyle(1,LIGHTGREEN);
 setcolor(DARKGRAY);
 bar(490,442,570,450);
 sprintf(str,"%d%d/%d%d/%d",ten_mon,one_mon,ten_day,one_day,today.da_year);
 outtextxy(494,442,str);
}


/*to show the time*/
void showtime()
{
 char asct[10];
 static long oldsec=0;
 struct time newtime;
 char ten_hour,ten_min,ten_sec,one_hour,one_min,one_sec;
 gettime(&newtime);
 if(newtime.ti_sec!=oldsec)
 {
  changenum(&ten_hour,&one_hour,newtime.ti_hour);
  changenum(&ten_min,&one_min,newtime.ti_min);
  changenum(&ten_sec,&one_sec,newtime.ti_sec);
  sprintf(asct,"%d%d:%d%d:%d%d",ten_hour,one_hour,ten_min,one_min,ten_sec,one_sec);
  setcolor(DARKGRAY);
  settextjustify(0,2);
  settextstyle(0,HORIZ_DIR,1);
  setfillstyle(1,LIGHTGREEN);
  bar(413,435,490,455);
  outtextxy(420,442,asct);
  oldsec=newtime.ti_sec;
 }
}


/*show instructions when options,credit,highscore*/
void instruction(char choice)
{
 char *string[3]={"USE THE ARROW KEYS.","ENTER OR ESC TO QUIT.","ENTER OR ESC TO QUIT."};
 clrgreenscr(0);
 setcolor(DARKGRAY);
 settextstyle(0,0,1);
 settextjustify(0,2);
 outtextxy(60,442,string[choice-1]);
}


/*to creatbeans*/
void creatbean()
{
 char type;

 /*decide the type*/
 if(Now_body_len==Max_body_len-1)
 {
  Beantype=9;
  Beanx=37;
  Beany=13;
 }
 else
 {
  type=random(80);
  switch(type)
  {
   case 79:;
   case 78:Beantype=8;break;
   case 77:Beantype=7;break;
   case 76:;
   case 75:Beantype=6;break;
   case 74:;
   case 73:Beantype=5;break;
   case 72:;
   case 71:;
   case 70:Beantype=4;break;
   case 69:;
   case 68:;
   case 67:Beantype=3;break;
   case 66:;
   case 65:;
   case 64:Beantype=2;break;
   case 63:;
   case 62:;
   case 61:Beantype=1;break;
   default:Beantype=0;
  }

   /*decide the position*/
  do{
     Beanx=random(36)+1;
     Beany=random(27)+1;
    }while(Map[Beanx][Beany]);
 }

 Map[Beanx][Beany]=2;
 if(Snakestyle==1&&Beantype==0)showmmp(Beanx*15+28,Beany*15+25,Snakebody[5]);
 else putimage(Beanx*15+28,Beany*15+25,Beans[Beantype].beanpic,0);

 Beancounter=0;
}


/*to creat a new bean when time over*/
void newbean()
{
 if(Beancounter<Beans[Beantype].beantime||Beantype==9)return;
 Map[Beanx][Beany]=0;
 setfillstyle(Gamefil,Gamecol);
 bar(Beanx*15+28,Beany*15+25,Beanx*15+42,Beany*15+39);
 creatbean();
}


/*to show score in 001230 mode*/
void showscore(int xx,int yy,long num,char digit,char size,char color)
{
 char string[10];
 int i;
 long temp;

 digit--;
 for(i=digit;i>=0;i--)
 {
  temp=pow(10,i);
  string[digit-i]=(int)(num/temp)+'0';
  num-=temp*(int)(num/temp);
 }
 string[digit-i]='\0';

 setcolor(color);
 settextstyle(0,HORIZ_DIR,size);
 settextjustify(0,2);
 outtextxy(xx,yy,string);
}


/*show the rolling line*/
void showroln()
{
 static char time=0;
 char *line[4]={"/","\263","\\","-"};
 if(Mycounter<1)return;
 setfillstyle(1,LIGHTBLUE);
 bar(245,442,253,450);
 bar(385,442,393,450);

 setcolor(YELLOW);
 settextstyle(0,HORIZ_DIR,1);
 settextjustify(0,2);
 outtextxy(245,442,line[time]);
 outtextxy(385,442,line[time++]);
 if(time>3)time=0;
 Mycounter=0;
}



/*show twinkling shield*/
void shield()
{
 if(Breakwall==FALSE)return;

 setcolor(LIGHTCYAN);
 switch(Snakestyle)
 {
  case 0:rectangle(P_head->xx*15+28,P_head->yy*15+25,P_head->xx*15+41,P_head->yy*15+38);break;
  case 1:rectangle(P_head->xx*15+28,P_head->yy*15+25,P_head->xx*15+42,P_head->yy*15+39);break;
  default:;
 }
}


/*show snake's body*/
void showsnake(int xx,int yy,char part)
{
 char dir;
 if(Snakestyle==0)
 {
  switch(part)
  {
   case SNAKE_HEAD:setfillstyle(1,DARKGRAY);
                   bar(xx,yy,xx+13,yy+13);
                   shield();
                   break;
   case SNAKE_BODY:setfillstyle(1,LIGHTBLUE);
                   bar(xx,yy,xx+13,yy+13);
   default:;
  }
  return;
 }

 switch(Bodydir)
 {
  case DOWN:dir=0;break;
  case UP:dir=1;break;
  case LEFT:dir=2;break;
  case RIGHT:dir=3;break;
  default:;
 }

 setfillstyle(Gamefil,Gamecol);
 bar(xx,yy,xx+14,yy+14);
 switch(part)
 {
  case SNAKE_HEAD:showmmp(xx,yy,Snakebody[dir]);
                  shield();
                  break;
  case SNAKE_BODY:showmmp(xx,yy,Snakebody[dir+4]);
  default:;
 }
}


/*to init a newstage*/
void initgame(char *filename)
{
 FILE *fp;
 int i,j,templen;

 /*read the map*/
 if((fp=fopen(filename,"rb"))==NULL)
 {
  printf("map file open error!!!\n");
  exit(0);
 }

 fread(Map,1,1053,fp);
 fread(&templen,sizeof(int),1,fp);
 fclose(fp);

 /*choose a Max_body_len according to 'templen'*/
 /*Max_body_len=templen*(2+Difficulty)/3;*/

 /*draw the background*/
 setfillstyle(Gamefil,Gamecol);
 bar(43,40,597,414);

 /*draw the map*/
 for(i=1;i<=37;i++)
   for(j=1;j<=25;j++)
     if(Map[i][j]==1)drawwindow(28+15*i,25+15*j,41+15*i,38+15*j,1);
 showgbp(43,220,"pic\\door1.gbp");

 /*init the data of the snake*/
 Speed=2;
 Bodydir=RIGHT;
 Breakwall=FALSE;

 /*init the body of the snake*/
 P_tail=Chain+1;
 P_head=Chain+2;

 P_head->xx=3;
 P_head->yy=13;
 P_tail->xx=2;
 P_tail->yy=13;
 P_tail->next=P_head;
 P_head->next=P_tail;
 Map[P_head->xx][P_head->yy]=4;
 Map[P_tail->xx][P_tail->yy]=4;

 Now_body_len=2;

 showsnake(P_head->xx*15+28,P_head->yy*15+25,SNAKE_HEAD);
 showsnake(P_tail->xx*15+28,P_tail->yy*15+25,SNAKE_BODY);

 randomize();
 creatbean();

 /*init other parameters*/
 Run=GAMERUN;
}


/*just walk*/
void justwalk()
{
 setfillstyle(Gamefil,Gamecol);
 bar(P_tail->xx*15+28,P_tail->yy*15+25,P_tail->xx*15+42,P_tail->yy*15+39);
 showsnake(P_head->xx*15+28,P_head->yy*15+25,SNAKE_BODY);

 Map[P_tail->xx][P_tail->yy]=0;
 P_head=P_tail;
 P_tail=P_tail->next;
 P_head->xx=Tempx;
 P_head->yy=Tempy;
 Map[Tempx][Tempy]=4;

 showsnake(P_head->xx*15+28,P_head->yy*15+25,SNAKE_HEAD);

 Timercounter=0;
}


/*grow long*/
void growlong()
{
 setfillstyle(1,LIGHTBLUE);
 showsnake(P_head->xx*15+28,P_head->yy*15+25,SNAKE_BODY);

 Now_body_len++;
 P_head->next=Chain+Now_body_len+1;
 P_head=P_head->next;
 P_head->xx=Beanx;
 P_head->yy=Beany;
 P_head->next=P_tail;

 Map[Beanx][Beany]=4;

 showsnake(P_head->xx*15+28,P_head->yy*15+25,SNAKE_HEAD);

 Timercounter=0;
}


/*do things according to Beantype*/
void eatbean()
{
 switch(Beantype)
 {
  case 0:growlong();
         Score+=(Beans[Beantype].scorechange-Beans[Beantype].scorechange*Beancounter/Beans[Beantype].beantime);
         break;
  case 1:;
  case 2:justwalk();
         Speed+=Beans[Beantype].speedchange;
         if(Speed<1)Speed=1;
         else if(Speed>4)Speed=4;
         Score+=(Beans[Beantype].scorechange-Beans[Beantype].scorechange*Beancounter/Beans[Beantype].beantime);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -