大二时实现的俄罗斯方块

作者在整理硬盘时发现了大学时期编写的俄罗斯方块游戏源代码,并对其进行了回顾。该程序使用TC2.0开发工具编写,通过本文档可以了解到游戏的基本逻辑和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


整理硬盘时,找到了大二时实现的俄罗斯方块的代码。
最后修改时间是2003年4月17日16点28分。
佩服当时的自己,虽然代码写的有点稚嫩,但算法还是比较不错。
现在我估计也写不出这样的代码了。
当时用的开发工具是TC2.0, 原封不动贴出来备份一下。

#define TRUE 1
#define FALSE 0

#include
<stdio.h>
#include
<stdlib.h>
#include
"lib.h"

unsigned 
char block[7][4][2]={{{2,2},{1,2},{3,2},{4,2}},  /*  ---- */
                  {{
2,2},{1,2},{3,2},{3,1}},  /*  ___| */
                  {{
2,2},{1,1},{1,2},{3,2}},  /* |___  */
                  {{
2,2},{1,1},{2,1},{3,2}},  /*   z   */
                  {{
2,2},{1,2},{2,1},{3,1}},  /*  -z   */
                  {{
1,1},{2,1},{1,2},{2,2}},  /*  88   */
                  {{
2,2},{1,2},{2,1},{3,2}}}; /*  _|_   */
char sc[12][22];
char cur[4][2],next[4][2];
char stop=0;
int  score=0;
char gameover=0;
char cu,ne;


 main()
{
 
int i,k;
 unsigned 
long int count=0;
 clrscr();
 init();
 randomize();
 cu
=random(7);
/* cu=0;  */
 
for(i=0;i<4;i++) {
   cur[i][
0]=block[cu][i][0]+5;
   cur[i][
1]=block[cu][i][1]+1;}
 Random();
 draw(cur,
254);
 
while(!gameover){
   
if(bioskey(1)) {
      k
=getkey();
      
switch(k){
    
case UP:   turn(cu);break;
    
case DOWN :if(legal('d')){
               draw(cur,
' ');
               
for(i=0;i<4;i++)
             cur[i][
1]++;
               draw(cur,
254);}
            
break;
    
case LEFT: if(legal('l')) {
               draw(cur,
' ');
               
for(i=0;i<4;i++)
              cur[i][
0]--;
               draw(cur,
254); }
              
break;
    
case RIGHT: if(legal('r')) {
             draw(cur,
' ');
             
for(i=0;i<4;i++)
                 cur[i][
0]++;
             draw(cur,
254);}
             
break;
    
case ESC :gameover=1;break;
    
case ' ' :getkey();break;}
    }
/* if(kbhit())    */
   count
++;
  
if(count==90000) {
   count
=0;
   
for(i=0;i<4;i++){
    
if(cur[i][1]==23) {
             stop
=1;
              
break;}
   
else if(sc[cur[i][0]-2][cur[i][1]-1]==1){ /* next pos whether 1 */
       stop
=1;
       
break;}}
   
if(!stop) {
          draw(cur,
' ');
          
for(i=0;i<4;i++)
        cur[i][
1]++;
          draw(cur,
254);}
  
else {
     
for(i=0;i<4;i++)
       sc[cur[i][
0]-2][cur[i][1]-2]=1;
     check();
     
for(i=0;i<4;i++) {
       cur[i][
0]=next[i][0]+5;
       cur[i][
1]=next[i][1]+1;}
     cu
=ne;
     Random();
     stop
=0;
     draw(cur,
254);
     }
  }
  }
 gotoxy(
3,11);
 printf(
"Game Over");
 sleep(
1);
 }
turn(
char c)
{
 
char dx,dy,i;
 
char temp[4][2];
 
if(c==5)
   
return;
 
for(i=1;i<4;i++){
      dx
=cur[0][0]-cur[i][0];
      dy
=cur[0][1]-cur[i][1];
      temp[i][
0]=cur[0][0]-dy;
      temp[i][
1]=cur[0][1]+dx;
      
if(sc[temp[i][0]-2][temp[i][1]-2]||temp[i][0]<2||temp[i][0]>13||temp[i][1]<2||temp[i][1]>23)
         
return;
      }
  draw(cur,
' ');
  
for(i=1;i<4;i++) {
      cur[i][
0]=temp[i][0];
      cur[i][
1]=temp[i][1];}
  draw(cur,
254);
 }

init()
{
int i,j;
 SetCursorType(NOCURSOR);
 
for(i=0;i<12;i++)
   
for(j=0;j<22;j++)
     sc[i][j]
=0;
 Bar(
1,1,14,24);
 gotoxy(
16,3);
 printf(
"Next :");
 gotoxy(
16,6);
 printf(
"Score : 0");
 gotoxy(
16,8);
 printf(
"ESC-quit");
 gotoxy(
16,10);
 printf(
"SPACE-pause");
}

draw(
char ch[][2],int a)
{
 
int i;
  
for(i=0;i<4;i++) {
    gotoxy(ch[i][
0],ch[i][1]);
    putch(a);}
 }
Random()
{
  
char i;
  ne
=random(7);
/*  ne=0;*/
  
for(i=0;i<4;i++) {
    next[i][
0]=block[ne][i][0];
    next[i][
1]=block[ne][i][1];}
  
for(i=0;i<4;i++) {
    gotoxy(
24+i,3);
    putch(
' ');
    gotoxy(
24+i,4);
    putch(
' ');}
  
for(i=0;i<4;i++) {
    gotoxy(next[i][
0]+23,next[i][1]+2);
    putch(
254);}
}
 legal(
char b)
{
int i;
 
if(b=='l') {
  
for(i=0;i<4;i++)
    
if(sc[cur[i][0]-3][cur[i][1]-2]||cur[i][0]==2)
     
return FALSE;}
 
else if(b=='r') {
   
for(i=0;i<4;i++)
     
if(sc[cur[i][0]-1][cur[i][1]-2]||cur[i][0]==13)
       
return FALSE;}
 
else if(b=='d') {
    
for(i=0;i<4;i++)
      
if(sc[cur[i][0]-2][cur[i][1]-1]||cur[i][1]==23)
     
return FALSE;}
 
else return TRUE;
}

 check()
int i,j,k,m,p;
  
char c,temp[4];
 
/* for(i=0;i<4;i++) {
     temp[i]=cur[i][1];
  }
*/
  temp[
0]=cur[0][1];
  temp[
1]=cur[1][1];
  temp[
2]=cur[2][1];
  temp[
3]=cur[3][1];
  c
=1;
  
for(i=0;i<4;i++) {
    p
=1;
    
for(j=0;j<12;j++)     /* check whether the line is full */
      
if(!sc[j][temp[i]-2]){
     p
=0;
     
break;
    }
    
if(p) {
       score
+=(100*c);
       c
++;
       
for(k=temp[i];k>2;k--)     /*  delete a line  */
      
for(m=0;m<12;m++) {
         sc[m][k
-2]=sc[m][k-3];
         gotoxy(m
+2,k);
         
if(sc[m][k-2])   putch(254);
         
else             putch(' ');
       }
       printf(
"/7");
       
for(k=i;k<4;k++) {
     
if(temp[k]<temp[i])
     temp[k]
++;}
       }
    }
    gotoxy(
24,6);
    printf(
"%d",score);
    
for(i=0;i<4;i++)
     
if(sc[cur[i][0]-2][0]){
    gameover
=1;
    
break;}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值