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

📄

📁 这是一个俄罗斯方块的C程序
💻
字号:
#include<stdio.h>
#include<graphics.h>
#include<bios.h>
#include<stdlib.h>
#define LEFT 1
#define RIGHT 2
#define DOWN 3
#define CHANGE 4
#define FASTDOWN 5
#define MAXY 30
#define MAXX 15
#define SCREENX 50
#define SCREENY 0
#define BLOCKSIZE 12
int shape[7][4][4][4]=
     {{{{1,1,0,0},{0,1,0,0},{0,1,0,0},{0,0,0,0}}
     ,{{0,0,0,0},{0,0,1,0},{1,1,1,0},{0,0,0,0}}
     ,{{1,0,0,0},{1,0,0,0},{1,1,0,0},{0,0,0,0}}
     ,{{1,1,1,0},{1,0,0,0},{0,0,0,0},{0,0,0,0}}},
     {{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
     ,{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
     ,{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
     ,{{1,1,0,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}},
     {{{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}
     ,{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}
     ,{{1,0,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}
     ,{{0,1,1,0},{1,1,0,0},{0,0,0,0},{0,0,0,0}}},
     {{{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}
     ,{{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}
     ,{{0,1,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}
     ,{{1,1,0,0},{0,1,1,0},{0,0,0,0},{0,0,0,0}}},
     {{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}
     ,{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}
     ,{{0,0,0,0},{1,1,1,1},{0,0,0,0},{0,0,0,0}}
     ,{{0,1,0,0},{0,1,0,0},{0,1,0,0},{0,1,0,0}}},
     {{{1,1,0,0},{1,0,0,0},{1,0,0,0},{0,0,0,0}}
     ,{{1,0,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}
     ,{{0,1,0,0},{0,1,0,0},{1,1,0,0},{0,0,0,0}}
     ,{{1,1,1,0},{0,0,1,0},{0,0,0,0},{0,0,0,0}}},
     {{{0,1,0,0},{1,1,1,0},{0,0,0,0},{0,0,0,0}}
     ,{{1,0,0,0},{1,1,0,0},{1,0,0,0},{0,0,0,0}}
     ,{{1,1,1,0},{0,1,0,0},{0,0,0,0},{0,0,0,0}}
     ,{{0,1,0,0},{1,1,0,0},{0,1,0,0},{0,0,0,0}}}};
int shapeall=0,subshape=0,nextshape,nextsubshape;
int stack[MAXX+1][MAXY+1];
int x=5,y=0,p,q;
int prex,preshape,prey,stop=0;
int speed=3,death=0,prespeed,score=0,subscore,quit=0,color;
void Delay(float clicks){
 unsigned int far *clock=(unsigned int far *)0x00000046CL;
 unsigned int now;
 now=*clock;
 while(abs(*clock-now)<clicks){}
}
void piantou(){
 int i,col;
   settextstyle(0,0,1);
   setcolor(LIGHTBLUE);
   outtextxy(400,350,"Made by Zhang Xinfeng!");
   outtextxy(400,370,"My oicq is 6372793!");
   settextstyle(0,0,1);
   outtextxy(150,200,"Xiao Xia,I miss you very much!");
 settextstyle(0,0,2);
 outtextxy(100,300,"Press any key to start");
 outtextxy(150,320,"Press Q to quit!");
 for(i=1;i<=10&&!kbhit();i++){
  settextstyle(0,0,i);
  col=random(16);
  while(col==BLACK) col=random(16);
  setcolor(col);
  outtextxy(100,100,"ECUST");
  Delay(5);
  setcolor(BLACK);
  outtextxy(100,100,"ECUST");
  }
        col=random(16);
 while(col==BLACK) col=random(16);
 setcolor(col);
 outtextxy(100,100,"ECUST"); 

 for(i=0;!kbhit();i+=2) {
  settextstyle(0,0,10);
  col=random(16);
  while(col==BLACK) col=random(16);
  Delay(2);
  setcolor(col);
  outtextxy(100,100,"ECUST");
  }
 nosound();
}

void window(int x1,int y1,int x2,int y2){
 setfillstyle(1,WHITE);
 bar(x1,y1-25,x2,y2);

 setfillstyle(1,LIGHTGRAY);
 bar(x1+1,y1-25+1,x2,y2);

 setfillstyle(1,BLUE);
 bar(x1+3,y1-25+3,x2-3,y1-25+20);

 setfillstyle(1,LIGHTBLUE);
 bar(x1+3,y1-25+23,x2-3,y2-3);

 }
void chacha(int x1,int y1,int x2,int y2,int press){

if(press==1) setfillstyle(1,BLACK);
else   setfillstyle(1,WHITE);
 bar(x2-17,y1+5,x2-5,y1+18);

 setfillstyle(1,LIGHTGRAY);
 bar(x2-16,y1+6,x2-5,y1+18);

 setwritemode(EMPTY_FILL);
 setcolor(BLACK);
 setlinestyle(0,0,3);
 line(x2-14,y1+8,x2-8,y1+16);
 line(x2-8,y1+8,x2-14,y1+16);
}

void realwindow(int x1,int y1,int x2,int y2){
 int temp;

 if(x1>x2) {
   temp=x1;
   x1=x2;
   x2=temp;
   }
 if(y1>y2){
   temp=y1;
   y1=y2;
   y2=temp;
   }

 window(x1,y1,x2,y2);
 chacha(x1,y1-25,x2,y2-25,0);

}


int check(){
int i,j;
for(i=0;i<=3;i++)
 for(j=0;j<=3;j++)
 if(shape[shapeall][preshape][j]==1&&stack[prex+j][prey+i]==1)
  return(0);
return(1);
}

void pop(){
int i,j;
for(i=0;i<=3;i++)
 for(j=0;j<=3;j++)
 if(shape[shapeall][subshape][j]==1) {
  stack[x+j][y+i]=0;
  setfillstyle(SOLID_FILL,LIGHTBLUE);
  bar((x+j)*BLOCKSIZE,(y+i)*BLOCKSIZE+30,(x+j)*BLOCKSIZE+10,(y+i)*BLOCKSIZE+40);
  }
 }

void push(){
int i,j;
for(i=0;i<=3;i++)
 for(j=0;j<=3;j++)
 if(shape[shapeall][subshape][j]==1) {
  stack[x+j][y+i]=1;
  setfillstyle(SOLID_FILL,LIGHTGRAY);
  bar((x+j)*BLOCKSIZE,(y+i)*BLOCKSIZE+30,(x+j)*BLOCKSIZE+10,(y+i)*BLOCKSIZE+40);
  if(shapeall+5==LIGHTBLUE)  color=YELLOW;
  else color=shapeall+5;
  setfillstyle(SOLID_FILL,color);
  bar((x+j)*BLOCKSIZE+1,(y+i)*BLOCKSIZE+30+1,(x+j)*BLOCKSIZE+10,(y+i)*BLOCKSIZE+40);
  }
}

void poprow(int row){
int m,n;
for(m=row;m>=1;m--)
 for(n=0;n<=MAXX;n++)
  stack[n][m]=stack[n][m-1];
}

void display(){
int i,j;
 realwindow(9,29,MAXX*BLOCKSIZE+1,MAXY*BLOCKSIZE+31);
 setfillstyle(1,LIGHTBLUE);
 bar(290,0,3*BLOCKSIZE+320,3*BLOCKSIZE+50);
for(i=1;i<=MAXY-1;i++)
 for(j=1;j<=MAXX-1;j++)
 if(stack[j]==1){
         setfillstyle(SOLID_FILL,WHITE);
  bar(j*BLOCKSIZE,i*BLOCKSIZE+30,j*BLOCKSIZE+10,i*BLOCKSIZE+40);
  setfillstyle(SOLID_FILL,DARKGRAY);
  bar(j*BLOCKSIZE+1,i*BLOCKSIZE+30+1,j*BLOCKSIZE+10,i*BLOCKSIZE+40);
  }
}
void end(){
char cntrl;
 setfillstyle(SOLID_FILL,LIGHTBLUE);
 bar(0,0,1000,1000);
while(!kbhit()){
 setcolor(WHITE);
 settextstyle(0,0,5);
 outtextxy(50,100,"YOU ARE DEAD!!");
 setcolor(random(15));
 settextstyle(0,0,1);
 outtextxy(200,200,"Press R to restart");
 Delay(1);
 }
cntrl=getch();
if(cntrl=='r')
 quit=0;
else
 quit=1;
}

void clearbonus(){
 int bonus[MAXY],i,j,hs;
 hs=0;
 subscore=0;
 for(i=0;i<=MAXY;i++) bonus=0;
 for(j=MAXY-1;j>=0;j--)
  for(i=1;i<=MAXX-1;i++)
   bonus[j]=bonus[j]+stack[j];
 for(i=0;i<=MAXY;i++){
  if(bonus>=MAXX-1) {
   poprow(i);
   display();
   hs=hs+1;
   }
  if(bonus[1]!=0) death=1;
   }
 switch(hs){
  case 1:subscore=50;break;
  case 2:subscore=150;break;
  case 3:subscore=250;break;
  case 4:subscore=350;break;
  }

}

void down(int speedr){
 prey=y+1;
 if(!check()) stop=1;
 else{
  y=prey;
  push();
  Delay(speedr);
  }
 if(!stop) pop();
 else      push();
}

void smallwindow(){
 setcolor(LIGHTGRAY);
 settextstyle(0,0,2);
 outtextxy(270,0,"Next Shape is:");
 setfillstyle(1,WHITE);
 bar(288,18,3*BLOCKSIZE+320+2,3*BLOCKSIZE+50+2);
 setfillstyle(1,LIGHTGRAY);
 bar(288,19,3*BLOCKSIZE+320+2,3*BLOCKSIZE+50+2);
 setfillstyle(1,DARKGRAY);
 bar(289,19,3*BLOCKSIZE+320,3*BLOCKSIZE+50);
 setfillstyle(1,WHITE);
 bar(290,20,3*BLOCKSIZE+320+1,3*BLOCKSIZE+50+1);
 setfillstyle(1,LIGHTBLUE);
 bar(290,20,3*BLOCKSIZE+320,3*BLOCKSIZE+50);
 }

void clearkeybuffer(){
 int offset;
 offset=peek(0x40,0x1a);
 pokeb(0x40,0x1c,offset);
 }

void control(){
 char cntrl=NULL,oldcntrl;
 int i;
 int directions=DOWN;
 while(!quit&&!death){
  switch(cntrl){
   case 'a':
   case 'A': directions=LEFT;break;
   case 'D':
   case 'd': directions=RIGHT;break;
   case 'S':
   case 's': directions=FASTDOWN;break;
   case 'H':
   case 'h': directions=CHANGE;break;
   default:  directions=DOWN;break;
   }
  switch(directions){
   case FASTDOWN:
    down(1);
    while(!kbhit()&&!stop) down(speed);
     break;
   case DOWN:
    while(!kbhit()&&!stop)  down(speed);
     break;
   case LEFT:
    pop();
    prex-=1;
    if(check()) x=prex;
    else prex+=1;
    while(!kbhit()&&!stop)  down(speed);
     break;
   case RIGHT:
    pop();
    prex+=1;
    if(check()) x=prex;
    else prex-=1;
    while(!kbhit()&&!stop) down(speed);
     break;
   case CHANGE:
    pop();
    preshape=subshape;
    preshape++;
    preshape=preshape%4;
    if(check()) subshape=preshape;
    else preshape=subshape;
    while(!kbhit()&&!stop) down(speed);
    break;
   }
if(stop){
   shapeall=nextshape;
   x=5;
   y=0;
   prex=x;
   prey=y;
   subshape=nextsubshape;
   preshape=subshape;
   stop=0;
   clearbonus();
   setfillstyle(SOLID_FILL,LIGHTBLUE);
   smallwindow();
   score=score+subscore;
   gotoxy(32,25);
   printf("Your score is:%d",score);
   nextshape=random(7);
   nextsubshape=random(4);
   for(p=0;p<=3;p++)
    for(q=0;q<=3;q++)
     if(shape[nextshape][nextsubshape][q][p]==1){
      setfillstyle(SOLID_FILL,WHITE);
      bar(q*BLOCKSIZE+300,p*BLOCKSIZE+30,q*BLOCKSIZE+10+300,p*BLOCKSIZE+40);
      if(nextshape+5==LIGHTBLUE)  color=YELLOW;
       else color=nextshape+5;
      setfillstyle(SOLID_FILL,color);
      bar(q*BLOCKSIZE+1+300,p*BLOCKSIZE+30+1,q*BLOCKSIZE+10+300,p*BLOCKSIZE+40);
      }
   }
else {
 cntrl=getch();
 if(cntrl=='S'||cntrl=='s')  clearkeybuffer();
 if(cntrl=='q'||cntrl=='Q') quit=1;
 }
 }
 if (death==1)  end();
}

void main(){
 int i,j;
 int gdriver=DETECT,gmode;
 registerbgidriver(EGAVGA_driver);
 initgraph(&gdriver,&gmode,"");
 piantou();
while(!quit){
 stop=0;
 death=0;
 randomize();
 setfillstyle(SOLID_FILL,BLACK);
 bar (0,0,700,500);
 gotoxy(32,25);
 printf("Your score is:%d",score);
 shapeall=random(7);
 subshape=random(4);
 realwindow(9,29,MAXX*BLOCKSIZE+1,MAXY*BLOCKSIZE+31);
 smallwindow();
 settextstyle(3,0,2);
 outtextxy(250,130,"Control keys:");
 settextstyle(3,0,6);
 outtextxy(250,150,"A");
 settextstyle(0,0,2);
 outtextxy(300,175,"left");
 settextstyle(3,0,6);
 outtextxy(420,150,"S");
 settextstyle(0,0,2);
 outtextxy(470,175,"right");
 settextstyle(3,0,6);
 outtextxy(350,200,"H");
 settextstyle(0,0,2);
 outtextxy(400,225,"change");

 nextshape=random(7);
 nextsubshape=random(4);
 for(p=0;p<=3;p++)
  for(q=0;q<=3;q++)
   if(shape[nextshape][nextsubshape][q][p]==1){
    setfillstyle(SOLID_FILL,WHITE);
    bar(q*BLOCKSIZE+300,p*BLOCKSIZE+30,q*BLOCKSIZE+10+300,p*BLOCKSIZE+40);
    if(nextshape+5==LIGHTBLUE)  color=YELLOW;
    else color=nextshape+5;
    setfillstyle(SOLID_FILL,color);
    bar(q*BLOCKSIZE+1+300,p*BLOCKSIZE+30+1,q*BLOCKSIZE+10+300,p*BLOCKSIZE+40);
    }
 prex=x;
 prey=y;
 preshape=subshape;

 for(i=0;i<=MAXX;i++)
  for(j=0;j<=MAXY;j++) stack[j]=0;
 for(i=0;i<=MAXX;i++)    stack[MAXY]=1;
 for(j=0;j<=MAXY;j++) {
  stack[0][j]=1;
  stack[MAXX][j]=1;
  }

 control();
 }
}

⌨️ 快捷键说明

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