找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3253|回复: 1
收起左侧

51单片机+Proteus仿真的俄罗斯方块游戏程序设计

[复制链接]
ID:517871 发表于 2019-5-9 17:09 | 显示全部楼层 |阅读模式
俄罗斯方块仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
俄罗斯方块.png

单片机俄罗斯方块源程序如下:
  1. #include <AT89X51.H>

  2. #define DOWNTIME 30
  3. #define MAXHANG 20
  4. #define MAXLIE  16
  5. #define MAXPIX  3
  6. #define PUSHON  50

  7. #define LCD P2
  8. #define EN  P3_0
  9. #define RW  P3_1
  10. #define RS  P3_2
  11. #define CS1 P3_3
  12. #define CS2 P3_4

  13. #define KEYLEFT P1_0
  14. #define KEYDOWN P1_7
  15. #define KEYRIGH P1_6
  16. #define KEYROTATION P1_1

  17. unsigned char gkey=0xff,keystate=0,t0ms1=0,t0ms=0,downtimegap=0;
  18. unsigned char miao=0,fen=0;
  19. unsigned char downok;
  20. bit keyflag,timeupdate,fashionupdate;
  21. unsigned char idata cubeMap[MAXHANG][2];
  22. typedef struct{
  23.                unsigned char code * box;
  24.                             unsigned char cube : 4;
  25.                            unsigned char state : 4;
  26.                            char row;
  27.                            char column;
  28.                            } block;
  29. block this;
  30. unsigned int score=0;
  31. unsigned char speed=1;
  32. unsigned char code bittable[8]={1,2,4,8,0x10,0x20,0x40,0x80};
  33. unsigned char code cube[]=
  34. {
  35. /*  ■
  36.   ■■■
  37. */
  38. 0,4,0xe,0,  0,2,6,2,     0,7,2,0,     4,6,4,0,

  39. /*■
  40.   ■■■
  41. */
  42. 0,8,0xe,0,  0,4,4,0xc,   0,0,0xe,2,   0,6,4,4,
  43. /*■■■
  44.   ■   
  45. */
  46. 0,0xe,8,0,  0,4,4,6,     0,1,7,0,     6,2,2,0,
  47. /*■■
  48.     ■■
  49. */
  50. 0,0xc,6,0,  0,2,6,4,     0,6,3,0,     2,6,4,0,
  51. /*  ■■
  52.   ■■  
  53. */
  54. 0,6,0xc,0,  0,4,6,2,     0,3,6,0,     4,6,2,0,
  55. /*■■■■
  56. */
  57. 0,0xf,0,0,  4,4,4,4,     0,0,0xf,0,   2,2,2,2,
  58. /*■■
  59.   ■■
  60. */
  61. 0,6,6,0,    0,6,6,0,     0,6,6,0,     0,6,6,0
  62. };
  63. unsigned char code asii[]=
  64. {
  65.     0x3E,0x51,0x49,0x45,0x3E, // -0-
  66.     0x00,0x42,0x7F,0x40,0x00, // -1-
  67.     0x62,0x51,0x49,0x49,0x46, // -2-
  68.     0x21,0x41,0x49,0x4D,0x33, // -3-
  69.     0x18,0x14,0x12,0x7F,0x10, // -4-
  70.     0x27,0x45,0x45,0x45,0x39, // -5-
  71.     0x3C,0x4A,0x49,0x49,0x31, // -6-
  72.     0x01,0x71,0x09,0x05,0x03, // -7-
  73.     0x36,0x49,0x49,0x49,0x36, // -8-
  74.     0x46,0x49,0x49,0x29,0x1E, // -9-
  75.     0x00,0x36,0x36,0x00,0x00, // -:-10
  76. //next
  77.     0x7F,0x04,0x08,0x10,0x7F, // -N-11
  78.     0x7F,0x49,0x49,0x49,0x41, // -E-12
  79.     0x63,0x14,0x08,0x14,0x63, // -X-13
  80.     0x01,0x01,0x7F,0x01,0x01, // -T-14
  81. //speed
  82.     0x26,0x49,0x49,0x49,0x32, // -S-15
  83.     0x7F,0x09,0x09,0x09,0x06, // -P-16
  84.     0x7F,0x49,0x49,0x49,0x41, // -E-17
  85.     0x7F,0x41,0x41,0x41,0x3E, // -D-18
  86. //score
  87.     0x3E,0x41,0x41,0x41,0x22, // -C-19   
  88.     0x3E,0x41,0x41,0x41,0x3E, // -O-20
  89.     0x7F,0x09,0x19,0x29,0x46, // -R-21
  90.     0x00,0x00,0x00,0x00,0x00,  // - -22
  91. //GAME OVER
  92.     0x3E,0x41,0x51,0x51,0x72, // -G-23
  93.     0x7C,0x12,0x11,0x12,0x7C, // -A-24
  94.     0x7F,0x02,0x0C,0x02,0x7F, // -M-25
  95.     0x1F,0x20,0x40,0x20,0x1F, // -V-26
  96. //TIME
  97. //  0x00,0x41,0x7F,0x41,0x00  // -I-27
  98. };
  99. ////////////////////////////////////////////////////////////////////////////////
  100. void lcdCmd(unsigned char cmd)
  101. {
  102. bit ea;
  103. ea=EA;
  104. EA=0;
  105. EN=0;
  106. RW=0;
  107. RS=0;
  108. LCD=cmd;
  109. EN=1;
  110. EN=1;
  111. EN=0;
  112. EA=ea;
  113. }
  114. //-------------------------------------------------------------------------------
  115. void lcdWriteByte(unsigned char ch)
  116. {
  117. EN=0;
  118. RS=1;
  119. RW=0;
  120. LCD=ch;
  121. EN=1;
  122. EN=1;
  123. EN=0;
  124. }
  125. //--------------------------------------------------------------------------------
  126. void lcdSetPage(unsigned char page)
  127. {
  128.   page &=0x7;
  129.   page +=0xb8;
  130.   lcdCmd(page);
  131. }
  132. //--------------------------------------------------------------------------------
  133. void lcdSetColumn(unsigned char column)
  134. {
  135.   column &=0x3f;
  136.   column +=0x40;
  137.   lcdCmd(column);
  138. }
  139. //--------------------------------------------------------------------------------
  140. //character fron=5*8
  141. void lcdPlayChar(unsigned char index,unsigned char page,unsigned char colume)
  142. {
  143. unsigned char i,temp;
  144. unsigned int p;
  145. p=5*index;
  146. for(i=colume;i<colume+5;i++)
  147.   {
  148.     if(i<64)
  149.          {
  150.           CS1=1;
  151.           CS2=0;
  152.           temp=i;
  153.          }
  154.         else
  155.          {
  156.           CS1=0;
  157.           CS2=1;
  158.           temp=i-64;
  159.          }
  160.         lcdSetPage(page);
  161.         lcdSetColumn(temp);
  162.         lcdWriteByte(asii[p++]);
  163.   }
  164. }
  165. //---------------------------------------------------------------------------------
  166. //rectangle(3,0,50,60)
  167. void rectangle(void)
  168. {
  169.   unsigned char i,page;
  170.   CS1=1;
  171.   CS2=0;
  172.   lcdSetPage(0);  
  173.   lcdSetColumn(2);
  174.   EN=0;
  175.   RS=1;
  176.   RW=0;
  177.   LCD=0xff;
  178.   EN=1;
  179.   EN=1;
  180.   EN=0;
  181.   for(i=3;i<51;i++)
  182.    {
  183.      EN=0;
  184.      RS=1;
  185.      RW=0;
  186.      LCD=0x1;
  187.      EN=1;
  188.      EN=1;
  189.      EN=0;   
  190.    }
  191.   EN=0;
  192.   RS=1;
  193.   RW=0;
  194.   LCD=0xff;
  195.   EN=1;
  196.   EN=1;
  197.   EN=0;
  198. //---------------------------
  199. for(page=1;page<7;page++)
  200. {
  201.   lcdSetPage(page);  
  202.   lcdSetColumn(2);
  203.   EN=0;
  204.   RS=1;
  205.   RW=0;
  206.   LCD=0xff;
  207.   EN=1;
  208.   EN=1;
  209.   EN=0;
  210.   for(i=3;i<51;i++)
  211.    {
  212.      EN=0;
  213.      RS=1;
  214.      RW=0;
  215.      LCD=0x0;
  216.      EN=1;
  217.      EN=1;
  218.      EN=0;   
  219.    }
  220.   EN=0;
  221.   RS=1;
  222.   RW=0;
  223.   LCD=0xff;
  224.   EN=1;
  225.   EN=1;
  226.   EN=0;
  227. }
  228. //---------------------------
  229.   lcdSetPage(7);  
  230.   lcdSetColumn(2);
  231.   EN=0;
  232.   RS=1;
  233.   RW=0;
  234.   LCD=0x1f;
  235.   EN=1;
  236.   EN=1;
  237.   EN=0;
  238.   for(i=3;i<51;i++)
  239.    {
  240.      EN=0;
  241.      RS=1;
  242.      RW=0;
  243.      LCD=0x10;
  244.      EN=1;
  245.      EN=1;
  246.      EN=0;   
  247.    }
  248.   EN=0;
  249.   RS=1;
  250.   RW=0;
  251.   LCD=0x1f;
  252.   EN=1;
  253.   EN=1;
  254.   EN=0;
  255. }
  256. //--------------------------------------------------------------------
  257. //x:列;y行,页 3*3
  258. void lcdPutPix(unsigned char x, unsigned char y,unsigned char flag)
  259. {
  260.   unsigned char i,dat,bitmask,nextbit;
  261.   bit bflag,pflag,ea;
  262.   x=x*MAXPIX;
  263.   y=y*MAXPIX;
  264.   bflag=0;
  265.   pflag=0;
  266.   i=y%8;
  267.   if(i==0)
  268.    bitmask=0x7;
  269.   else if(i==1)
  270.    bitmask=0xe;
  271.   else if(i==2)
  272.    bitmask=0x1c;
  273.   else if(i==3)
  274.    bitmask=0x38;
  275.   else if(i==4)
  276.    bitmask=0x70;
  277.   else if(i==5)
  278.    bitmask=0xe0;
  279.   else if(i==6)
  280.    {
  281.     bflag=1;
  282.     bitmask=0xc0;
  283.     nextbit=1;
  284.    }
  285.   else if(i==7)
  286.    {
  287.     bflag=1;
  288.     bitmask=0x80;
  289.     nextbit=3;
  290.    }
  291.   if(x<62)
  292.    {
  293.     CS1=1;
  294.     CS2=0;
  295.    }
  296.   else if(x>63)
  297.    {
  298.     x-=64;
  299.     CS1=0;
  300.     CS2=1;
  301.    }
  302.   else
  303.    pflag=1;
  304.   lcdSetPage(y/8);
  305.   for(i=x;i<x+MAXPIX;i++)
  306.    {
  307.     if(pflag)
  308.      {
  309.       if(i==62 || i==63)
  310.        {
  311.          CS1=1;
  312.          CS2=0;
  313.          lcdSetPage(y/8);
  314.         }
  315.       else if(pflag && i==64)
  316.        {
  317.         CS1=0;
  318.         CS2=1;
  319.         lcdSetPage(y/8);
  320.        }
  321.       }
  322.      lcdSetColumn(i);
  323.      ea=EA;
  324.      EA=0;
  325.      EN=0;
  326.      LCD=0xff;
  327.      RS=1;
  328.      RW=1;
  329.      EN=1;
  330.      EN=0;

  331.      EN=1;
  332.      dat=LCD;
  333.      EN=0;
  334.      if(flag==1)
  335.        dat|=bitmask;
  336.      else
  337.        dat&=~bitmask;
  338.      lcdSetColumn(i);  

  339.      EN=0;
  340.      RW=0;
  341.      RS=1;
  342.      LCD=dat;
  343.      EN=1;
  344.      EN=1;
  345.      EN=0;
  346.      EA=ea;
  347.    }
  348. if(bflag)
  349.   {
  350.    lcdSetPage(y/8+1);
  351.    for(i=x;i<x+MAXPIX;i++)
  352.    {
  353.     if(pflag)
  354.      {
  355.       if(i==62 || i==63)
  356.        {
  357.         CS1=1;
  358.         CS2=0;
  359.         lcdSetPage(y/8+1);
  360.        }
  361.       else if(pflag && i==64)
  362.        {
  363.         CS1=0;
  364.         CS2=1;
  365.         lcdSetPage(y/8+1);
  366.        }
  367.       }
  368.      lcdSetColumn(i);
  369.      ea=EA;
  370.      EA=0;
  371.      EN=0;
  372.      LCD=0xff;
  373.      RS=1;
  374.      RW=1;
  375.      EN=1;
  376.      EN=0;

  377.      EN=1;
  378.      dat=LCD;
  379.      EN=0;
  380.      if(flag==1)
  381.        dat|=nextbit;
  382.      else
  383.        dat&=~nextbit;
  384.      lcdSetColumn(i);  

  385.      EN=0;
  386.      RW=0;
  387.      RS=1;
  388.      LCD=dat;
  389.      EN=1;
  390.      EN=1;
  391.      EN=0;
  392.      EA=ea;  
  393.   }
  394. }
  395. }
  396. //------------------------------------------------------------------
  397. void lcdClear(void)
  398. {
  399.   unsigned char i,page;
  400.   CS1=1;
  401.   CS2=0;
  402.   for(page=0;page<8;page++)
  403.    {
  404.     lcdSetPage(page);
  405.     lcdSetColumn(0);
  406.     for(i=0;i<64;i++)
  407.            lcdWriteByte(0);
  408.    }
  409.   CS1=0;
  410.   CS2=1;
  411.   for(page=0;page<8;page++)
  412.    {
  413.     lcdSetPage(page);
  414.     lcdSetColumn(0);
  415.     for(i=0;i<64;i++)
  416.            lcdWriteByte(0);
  417.    }
  418. }
  419. //-----------------------------------------------------------------
  420. #define STAR 53
  421. #define WIDE 6
  422. void lcdIni(void)
  423. {
  424. lcdCmd(0x3f);
  425. lcdCmd(0xc0);
  426. lcdClear();
  427. rectangle();
  428. //NEXT
  429. lcdPlayChar(11,0,STAR);
  430. lcdPlayChar(12,0,STAR+1*WIDE);
  431. lcdPlayChar(13,0,STAR+2*WIDE);
  432. lcdPlayChar(14,0,STAR+3*WIDE);
  433. //SPEED
  434. lcdPlayChar(15,3,STAR);
  435. lcdPlayChar(16,3,STAR+1*WIDE);
  436. lcdPlayChar(17,3,STAR+2*WIDE);
  437. lcdPlayChar(17,3,STAR+3*WIDE);
  438. lcdPlayChar(18,3,STAR+4*WIDE);
  439. //01
  440. lcdPlayChar(0,4,STAR+2*WIDE);
  441. lcdPlayChar(1,4,STAR+3*WIDE);

  442. //SCORE
  443. lcdPlayChar(15,5,STAR);
  444. lcdPlayChar(19,5,STAR+1*WIDE);
  445. lcdPlayChar(20,5,STAR+2*WIDE);
  446. lcdPlayChar(21,5,STAR+3*WIDE);
  447. lcdPlayChar(12,5,STAR+4*WIDE);

  448. lcdPlayChar(0,6,STAR+1*WIDE);
  449. lcdPlayChar(0,6,STAR+2*WIDE);
  450. lcdPlayChar(0,6,STAR+3*WIDE);
  451. lcdPlayChar(0,6,STAR+4*WIDE);
  452. //TIME
  453. lcdPlayChar(0,7,STAR);
  454. lcdPlayChar(0,7,STAR+1*WIDE);
  455. lcdPlayChar(10,7,STAR+2*WIDE);
  456. lcdPlayChar(0,7,STAR+3*WIDE);
  457. lcdPlayChar(0,7,STAR+4*WIDE);
  458. }
  459. //-----------------------------------------------------------------
  460. void showScoreSpeed(void)
  461. {
  462.   unsigned char num[5];
  463.   char i;
  464.   unsigned int temp;
  465.   temp=score;
  466.   for(i=0;i<5;i++)
  467.    {
  468.      num[i]=temp%10;
  469.          temp=temp/10;
  470.    }
  471.   for(i=4;i>0;i--)
  472.    {
  473.      if(num[i]==0)
  474.           num[i]=22;
  475.          else
  476.           break;
  477.    }
  478.   for(i=4;i>-1;i--)
  479.    lcdPlayChar(num[i],6,STAR+(4-i)*WIDE);

  480. lcdPlayChar(speed/10,4,STAR+2*WIDE);
  481. lcdPlayChar(speed%10,4,STAR+3*WIDE);
  482. }
  483. //-------------------------------------------------------------------
  484. void timeServer(void)
  485. {
  486.   if(timeupdate)
  487.    {
  488.     timeupdate=0;
  489.     lcdPlayChar(fen/10,7,STAR);
  490.     lcdPlayChar(fen%10,7,STAR+1*WIDE);
  491.     lcdPlayChar(10,7,STAR+2*WIDE);
  492.     lcdPlayChar(miao/10,7,STAR+3*WIDE);
  493.     lcdPlayChar(miao%10,7,STAR+4*WIDE);   
  494.    }
  495.   if(fashionupdate)
  496.    {
  497.      fashionupdate=0;
  498.      lcdPlayChar(22,7,STAR+2*WIDE);
  499.    }
  500. }
  501. //===================================================================
  502. void t0isr(void) interrupt 1
  503. {
  504.   unsigned char key;
  505.   TH0=(65536-10000)/256;
  506.   TL0=(65536-10000)%256;
  507.   downtimegap++;
  508.   t0ms=++t0ms%100;
  509.   if(t0ms==0)
  510.    {
  511.      timeupdate=1;
  512.      miao=++miao%60;
  513.          if(miao==0)
  514.           fen=++fen%60;
  515.    }
  516.   if(t0ms==50)
  517.    fashionupdate=1;
  518. //----------------------------
  519.   key=0xff;
  520.   KEYLEFT=1;
  521.   KEYRIGH=1;
  522.   KEYROTATION=1;
  523.   KEYDOWN=1;
  524.   if(!KEYLEFT)
  525.     key=0;
  526.   if(!KEYRIGH)
  527.     key=1;
  528.   if(!KEYROTATION)
  529.     key=2;
  530.   if(!KEYDOWN)
  531.     key=3;

  532.   switch(keystate)
  533.    {
  534.     case 0: if(key!=gkey)
  535.                  {
  536.                           gkey=key;
  537.                           keystate=1;
  538.                          }
  539.                         break;
  540.    case 1: if(key==gkey)
  541.              {
  542.                            t0ms1=0;
  543.                            keystate=2;
  544.                            if(key!=0xff)
  545.                              keyflag=1;
  546.                          }
  547.                    else
  548.                      keystate=0;
  549.                    break;
  550.    case 2: if(key==gkey)
  551.              {
  552.                            if(t0ms1<PUSHON)
  553.                              t0ms1++;
  554.                          }
  555.                    else
  556.                     {
  557.                           keystate=0;
  558.                           keyflag=0;
  559.                           gkey=0xff;
  560.                         }
  561.                    break;
  562.    }

  563. }
  564. //===================================================================
  565. void showNextCube(unsigned char code * p,unsigned char x,unsigned char y)
  566. {
  567. unsigned char i,j,temp;
  568.   for(i=0;i<4;i++)
  569.    {  
  570.       temp=1;
  571.       for(j=0;j<4;j++)
  572.       {
  573.         if(p[i] & temp)
  574.          lcdPutPix(x+j,y+i,1);
  575.         else
  576.          lcdPutPix(x+j,y+i,0);
  577.         temp<<=1;
  578.       }
  579.    }  
  580. }
  581. //------------------------------------------------------------------
  582. void createCube(void)
  583. {
  584.   static unsigned char next;
  585.   this.cube=next;
  586.   next=TL0%7;
  587.   this.row=0;
  588.   this.column=6;
  589.   this.state=0;
  590.   this.box=cube+16*this.cube;
  591.   showNextCube(cube+16*next,19,3);
  592. }
  593. //------------------------------------------------------------------
  594. void showCubeMap(void)
  595. {
  596. unsigned char hang,lie,temp;
  597. for(hang=MAXHANG-1;hang>0;hang--)
  598.   {
  599.    if(cubeMap[hang][0]==0 && cubeMap[hang][1]==0)
  600.      break;

  601.    for(lie=0;lie<(MAXLIE/8);lie++)
  602.     {
  603.      temp=8*lie;
  604.      if(cubeMap[hang][lie]&0x01)
  605.        lcdPutPix(temp+1,hang,1);

  606.      if(cubeMap[hang][lie]&0x02)
  607.        lcdPutPix(temp+2,hang,1);

  608.      if(cubeMap[hang][lie]&0x04)
  609.        lcdPutPix(temp+3,hang,1);

  610.      if(cubeMap[hang][lie]&0x08)
  611.        lcdPutPix(temp+4,hang,1);

  612.      if(cubeMap[hang][lie]&0x10)
  613.        lcdPutPix(temp+5,hang,1);

  614.      if(cubeMap[hang][lie]&0x20)
  615.        lcdPutPix(temp+6,hang,1);

  616.      if(cubeMap[hang][lie]&0x40)
  617.        lcdPutPix(temp+7,hang,1);

  618.      if(cubeMap[hang][lie]&0x80)
  619.        lcdPutPix(temp+8,hang,1);
  620.     }
  621.   }
  622. }
  623. //-------------------------------------------------------------------
  624. void writeCubeToMap(void)
  625. {
  626.   unsigned char row,column,temp;
  627.   unsigned char hang,lie;
  628.   for(row=0;row<4;row++)
  629.    {
  630.      temp=1;
  631.      for(column=0;column<4;column++)
  632.           {
  633.             if(this.box[row] & temp)
  634.                  {
  635.                    hang=this.row+row;
  636.                    lie=this.column+column;
  637.            cubeMap[hang][lie/8] |=bittable[lie%8];
  638.                lcdPutPix(lie+1,hang,1);                          
  639.                  }
  640.                 temp<<=1;
  641.           }
  642.    }
  643. }
  644. //-------------------------------------------------------------------
  645. void clearCubeFromMap(void)
  646. {
  647.   unsigned char row,column,temp;
  648.   unsigned char hang,lie;
  649.   for(row=0;row<4;row++)
  650.    {
  651.      temp=1;
  652.      for(column=0;column<4;column++)
  653.           {
  654.             if(this.box[row] & temp)
  655.                  {
  656.                    hang=this.row+row;
  657.                    lie=this.column+column;
  658.            cubeMap[hang][lie/8] &=~bittable[lie%8];
  659.                lcdPutPix(lie+1,hang,0);                  
  660.                  }
  661.                 temp<<=1;
  662.           }
  663.    }
  664. }
  665. //-------------------------------------------------------------------
  666. unsigned char checkBorder(void)
  667. {
  668. if(this.box[3]!=0 && this.row>(MAXHANG-4))
  669.     return 1;
  670.   else if(this.box[2]!=0 && this.row>(MAXHANG-3))
  671.     return 1;
  672.   else if(this.box[1]!=0 && this.row>(MAXHANG-2))
  673.     return 1;
  674.   else if(this.box[0]!=0 && this.row>(MAXHANG-1))
  675.     return 1;
  676. //---------------------
  677.   if((this.box[0] & 0x01) || (this.box[1] & 0x01) || (this.box[2] & 0x01) ||(this.box[3] & 0x01) )
  678.    {
  679.      if(this.column<0)
  680.       return 1;
  681.    }   
  682.   else if((this.box[0] & 0x02) || (this.box[1] & 0x02) || (this.box[2] & 0x02) ||(this.box[3] & 0x02) )
  683.    {
  684.      if(this.column<-1)
  685.       return 1;
  686.    }
  687.   else if((this.box[0] & 0x04) || (this.box[1] & 0x04) || (this.box[2] & 0x04) ||(this.box[3] & 0x04) )
  688.    {
  689.      if(this.column<-2)
  690.       return 1;
  691.    }
  692.    else if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
  693.    {
  694.      if(this.column<-3)
  695.       return 1;
  696.    }
  697. //---------------------
  698.   if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
  699.    {
  700.      if(this.column>(MAXLIE-4))
  701.       return 1;
  702.    }   
  703.   else if((this.box[0] & 0x04) || (this.box[1] & 0x04) || (this.box[2] & 0x04) ||(this.box[3] & 0x04) )
  704.    {
  705.      if(this.column>(MAXLIE-3))
  706.       return 1;
  707.    }
  708.   else if((this.box[0] & 0x02) || (this.box[1] & 0x02) || (this.box[2] & 0x02) ||(this.box[3] & 0x02) )
  709.    {
  710.      if(this.column>(MAXLIE-2))
  711.       return 1;
  712.    }
  713.   else if((this.box[0] & 0x08) || (this.box[1] & 0x08) || (this.box[2] & 0x08) ||(this.box[3] & 0x08) )
  714.    {
  715.      if(this.column>(MAXLIE-1))
  716.       return 1;
  717.    }
  718. //--------------------
  719.   return 0;
  720. }
  721. //------------------------------------------------------------------
  722. unsigned char checkClask(void)
  723. {
  724.   unsigned char row,column,temp;
  725.   unsigned char hang,lie;
  726.   for(row=0;row<4;row++)
  727.    {
  728.      temp=1;
  729.      for(column=0;column<4;column++)
  730.           {
  731.             if(this.box[row] & temp)
  732.                {
  733.                     hang=this.row+row;
  734.                         lie=this.column+column;
  735.                     if(cubeMap[hang][lie/8] & bittable[lie%8])
  736.                           return 1;
  737.                   }
  738.                 temp<<=1;
  739.           }
  740.    }
  741.   return 0;
  742. }
  743. //-------------------------------------------------------------------
  744. void checkMap(void)
  745. {
  746.   unsigned char i,j,delete;
  747.   bit full;
  748.   full=0;
  749.   delete=0;
  750.   for(i=MAXHANG-1;i>0;i--)
  751.    {
  752.      if(cubeMap[i][0]==0 && cubeMap[i][1]==0)
  753.             break;
  754.          if(cubeMap[i][0]==0xff && cubeMap[i][1]==0xff)
  755.            {
  756.              delete++;
  757.                  full=1;
  758.                  for(j=i;j>0;j--)
  759.                   {
  760.                     cubeMap[j][0]=cubeMap[j-1][0];
  761.                     cubeMap[j][1]=cubeMap[j-1][1];
  762.                   }
  763.                  i++;
  764.                  cubeMap[0][0]=0;
  765.                  cubeMap[0][1]=0;
  766.            }
  767.    }
  768. if(full)
  769.   {
  770.     if(delete==1)
  771.           score++;
  772.         else if(delete==2)
  773.           score+=4;
  774.         else if(delete==3)
  775.           score+=9;
  776.         else if(delete==4)
  777.           score+=16;
  778.         rectangle();
  779.         showCubeMap();
  780.     if(score<50)
  781.          speed=1;
  782.         else if(score<100)
  783.          speed=2;
  784.         else if(score<500)
  785.          speed=3;
  786.         else if(score<1000)
  787.          speed=4;
  788.         else if(score<5000)
  789.          speed=5;
  790.         else if(score<10000)
  791.          speed=6;
  792.         else if(score<20000)
  793.          speed=7;
  794.         else if(score<30000)
  795.          speed=8;
  796.         else if(score<40000)
  797.          speed=9;
  798.         else if(score<50000)
  799.          speed=10;
  800.         else if(score<60000)
  801.          speed=11;
  802.         else
  803.          speed=12;  
  804.         showScoreSpeed();
  805.   }
  806. }
  807. //-------------------------------------------------------------------
  808. void moveLeft(void)
  809. {
  810.   clearCubeFromMap();
  811.   this.column--;
  812.   if(checkBorder() || checkClask())
  813.     this.column++;
  814.   writeCubeToMap();
  815. }
  816. //-------------------------------------------------------------------
  817. void moveRigh(void)
  818. {
  819.   clearCubeFromMap();
  820.   this.column++;
  821.   if(checkBorder() || checkClask())
  822.     this.column--;
  823.   writeCubeToMap();
  824. }
  825. //-------------------------------------------------------------------
  826. void moveDown(void)
  827. {
  828.   clearCubeFromMap();
  829.   this.row++;
  830.   if(checkBorder() || checkClask())
  831.    {
  832.     this.row--;
  833.         downok=1;
  834.    }
  835.   else
  836.    downok=0;
  837.   writeCubeToMap();
  838.   if(downok)
  839.     checkMap();
  840. }
  841. //------------------------------------------------------------------
  842. void cubeRotation(void)
  843. {
  844.   unsigned char temp;
  845.   temp=this.state;
  846.   clearCubeFromMap();
  847.   this.state=++this.state%4;
  848.   this.box=cube+16*this.cube+4*this.state;
  849.   if(checkBorder() || checkClask())
  850.    {
  851.      this.state=temp;
  852.      this.box=cube+16*this.cube+4*this.state;
  853.    }
  854.   writeCubeToMap();
  855. }
  856. /////////////////////////////////////////////////////////////////////
  857. void main(void)
  858. {
  859.   TMOD=0x1;
  860.   TH0=(65536-10000)/256;
  861.   TL0=(65536-10000)%256;
  862.   EA=1;
  863.   ET0=1;
  864.   TR0=1;
  865.   lcdIni();
  866.   for(t0ms=0;t0ms<MAXHANG;t0ms++)
  867.    {
  868.      cubeMap[t0ms][0]=0;
  869.      cubeMap[t0ms][1]=0;
  870.    }
  871.   while(1)
  872.    {
  873.      createCube();
  874.          if(checkClask())
  875.           {
  876.        rectangle();
  877. #define SHOWSTAR 12
  878. #define GAP 8
  879.        lcdPlayChar(23,2,SHOWSTAR); //GAME
  880.        lcdPlayChar(24,2,SHOWSTAR+GAP);            
  881.        lcdPlayChar(25,2,SHOWSTAR+2*GAP);
  882.        lcdPlayChar(12,2,SHOWSTAR+3*GAP);

  883.        lcdPlayChar(20,4,SHOWSTAR); //OVER     
  884.        lcdPlayChar(26,4,SHOWSTAR+GAP);
  885.        lcdPlayChar(12,4,SHOWSTAR+2*GAP);
  886.        lcdPlayChar(21,4,SHOWSTAR+3*GAP);
  887.        t0ms=0;
  888.        while(t0ms<95);//延时2秒
  889.            t0ms=0;
  890.        while(t0ms<95);
  891.        ((void (code *) (void)) 0x0000) ( );           
  892.           }
  893.      while(1)
  894.           {
  895.                 timeServer();
  896.             if(keyflag)
  897.                  {
  898.                    keyflag=0;
  899.                    t0ms1=0;
  900.                    if(gkey==0)
  901.                      moveLeft();
  902.                    if(gkey==1)
  903.                      moveRigh();
  904.                    if(gkey==2)
  905.                      cubeRotation();
  906.                    if(gkey==3)
  907.                      moveDown();                     
  908.                  }
  909.        if(gkey==0 && t0ms1==PUSHON)
  910.             {
  911.                   t0ms1-=10;
  912.                   moveLeft();
  913.                 }
  914.        if(gkey==1 && t0ms1==PUSHON)
  915.             {
  916.                   t0ms1-=10;
  917.                   moveRigh();
  918.                 }
  919.        if(gkey==3 && t0ms1==PUSHON)
  920.             {
  921.                   t0ms1-=10;
  922.                   moveDown();
  923.                 }
  924.            if(downtimegap>(DOWNTIME-speed))
  925.             {
  926.              moveDown();
  927.                  downtimegap=0;
  928.                 }
  929.            if(downok)
  930.             {
  931.                   downok=0;
  932.                   break;
  933.                 }
  934.           }
  935.    }
  936. }
复制代码

0.png

全部资料51hei下载地址:
基于51单片机和Proteus仿真的俄罗斯方块游戏设计.zip (89.2 KB, 下载次数: 85)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:516650 发表于 2020-6-2 16:48 | 显示全部楼层
感谢楼主了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表