C言語のテトリスのゲームの課程の設計
本論文の例では、C言語によるテトリスゲームの実現のための具体的なコードを共有しています。
1、設計プロセス
2、関連プログラム
1、設計プロセス
2、関連プログラム
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
#include<time.h>
#include<dos.h>
#include<bios.h>
#define LEFT 0x4b00 /* */
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
#define TIMER 0x1c /* */
struct Snow
{
int x;
int y;
int speed;
}snow[100];
typedef struct
{
int box[4][4];
int color;
int next;
}SHAPE;
int x=0,y=4,form[16][12]={ /*x,y */
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1},
}; /* */
SHAPE shapes[19]={
{1,0,0,0,
1,0,0,0,
1,1,0,0,
0,0,0,0,CYAN,1},
{1,1,1,0,
1,0,0,0,
0,0,0,0,
0,0,0,0,CYAN,2},
{1,1,0,0,
0,1,0,0,
0,1,0,0,
0,0,0,0,CYAN,3},
{0,0,0,0,
0,0,1,0,
1,1,1,0,
0,0,0,0,CYAN,0},
{0,1,0,0,
0,1,0,0,
1,1,0,0,
0,0,0,0,MAGENTA,5},
{1,0,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0,MAGENTA,6},
{1,1,0,0,
1,0,0,0,
1,0,0,0,
0,0,0,0,MAGENTA,7},
{1,1,1,0,
0,0,1,0,
0,0,0,0,
0,0,0,0,MAGENTA,4},
{1,0,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0,YELLOW,9},
{0,1,1,0,
1,1,0,0,
0,0,0,0,
0,0,0,0,YELLOW,8},
{0,1,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0,BROWN,11},
{1,1,0,0,
0,1,1,0,
0,0,0,0,
0,0,0,0,BROWN,10},
{0,1,0,0,
1,1,1,0,
0,0,0,0,
0,0,0,0,WHITE,13},
{1,0,0,0,
1,1,0,0,
1,0,0,0,
0,0,0,0,WHITE,14},
{1,1,1,0,
0,1,0,0,
0,0,0,0,
0,0,0,0,WHITE,15},
{0,1,0,0,
1,1,0,0,
0,1,0,0,
0,0,0,0,WHITE,12},
{1,0,0,0,
1,0,0,0,
1,0,0,0,
1,0,0,0,RED,17},
{1,1,1,1,
0,0,0,0,
0,0,0,0,
0,0,0,0,RED,16},
{1,1,0,0,
1,1,0,0,
0,0,0,0,
0,0,0,0,BLUE,18}
};
int TimerCounter=0;
int snownum=0;
int size;
int change1=10;
int annal[4][2],score=0,level=0,color,Boxnumber;
void plot();
void operation();
void *save1,*save2;
void Copy();
void DrawSnow();
void Pr();
void interrupt ( *oldhandler)();
void interrupt newhandler( )
{
TimerCounter++;
TimerCounter==36;
oldhandler();
}
void SetTimer(void interrupt (*IntProc)())
{
oldhandler=getvect(TIMER);
disable(); /* , */
setvect(TIMER,IntProc);
enable(); /* */
}
void KillTimer()
{
disable();
setvect(TIMER,oldhandler);
enable();
}
void main()
{
int gdriver=DETECT,gmode;
SetTimer(newhandler); /* */
registerbgidriver(EGAVGA_driver);
initgraph(&gdriver,&gmode,"c:\\turboc2");
Copy();
DrawSnow();
closegraph();
initgraph(&gdriver,&gmode,"E:\\TC20H\\INCLUDE\\GRAPHICS.H"); /* */
plot();
operation();
getch();
}
void Copy()
{
setcolor(0);
setfillstyle(SOLID_FILL,15);
fillellipse(200,200,4,4);
size=imagesize(196,196,204,204);
save1=malloc(size);
save2=malloc(size);
getimage(196,196,204,204,save1);
getimage(96,96,104,104,save2);
}
void Pr()
{
int fr[]={392,392,440,294,262,262,220,294,392,392,
440,532,440,392,262,262,220,294,392,294,
262,247,220,196,392,294,330,294,262,262,
220,294,330,294,262,294,22,247,220,196};
setcolor(change1/10);
settextstyle(0,0,3);
outtextxy(100,200,"Welcome to our Game!!!”);
sound(fr[change1/10]);
}
void DrawSnow()
{
int i;
int sx[62];
randomize();
for(i=0;i<62;i++)
sx[i]=(i+2)*10;
cleardevice();
while(!kbhit())
{
Pr();
if (snownum!=100)
{
snow[snownum].speed=2+random(5);
i=random(62);
snow[snownum].x=sx[i];
snow[snownum].y=10-random(100);
}
for(i=0;i<snownum;i++)
putimage(snow[i].x,snow[i].y,save2,COPY_PUT);
Pr();
if(snownum!=100)
snownum++;
/*delay(300);*/
setfillstyle(SOLID_FILL,15);
for(i=0;i<snownum;i++)
{
snow[i].y+=snow[i].speed;
putimage(snow[i].x,snow[i].y,save1,COPY_PUT);
if(snow[i].y>500)
snow[i].y=10-random(200);
}
change1++;
if(change1==140)
change1=10;
}
nosound();
}
void plot()
{
int i,j;
char ch1[]={24,'-','R','o','l','l','\0'},
ch2[]={25,'-','D','o','w','n','w','a','r','d','s','\0'},
ch3[]={26,'-','T','u','r','n',' ','L','e','f','t','\0'},
ch4[]={27,'-','T','u','r','n',' ','R','i','g','h','t','\0'};
setcolor(LIGHTGRAY);
rectangle(200,30,350,255);
i=0;
while((i++)<10)
line(200+i*15,30,200+i*15,255);
i=0;
while((i++)<15)
line(200,30+i*15,350,30+i*15);
setcolor(WHITE);
rectangle(170,270,390,330);
outtextxy(190,280,ch1);
outtextxy(270,280,ch2);
outtextxy(190,300,ch3);
outtextxy(290,300,ch4);
outtextxy(190,320,"Esc-Exit");
settextjustify(1,1);
outtextxy(390,50,"score");
outtextxy(390,100,"lexel");
outtextxy(390,150,"Next box");
outtextxy(390,65,"0");
outtextxy(390,115,"0");
}
void nextboxfun(SHAPE shapes[],int i)
{
int m,n;
for(m=0;m<4;m++)
for(n=0;n<4;n++)
if(shapes[i].box[m][n])
{
setfillstyle(1,shapes[i].color);
bar(370+n*15+1,180+m*15+1,370+n*15+15-1,180+m*15+15-1);
setcolor(LIGHTGRAY);
rectangle(370+n*15,180+m*15,370+n*15+15,180+m*15+15);
}
}
int Leftmobile()
{
int m,k=-1; /* annal , */
for(m=0;m<4;m++)
{
if(annal[m][0]!=k)
{
if(form[annal[m][0]][annal[m][1]-1])
return(0);
k=annal[m][0];
}
}
return(1);
}
int Rightmobile()
{
int m,k=-1;
for(m=3;m>=0;m--)
{
if(annal[m][0]!=k)
{
if(form[annal[m][0]][annal[m][1]+1])
return(0);
k=annal[m][0];
}
}
return(1);
}
int Downmobile()
{
int m;
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=2; /* 2 */
for(m=0;m<4;m++)
{
if(form[annal[m][0]+1][annal[m][1]]==1) /* 1 */
{
for(m=0;m<4;m++)/* */
form[annal[m][0]][annal[m][1]]=1;
return(0);
}
}
for(m=0;m<4;m++) /* */
form[annal[m][0]][annal[m][1]]=1;
return(1);
}
void LeftRedraw()
{
int m;
y--;
for(m=0;m<4;m++)
{
setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
annal[m][1]--;
form[annal[m][0]][annal[m][1]]=1;
setfillstyle(1,color);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
}
void RightRedraw()
{
int m;
y++;
for(m=0;m<4;m++)
{
setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
annal[m][1]++;
form[annal[m][0]][annal[m][1]]=1;
setfillstyle(1,color);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); } /* */
}
void DownRedraw()
{
int m,n;
x++;
for(m=0;m<4;m++)
{
setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++)
{
annal[m][0]++;
form[annal[m][0]][annal[m][1]]=1;
setfillstyle(1,color); bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
}
int UPmobile()
{
int m,n,k;
k=shapes[Boxnumber].next;/*k== */
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=2;
for(m=0;m<4;m++)
for(n=0;n<4;n++)
{
if(form[x+m][y+n]==1&&shapes[k].box[m][n])
{
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=1;/* 2 1*/
return(0);
}
}
for(m=0;m<4;m++)
form[annal[m][0]][annal[m][1]]=1;
return(1);
}
void change()
{
int m,n,k,i=0;
k=Boxnumber=shapes[Boxnumber].next;/* */
for(m=0;m<4;m++)
{
setfillstyle(1,BLACK);
bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1); /* */
}
for(m=0;m<4;m++) /* 0*/
form[annal[m][0]][annal[m][1]]=0;
for(m=0;m<4;m++) /* */
for(n=0;n<4;n++)
if(shapes[k].box[m][n])
{
annal[i][0]=x+m; /*annal */
annal[i][1]=y+n;
i++;
}
for(m=0;m<4;m++) /* 1 */
{
form[annal[m][0]][annal[m][1]]=1;
setfillstyle(1,color); bar(200+(annal[m][1]-1)*15+1,30+annal[m][0]*15+1,200+(annal[m][1]-1)*15+15-1,30+annal[m][0]*15+15-1) ;/* */
}
}
void FulllineJudge()
{
void *p1;
int m,n,i,k,p,q;
char *ch;
if(!(p1=malloc(imagesize(200,30,350,255))))
{
printf("
");
getch();
exit(1);
}
i=0;
for(m=14;m>=0;m--) /* */
{
for(n=1;n<=10;n++)
{
if(!form[m][n]) /* 0 */
break;
else if(n==10) /*1-10 1*/
{
i++; /* */
for(p=m;p>=1;p--)
for(q=1;q<=10;q++) /* 1 */
form[p][q]=form[p-1][q];
for(p=1;p<=10;p++) /* */
form[0][p]=0;
getimage(200,30,350,30+m*15,p1);
putimage(200,45,p1,0);
m++;
}
}
if(i==4) /* 4 */
break;
}
if(i==1) /* */
score+=10;
if(i==2)
score+=30;
if(i==3)
score+=60;
if(i==4)
score+=100;
setcolor(WHITE); /* */
sprintf(ch,"%d",score);
setfillstyle(1,BLACK);
bar(380,60,400,80);
outtextxy(390,65,ch);
level=score/500;
setfillstyle(1,BLACK);
bar(380,110,400,130);
sprintf(ch,"%d",level);
outtextxy(390,115,ch);
free(p1);
}
void operation()
{
int newbox,nextbox,m,n,k=1,i,KEY,l,o;
o=1;
srand((unsigned)time(NULL));
nextbox=rand()%19;
while(o)
{
if(k) /* */
{
x=0,y=4; /* x,y*/
Boxnumber=newbox=nextbox;
nextbox=rand()%19;
setfillstyle(1,BLACK);
bar(360,160,430,250);
nextboxfun(shapes,nextbox);
i=0;
color=shapes[newbox].color;
for(m=0;m<4;m++)
for(n=0;n<4;n++)
if(shapes[newbox].box[m][n]) /* annal */
{
annal[i][0]=0+m; /* */
annal[i][1]=4+n;
if(form[0+m][4+n])
{
setfillstyle(1,BLACK);
bar(240,130,310,150);
setcolor(RED);
outtextxy(275,140,"GAME OVER");
getch();
o=0;
}
form[0+m][4+n]=1;
setfillstyle(1,shapes[newbox].color);
bar(200+n*15+1+45,30+m*15+1,200+n*15+15-1+45,30+m*15+15-1);/* */
setcolor(LIGHTGRAY);
rectangle(200+n*15+45,30+m*15,200+n*15+15+45,30+m*15+15);
i++;
}
k=0;
}
if(bioskey(1)) /* */
KEY=bioskey(0);
else
KEY=0;
switch(KEY)
{
case LEFT:/* */
if(Leftmobile())
LeftRedraw();break;
case RIGHT:/* */
if(Rightmobile())
RightRedraw();break;
case DOWN:/* */
if(Downmobile())
DownRedraw();
else
{FulllineJudge();k=1;}break;
case UP:/* */
if(UPmobile())
change();break;
case ESC:o=0;break;
}
if(TimerCounter>(36-level*2)) /*TimerCounter 18*/
{
TimerCounter=0;
if(Downmobile()) /* */
DownRedraw();
else
{FulllineJudge();k=1;}
}
}
KillTimer();
}
以上が本文の全部です。皆さんの勉強に役に立つように、私たちを応援してください。