一个图形界面的俄罗斯方快c源码

本程序运行环境为TC2.0,游戏的难度级别分为A,B两级

#include "stdio.h"
#include "stdlib.h"
#include "dos.h"
#include "bios.h"
#include "time.h"
#include "malloc.h"
#include "graphics.h"
#include <stddef.h>
unsigned freq[15]={196,262,262,262,330,294,262,
   294,330,294,262,262,330,394,440,};
int dely[15]={25,38,12,25,25,38,12,25,
       12,12,50,25,25,25,50,};
#define ROWS    15                     /*行数*/
#define COLUMNS 10                     /*列数*/
#define BCOLOR  BLUE                   /*填充方块的颜色*/
#define RIGHT 333
#define LEFT  331
#define UP    328
#define DOWN  336
#define PGUP  329
#define PGDN  337
#define ESC   27
#define ENTER 13
#define CTRL_F1 350
#define NOKEY -1
#define a     97
#define F1    315
#define YES_y 89
#define YES_Y 121
char *mask;                                             /*存储填充方块的指针变量*/
char *helpback;      /*存储帮助背景的指针变量*/
int  speed=1,speeddelay=2300;
char level='A';
long score=0;
long maxscore;
int  xMAX=ROWS+1;
char hzmleft[150][16],hzmright[150][16]; /*存储汉字字模*/
int  brick[18][13];                      /*控制画面的数组*/
int  sml[5][5];                     /*用于备份的数组*/
int  sml0[5][5];                    /*存储变幻小方格的数组0*/
int  sml1[5][5];                    /*存储变幻小方格的数组1*/
int  x0,y0,x1,y1;                        /*确定主画面中变幻小方格所在的 4 X 4区域

的两个顶点*/
int  nomovedown=0;
int  cutline_num=0;
void init();
void gethzm();
void disp_hz();
int  get_key();
void clrkey();
void getmaxscore();
void outmaxscore();
void bar3D();
void frame3D();
void makerndbrick();
void outbrick();
void movedown();
void moveleft();
void moveright();
void rotate();
void cutline();
void drwbrick();
void outscore();
void clrtext();
void gameend();
void outtime();
void outspeed();
void getbmp();
void helptext();
void seloption();
void song();
void gamewin();
void main()
{
 int key;
 init();
 seloption();
 randomize();
 makerndbrick();
 outbrick();
 while(1)
 {
  if(nomovedown)
    {
      cutline();
      nomovedown=0;
      outbrick();
    }
  key=get_key();
  clrkey();
  switch(key)
  {
    case UP:
      while(!bioskey(1));
      break;
    case a:
      rotate();
      break;
    case LEFT:
      moveleft();
      break;
    case RIGHT:
      moveright();
      break;
    case DOWN:
      movedown();
      break;
    case F1:
      helptext();
      break;
    case ESC:
      break;
    case NOKEY:
      movedown();
      delay(speeddelay-speed*200);
   }
   if(key==ESC) gameend();
   outtime(440,317);
  }
  closegraph();
  free(mask);
  exit(0);
 }
void seloption()
{
 int updown=0,key;
 char *p1;
 char crt[]={26,'/0'};
 p1=(char *)malloc(sizeof(char));
 helpback=(char *)malloc(imagesize(150,100,340,210)+1);
 getimage(150,100,340,210,helpback);
 frame3D(150,100,340,210);
 disp_hz(216,110,43,3,GREEN);
 disp_hz(160,137,46,2,GREEN);
 setcolor(BLACK);
 outtextxy(195,133,":");
 disp_hz(160,155,48,2,GREEN);
 outtextxy(195,151,":");
 settextstyle(0,0,1);
 outtextxy(240,190,"(PgUP+/-)");
 settextstyle(1,0,1);
 outtextxy(260,133,"(1-9)");
 outtextxy(260,155,"(A/B)");
 setcolor(LIGHTRED);
 outtextxy(240,133,"1");
 outtextxy(240,155,&level);
 while(1)
  { settextstyle(0,0,1);
    setcolor(LIGHTGRAY);
    bar(224,157,234,166);
    bar(224,139,234,148);
    setcolor(BLACK);
    if(updown==0)
      outtextxy(225,140,crt);
    else
      outtextxy(225,158,crt);
    setcolor(LIGHTRED);
    settextstyle(1,0,1);
    key=get_key();
    switch(key)
    {
      case UP:
      case DOWN:
 updown=(updown+1)%2;
 break;
      case ESC:
      case ENTER:
 break;
      case PGUP:
      case PGDN:
       if(updown==0)
 {
  speed=(speed+1)%9+1;
  itoa(speed,p1,10);
  clrtext(210,133,18,40,LIGHTGRAY);
  outtextxy(240,135,p1);
  }
 else
  {
   level=(level=='A'? 'B':'A');
   clrtext(210,153,18,40,LIGHTGRAY);
   outtextxy(240,155,&level);
  }
      }
      if(key==ESC||key==ENTER) break;
   }
 outspeed();
 putimage(150,100,helpback,COPY_PUT);
 free(p1);
 free(helpback);
}
void song()
{
  int i;
  unsigned on;
  for(i=0;i<15;i++)
  {
   outportb(0x43,0xb6);
   freq[i]=0x1234dc/freq[i];
   outportb(0x42,freq[i]&0x00ff);
   freq[i]=freq[i]>>8;
   outportb(0x42,freq[i]);
   on=inportb(0x61);
   outportb(0x61,0x13);
   delay(dely[i]*125);
   outportb(0x61,on);
   }
}
void helptext()                       /*显示帮助信息*/
{
 char cup[]={24,'/0'};
 char cdn[]={25,'/0'};
 char clf[]={27,'/0'};
 char crt[]={26,'/0'};
 helpback=(char *)malloc(imagesize(150,100,340,270)+1);
 getimage(150,100,340,270,helpback);
 frame3D(150,100,340,270);
 settextstyle(0,0,2);
 setcolor(LIGHTGREEN);
 outtextxy(240,160,"@");
 disp_hz(230,110,32,2,YELLOW);
 setcolor(RED);
 outtextxy(240,135,cup);
 disp_hz(160,158,26,2,YELLOW);
 outtextxy(200,160,clf);
 disp_hz(230,206,30,2,YELLOW);
 outtextxy(240,190,cdn);
 disp_hz(300,158,28,2,YELLOW);
 outtextxy(278,160,crt);
 disp_hz(295,244,34,2,LIGHTGREEN);
 setcolor(LIGHTGREEN);
 outtextxy(160,244,"ESC");
 setcolor(RED);
 outtextxy(208,244,"-----");
 while(!bioskey(1));
 putimage(150,100,helpback,COPY_PUT);
 free(helpback);
}
void outspeed()
{
  char *sp;
  sp=(char *)malloc(sizeof(char));
  itoa(speed,sp,10);
  clrtext(470,245,24,100,LIGHTGRAY);
  setcolor(RED);
  outtextxy(470,248,sp);
  free(sp);
}
void outtime(xt,yt)
int xt,yt;
{ struct time t;
  char *sh,*sm,*ss;
  sh=(char *)malloc(sizeof(char));
  sm=(char *)malloc(sizeof(char));
  ss=(char *)malloc(sizeof(char));
  gettime(&t);
  itoa(t.ti_hour,sh,10);
  itoa(t.ti_min,sm,10);
  itoa(t.ti_sec,ss,10);
  setcolor(BLACK);
  settextstyle(0,0,1);
  clrtext(xt,yt,8,64,YELLOW);
  outtextxy(xt,yt,sh);
  outtextxy(xt+16,yt,":  :");
  if(strlen(sm)==1)
   {
    outtextxy(xt+24,yt,"0");
    outtextxy(xt+32,yt,sm);
   }
  else
   outtextxy(xt+24,yt,sm);
  if(strlen(ss)==1)
   {
    outtextxy(xt+48,yt,"0");
    outtextxy(xt+56,yt,ss);
   }
  else
   outtextxy(xt+48,yt,ss);
  free(sh);
  free(sm);
  free(ss);
}
void cutline()
{
 int i,j,STARTROWS,ENDROWS;
 int ii,num,line_num=0;
 STARTROWS=x0;
 if(x1>ROWS)
   ENDROWS=ROWS;
 else
   ENDROWS=x1;
 for(i=ENDROWS;i>=STARTROWS;i--)
  {
   num=0;
   for(j=1;j<=COLUMNS;j++)
     num+=brick[i][j];
   if(num==COLUMNS)
     {
      line_num++;
      for(j=1;j<=COLUMNS;j++)
       {
  brick[i][j]=0;
  putimage((j-1)*25+100,(i-1)*25+50,mask,XOR_PUT);
       }
       for(ii=i;ii>=xMAX;ii--)
  for(j=1;j<=COLUMNS;j++)
   {
    if(brick[ii-1][j]==1)
     {
      brick[ii-1][j]=0;
      brick[ii][j]=1;
      putimage((j-1)*25+100,(ii-2)*25+50,mask,XOR_PUT);
      putimage((j-1)*25+100,(ii-1)*25+50,mask,XOR_PUT);
     }
   }
       i++;
       }
   }
   if(line_num>0)
    {score+=200*(line_num-1)+100;
     outscore();
     if(score>maxscore)
  outmaxscore();
     cutline_num+=line_num;
     if(cutline_num>=20)
      {
 if(speed==9)
  {
   cleardevice();
   gamewin();
  }
 speed++;
 if(speed>5)speeddelay=1800;
 outspeed();
 song();
 cutline_num=0;
       }
    }
}
void gamewin()
{
  getbmp("rusewin.bmp",150,120);
  while(!bioskey(1));
  exit(0);
}
void outmaxscore()
{
 FILE *fpp;
 char *pn;
 long m=0;
 pn=(char *)malloc(sizeof(char)*8);
 m=(~score+1234)*100+(~score+1234)%20;
 ltoa(m,pn,10);
 fpp=fopen("score.dat","w");
 if(fpp!=NULL)
  fputs(pn,fpp);
 fclose(fpp);
 free(pn);
}
void rotate()
{
 int i,j;
 for(i=x0;i<=x1;i++)
  for(j=y0;j<=y1;j++)
   if(brick[i][j]==1)
   {
     delay(speeddelay-200*speed);
     movedown();
     goto outrotate;
   }
 drwbrick();
 for(i=x0;i<=x1;i++)
  for(j=y0;j<=y1;j++)
    sml0[i-x0+1][j-y0+1]=brick[i][j];
 for(i=x0;i<=x1;i++)
  for(j=y0;j<=y1;j++)
    brick[j+x0-y0][-i+x1+y0]=sml0[i-x0+1][j-y0+1];
 drwbrick();
 outrotate:;
}
void moveright()
{
 int i,j;
  for(i=x0;i<=x1;i++)
   for(j=y0;j<=y1;j++)
     {
       if((brick[i][j]==2)&&(brick[i][j+1]==1))
  {
   delay(speeddelay-200*speed);
   movedown();
   goto outright;
  }
      }
  drwbrick();
  for(i=x1;i>=x0;i--)
   for(j=y1;j>=y0;j--)
       if(brick[i][j]==2)
          {
     brick[i][j+1]=2;
            brick[i][j]=0;
           }
  y0++;
  y1++;
  drwbrick();
  outright:;
}
void moveleft()
{
  int i,j;
  for(i=x0;i<=x1;i++)
   for(j=y0;j<=y1;j++)
     {
       if((brick[i][j]==2)&&(brick[i][j-1]==1))
  {delay(speeddelay-200*speed);
   movedown();
   goto outleft;
   }
      }
  drwbrick();
  for(i=x1;i>=x0;i--)
   for(j=y0;j<=y1;j++)
       if(brick[i][j]==2)
          {
     brick[i][j-1]=2;
            brick[i][j]=0;
           }
  y0--;
  y1--;
  drwbrick();
  outleft:;
}
void movedown()
{
  int i,j;
  for(i=x0;i<=x1;i++)
   for(j=y0;j<=y1;j++)
     {
       if((brick[i][j]==2)&&(brick[i+1][j]==1))
  {
   nomovedown=1;
   xMAX=(xMAX>x0? x0:xMAX);
   for(i=x0;i<=x1;i++)
    for(j=y0;j<=y1;j++)
      if(brick[i][j]==2)
        brick[i][j]=1;
   goto outdown;
  }
      }
  drwbrick();
  for(i=x1;i>=x0;i--)
   for(j=y1;j>=y0;j--)
       if(brick[i][j]==2)
          {
     brick[i+1][j]=2;
            brick[i][j]=0;
           }
  x0++;
  x1++;
  drwbrick();
outdown:
  ;
}
void drwbrick()
{
  int i,j;
  for(i=x0;i<=x1;i++)
    for(j=y0;j<=y1;j++)
     {
      if(brick[i][j]==2)
  putimage((j-1)*25+100,(i-1)*25+50,mask,XOR_PUT);
      }
}
void outbrick()                    /*将欲览窗口中的图形移入主窗口*/
{
  int i,j;
  x0=0;y0=4;
  x1=3;y1=7;
  for(i=1;i<=2;i++)
    for(j=4;j<=7;j++)
     {
      if(sml1[i][j-4]==1)
 {
  if(brick[i][j]==1)
    gameend();
  brick[i][j]=2;
  putimage((j-1)*25+100,(i-1)*25+50,mask,XOR_PUT);
 }
      }
  makerndbrick();
}
void gameend()
{
  if(score>maxscore)
    outmaxscore();
  frame3D(240,180,325,220);
  disp_hz(250,193,24,2,GREEN);
  disp_hz(282,193,22,2,GREEN);
  while(!bioskey(1));
  closegraph();
  exit(0);
}
void makerndbrick()        /*在欲览窗口中产生一个图形*/
{
  int brk[10][8]={
   {1,1,1,1,0,0,0},
   {1,0,0,0,1,1,1},
   {0,1,0,0,1,1,1},
   {0,0,1,0,1,1,1},
   {1,1,0,0,0,1,1},
   {0,1,1,0,1,1,0},
   {0,1,1,0,0,1,1},
   {1,0,1,0,1,1,1},
   {1,1,0,0,1,1,1},
   {0,1,1,0,1,1,1},
   };
  int n,i,j,k=0;
  if(level=='A')
    n=random(7);
  else
    n=random(10);
  for(i=1;i<3;i++)
   for(j=0;j<4;j++)
     {
       if(sml1[i][j]==1)
  putimage(j*25+362,i*25+50,mask,XOR_PUT);
       sml1[i][j]=brk[n][k++];
       if(sml1[i][j]==1)
  putimage(j*25+362,i*25+50,mask,XOR_PUT);
      }
}
 void frame3D(int left,int top,int right,int bottom)
{setfillstyle(SOLID_FILL,LIGHTGRAY);
 bar(left,top,right,bottom);
 setcolor(WHITE);
 line(left,top,left,bottom);
 line(left,top,right,top);
 setcolor(BLACK);
 line(right,top,right,bottom);
 line(left,bottom,right,bottom);
 setcolor(BLACK);
 line(left+4,top+4,left+4,bottom-4);
 line(left+4,top+4,right-4,top+4);
 setcolor(WHITE);
 line(right-4,top+4,right-4,bottom-4);
 line(left+4,bottom-4,right-4,bottom-4);
}
 void bar3D(int left,int top,int right,int bottom)
{setfillstyle(SOLID_FILL,LIGHTGRAY);
 bar(left,top,right,bottom);
 setcolor(WHITE);
 line(left,top,left,bottom);
 line(left,top,right,top);
 setcolor(BLACK);
 line(right,top,right,bottom);
 line(left,bottom,right,bottom);
}
void outscore()
{ char *pp;
  pp=(char *)malloc(sizeof(char)*8);
  ltoa(score,pp,10);
  clrtext(470,210,24,100,LIGHTGRAY);
  setcolor(RED);
  settextstyle(1,0,1);
  outtextxy(470,214,pp);
  free(pp);
}
void clrtext(int x,int y,int high,int textlen,int color)
{
  setfillstyle(SOLID_FILL,color);
  bar(x,y,x+textlen,y+high);
}
 void init()                           /*初始化主窗口*/
{
 int gd=DETECT,gm,i,j;
 initgraph(&gd,&gm,"");
 cleardevice();
 setfillstyle(9,BCOLOR);
 bar(0,0,25,25);
 mask=(char *)malloc(imagesize(0,0,25,25)+1);
 getimage(0,0,25,25,mask);
 getbmp("boat.bmp",0,0);
 bar3D(98,48,351,426);
 for(i=0;i<COLUMNS;i++)                /*画主窗口*/
  for(j=0;j<ROWS;j++)
   bar3D(i*25+100,j*25+50,i*25+24+100,j*25+24+50);
 bar3D(360,48,464,152);
 for(i=0;i<4;i++)                      /*画欲览窗口*/
  for(j=0;j<4;j++)
   bar3D(i*25+362,j*25+50,i*25+24+362,j*25+24+50);
 bar3D(360,180,580,426);
 gethzm();
 settextstyle(1,0,1);
 setcolor(LIGHTBLUE);
 disp_hz(370,190,1,4,LIGHTGREEN);
 disp_hz(370,220,5,4,LIGHTGREEN);
 disp_hz(370,250,11,2,LIGHTGREEN);
 settextstyle(1,0,1);
 setcolor(BLACK);
 setfillstyle(SOLID_FILL,BLACK);
 fillellipse(470,320,59,15);
 setfillstyle(SOLID_FILL,YELLOW);
 fillellipse(470,320,55,13);
 setcolor(WHITE);
 ellipse(470,320,0,360,57,14);
 setcolor(BLACK);
 outtextxy(363,400,"          By suprman 99.");
 setcolor(YELLOW);
 outtextxy(361,398,"          By suprman 99.");
 getmaxscore();
 for(i=0;i<=COLUMNS+1;i++)                /*初始化控制数组*/
   {
     brick[0][i]=1;
     brick[ROWS+1][i]=1;
    }
 for(j=0;j<=ROWS+1;j++)
   {
     brick[j][0]=1;
     brick[j][COLUMNS+1]=1;
    }
 for(i=1;i<=ROWS;i++)
   for(j=1;j<=COLUMNS;j++)
     brick[i][j]=0;
}
 void getmaxscore()
{
 FILE *fppp;
 char n[10]="0";
 long m=0;
 fppp=fopen("score.dat","r");
 if(fppp!=NULL)
  {
   fgets(n,10,fppp);
   maxscore=atol(n);
   m=maxscore/100;
   if((m%20)==(maxscore%100))
    maxscore=~(m-1234);
   else
    maxscore=0;
  }
 else
  {
   fppp=fopen("score.dat","w");
   maxscore=0;
   }
  ltoa(maxscore,n,10);
  setcolor(RED);
  outtextxy(470,188,n);
  fclose(fppp);
}
 void disp_hz(x,y,wz,num,color)                /*显示汉字*/
 int x,y,wz,color,num;
  {
   int i,j,k;
   wz--;
   for(i=0;i<num;i++)
    for(j=0;j<16;j++)
    {
     for(k=0;k<8;k++)
      {
       if((hzmleft[wz+i][j]>>(7-k))&1)
 {
  putpixel(x+16*i+k,y+j,BLACK);
  putpixel(x+16*i+k-1,y+j-1,color);
 }
      }
     for(k=0;k<8;k++)
      {
 if((hzmright[wz+i][j]>>(7-k))&1)
 {
  putpixel(x+16*i+k+8,y+j,BLACK);
  putpixel(x+16*i+k+7,y+j-1,color);
 }
      }
    }
  }
 void getbmp(fn,x,y)
 char *fn;
 int x,y;
{
 struct bmpheader
  {
   char s[2];
   unsigned long bfsize;
   unsigned long res;
   unsigned long bfoffbits;
   unsigned long bisize;
   unsigned long width;
   unsigned long height;
   }bh;
 FILE *fp;
 int c,off_set,startdot=0,enddot=0,color;
 long rows=0,linelong;
 fp=fopen(fn,"rb");
 if(fp!=NULL)
  {
   fread(&bh,sizeof(struct bmpheader),1,fp);
   linelong=(bh.bfsize-bh.bfoffbits)/bh.height;
   off_set=(2*linelong-bh.width)/2;
   fseek(fp,bh.bfoffbits,SEEK_SET);
   c=getc(fp);
   color=c/16;
   fseek(fp,-1,SEEK_CUR);
   while((c=fgetc(fp))!=EOF)
       {
 if((c/16)!=color)
  {
          setcolor(color);
   line(x+startdot,y+bh.height-rows,x+enddot,y+bh.height-rows);
   enddot++;
   startdot=enddot;
   color=(c/16);
   }
  else
   {
    enddot++;
   }
 if(enddot==bh.width)
  {
   setcolor(color);
   line(x+startdot,y+bh.height-rows,x+enddot-1,y+bh.height-rows);
   startdot=0;
   enddot=0;
   rows++;
   fseek(fp,off_set,SEEK_CUR);
   c=getc(fp);
   color=c/16;
   fseek(fp,-1,SEEK_CUR);
  }
 else
  {
   if((c%16)!=color)
    {
     setcolor(color);
     line(x+startdot,y+bh.height-rows,x+enddot,y+bh.height-rows);
     enddot++;
     startdot=enddot;
     color=(c%16);
    }
   else
    {
     enddot++;
    }
   if(enddot==bh.width)
    {
     setcolor(color);
     line(x+startdot,y+bh.height-rows,x+enddot-1,y+bh.height-rows);
     startdot=0;
     enddot=0;
     rows++;
     fseek(fp,off_set,SEEK_CUR);
            c=getc(fp);
     color=c/16;
     fseek(fp,-1,SEEK_CUR);
    }
  }
 }
  }
  fclose(fp);
}
 void gethzm()                           /*获得汉字字模*/
{
  FILE *fp;
  int i,j,c,cc_num=0;
  fp=fopen("hz","rb");
 for(i=0;i<150;i++)
  {
   for(j=0;j<16;j++)
    {
     c=fgetc(fp);
     if(feof(fp))break;
     hzmleft[i][j]=c;
     hzmright[i][j]=fgetc(fp);
     }
     if(feof(fp))break;
     cc_num++;
   }
}
int get_key()
{
  union key1{
       char ch[2];
       int int_value;
      }key2;
  int key=0;
  delay(200);
  while(!bioskey(1))return -1;
  key2.int_value=bioskey(0);
  if(key2.ch[0]==0)key=key2.ch[1]+256;
  else key=key2.ch[0];
  return key;
}
void clrkey()                       /*清键盘缓冲区*/
{
  union REGS r;
  r.h.ah=0x0c;
  intdos(&r,&r);
}
 


<script type="text/javascript"> google_ad_client = "pub-2416224910262877"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_channel = ""; google_color_border = "E1771E"; google_color_bg = "FFFFFF"; google_color_link = "0000FF"; google_color_text = "000000"; google_color_url = "008000"; </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
发布了135 篇原创文章 · 获赞 162 · 访问量 171万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览