找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 4800|回復(fù): 2
收起左側(cè)

基于LPC1768的俄羅斯方塊源代碼,配合3.2寸TFT屏幕

[復(fù)制鏈接]
ID:324527 發(fā)表于 2018-5-7 20:48 | 顯示全部樓層 |閱讀模式
程序運行后,用板子上的五向按鍵控制。

單片機源程序如下:
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include "lcd_api.h"
  4. #include "ili_lcd_general.h"
  5. #include "LPC17xx.h"

  6. typedef struct                                                 //方塊結(jié)構(gòu)體,包括類型,狀態(tài),坐標,顏色
  7. {
  8. uint8_t type;                                                //方塊的類型(LJITOSZ)
  9. uint8_t state;                                                //方塊的狀態(tài)(0、90、180、270°旋轉(zhuǎn))
  10. int16_t x,y;                                                        //方塊的坐標
  11. uint8_t colorType;                                           //顏色類型
  12. }BOX;

  13. const uint8_t box[56]=                                           //方塊數(shù)組,包括七種方塊,每種四種狀態(tài)
  14. {
  15. 0x80,0x0e,0x46,0x04,0x70,0x01,0x20,0x62,/*J*/
  16. 0x20,0x0e,0x44,0x06,0x70,0x04,0x60,0x22,/*L*/
  17. 0x60,0x0c,0x64,0x02,0x30,0x06,0x40,0x26,/*s*/
  18. 0xc0,0x06,0x62,0x04,0x60,0x03,0x20,0x46,/*z*/
  19. 0x40,0x0e,0x64,0x04,0x70,0x02,0x20,0x26,/*T*/
  20. 0x44,0x44,0xf0,0x00,0x22,0x22,0x00,0x0f,/*I*/
  21. 0xc0,0x0c,0x66,0x00,0x30,0x03,0x00,0x66,/*O*/  
  22. };

  23. uint8_t map[10][20];                                                //地圖數(shù)組,包括二十行十列,存方塊的顏色類型

  24. const int colors[]={0,Blue,Red,Magenta,Green,Yellow};                  //顏色數(shù)組,聲明要用到的顏色
  25.        
  26. BOX this,next;                                                // 定義兩個結(jié)構(gòu)變量,當(dāng)前方塊和下個方塊

  27. uint16_t score=0;                                 //全局變量總分

  28. uint8_t toButtom,downTimes;                                         //標記是否落到底

  29. void mapInit(){                                                                 //初始化地圖
  30.         lcd_Initializtion();
  31.         DrawYLine(161,0,319,Red);
  32.         WriteChineseString(176,32,0,3,White,Black);
  33.         WriteChineseString(176,160,3,3,White,Black);
  34.         WriteEnglishString(176,192,"00000",White,Black);
  35. }

  36. void showScore(){                                          //顯示總分函數(shù)
  37.         WriteEnglishWord(176,192,score/10000+48,White,Black);
  38.         WriteEnglishWord(184,192,(score/1000)%10+48,White,Black);
  39.         WriteEnglishWord(192,192,(score/100)%10+48,White,Black);
  40.         WriteEnglishWord(200,192,(score/10)%10+48,White,Black);
  41.         WriteEnglishWord(208,192,score%10+48,White,Black);
  42. }

  43. void delay(uint8_t k){                                                   //延時函數(shù)
  44.         int i,j;
  45.         for(j=0;j<k;j++){
  46.                 for(i=0;i<900000;i++);
  47.         }
  48. }

  49. void disBox(BOX b){                                           //顯示方塊函數(shù),參數(shù)是一個BOX
  50.         uint8_t i,j,k,l;
  51.         i=b.type*8+b.state*2;
  52.         l=box[i];
  53.         for(j=0;j<2;j++){
  54.                 for(k=0;k<4;k++){
  55.                         if(l&0x01){
  56.                                 DrawBox(b.x+k,b.y+j,colors[b.colorType]);
  57.                                 if(b.x!=11){
  58.                                         map[b.x+k][b.y+j]=b.colorType;
  59.                                 }
  60.                         }
  61.                         l>>=1;
  62.                 }
  63.         }
  64.         l=box[i+1];
  65.         for(j=0;j<2;j++){
  66.                 for(k=0;k<4;k++){
  67.                         if(l&0x01){
  68.                                 DrawBox(b.x+k,b.y+j+2,colors[b.colorType]);
  69.                                 if(b.x!=11){
  70.                                         map[b.x+k][b.y+j+2]=b.colorType;
  71.                                 }
  72.                         }
  73.                         l>>=1;
  74.                 }
  75.         }
  76. }

  77. void clearBox(){                                          //清除方塊函數(shù)
  78.         uint8_t i,j,k,l;
  79.         i=this.type*8+this.state*2;
  80.         l=box[i];
  81.         for(j=0;j<2;j++){
  82.                 for(k=0;k<4;k++){
  83.                         if(l&0x01){
  84.                                 CoverBox(this.x+k,this.y+j);
  85.                                 map[this.x+k][this.y+j]=0;
  86.                         }
  87.                         l>>=1;
  88.                 }
  89.         }
  90.         l=box[i+1];
  91.         for(j=0;j<2;j++){
  92.                 for(k=0;k<4;k++){
  93.                         if(l&0x01){
  94.                                 CoverBox(this.x+k,this.y+j+2);
  95.                                 map[this.x+k][this.y+j+2]=0;
  96.                         }
  97.                         l>>=1;
  98.                 }
  99.         }
  100. }

  101. void clearNext(){                                                          //清除下一個函數(shù)
  102.         uint8_t x,y;
  103.         for(x=11;x<15;x++){
  104.                 for(y=5;y<9;y++){
  105.                         CoverBox(x,y);       
  106.                 }
  107.         }
  108. }

  109. void createNext(){                                                   //創(chuàng)建下一個函數(shù)
  110.         clearNext();
  111.         next.x=11;
  112.         next.y=5;
  113.         next.colorType=rand()%5+1;                          //隨機顏色
  114.         next.type=rand()%7;                                          //隨機種類
  115.         next.state=rand()%4;                                   //隨機狀態(tài)
  116.         disBox(next);
  117. }

  118. int16_t checkCrack(){           //檢測是否碰撞
  119.         uint8_t isCrack = 0;
  120.         uint8_t i,j,k,l;
  121.         int16_t x,y;
  122.         i=this.type*8+this.state*2;
  123.         l=box[i];
  124.         for(j=0;j<2;j++){
  125.                 for(k=0;k<4;k++){
  126.                         if(l&0x01){
  127.                                 x=this.x+k;
  128.                                 y=this.y+j;
  129.                                 if(x<0||x>9||y>19||map[x][y]!=0){           // 方塊越過邊界或者已經(jīng)被占即為已碰撞
  130.                                         isCrack = 1;
  131.                                         break;
  132.                                 }                               
  133.                         }
  134.                         l>>=1;
  135.                 }
  136.         }
  137.         if(!isCrack){
  138.                 l=box[i+1];
  139.                 for(j=0;j<2;j++){
  140.                         for(k=0;k<4;k++){
  141.                                 if(l&0x01){
  142.                                         x=this.x+k;
  143.                                         y=this.y+j+2;
  144.                                         if(x<0||x>9||y>19||map[x][y]!=0){
  145.                                                 isCrack = 1;
  146.                                                 break;
  147.                                         }                               
  148.                                 }
  149.                                 l>>=1;
  150.                         }
  151.                 }
  152.         }
  153.         return isCrack;
  154. }

  155. void putIntoMap(){                                                  //把方塊放入地圖
  156.         this=next;
  157.         this.x=3;
  158.         this.y=0;
  159. }

  160. void moveLeft(){                                          //向左移動
  161.         clearBox();
  162.         this.x--;
  163.         if(checkCrack()){
  164.                 this.x++;
  165.         }
  166.         disBox(this);
  167. }

  168. void moveRight(){                                          //向右移動
  169.         clearBox();
  170.         this.x++;
  171.         if(checkCrack()){
  172.                 this.x--;
  173.         }
  174.         disBox(this);
  175. }

  176. void refreshMap(){                                                //刷新地圖
  177.         uint8_t i,j;
  178.         FillRect(0,0,159,319,Black);
  179.         for(i=0;i<20;i++){
  180.                 for(j=0;j<10;j++){
  181.                         if(map[j][i]!=0){
  182.                                 DrawBox(j,i,colors[map[j][i]]);
  183.                         }
  184.                 }
  185.         }
  186. }

  187. void checkMap(){                         //檢查是否需要消行
  188.         int8_t i,j,k;
  189.         for(i=19;i>=0;i--){
  190.                 k=0;
  191.                 for(j=0;j<10;j++){
  192.                         if(map[j][i]!=0){
  193.                                 k++;
  194.                         }
  195.                 }
  196.                 if(k==10){                                         //如果一行上全部被占位即為需要消行
  197.                         score++;
  198.                         showScore();
  199.                         for(j=i;j>0;j--){
  200.                                 for(k=0;k<10;k++){
  201.                                         map[k][j]=map[k][j-1];
  202.                                 }
  203.                         }
  204.                         for(k=0;k<10;k++){
  205.                                 map[k][0]=0;
  206.                         }
  207.                         i++;
  208.                         refreshMap();
  209.                 }
  210.         }
  211. }

  212. void moveDown(){                                //下移方塊
  213.         clearBox();
  214.         this.y++;
  215.         if(checkCrack()){
  216.                 toButtom = 1;
  217.                 this.y--;
  218.                 disBox(this);
  219.                 checkMap();
  220.         }else{
  221.                 disBox(this);
  222.         }
  223.         downTimes=0;
  224. }

  225. void moveTurn(){                                        //翻轉(zhuǎn)方塊
  226.         clearBox();
  227.         this.state=(++this.state)%4;
  228.         if(checkCrack()){
  229.                 this.state=(--this.state)%4;
  230.         }
  231.         disBox(this);
  232.         delay(5);
  233. }

  234. void showMap(){                                        //顯示地圖函數(shù)
  235.         uint16_t key;
  236.         createNext();                                                          
  237.         while(1){
  238.                 putIntoMap();
  239.                 createNext();
  240.                 if(!checkCrack()){
  241.                         disBox(this);
  242.                 }else{
  243.                         WriteEnglishString(50,150,"GAME OVER!",White,Black);
  244.                         break;
  245.                 }
  246.                 while(1){
  247.                         key = ((LPC_GPIO1->FIOPIN >> 26)&0x0f);       
  248.                         switch(key){
  249.                                 case 0x07:                //上
  250.                                         moveTurn();
  251.                                 break;
  252.                                 case 0x0e:                 //下
  253.                                         moveDown();
  254.                                 break;
  255. ……………………

  256. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
【02】寶馬1768_LCD俄羅斯方塊(2014.05.28).rar (96.9 KB, 下載次數(shù): 31)


回復(fù)

使用道具 舉報

ID:507806 發(fā)表于 2019-4-9 14:09 | 顯示全部樓層
打開程序怎么在keil中不能運行啊
回復(fù)

使用道具 舉報

ID:251735 發(fā)表于 2019-4-11 18:00 | 顯示全部樓層
頂一下
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表