找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3967|回复: 2
收起左侧

基于LPC1768的俄罗斯方块源代码,配合3.2寸TFT屏幕

[复制链接]
ID:324527 发表于 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                                                 //方块结构体,包括类型,状态,坐标,颜色
  7. {
  8. uint8_t type;                                                //方块的类型(LJITOSZ)
  9. uint8_t state;                                                //方块的状态(0、90、180、270°旋转)
  10. int16_t x,y;                                                        //方块的坐标
  11. uint8_t colorType;                                           //颜色类型
  12. }BOX;

  13. const uint8_t box[56]=                                           //方块数组,包括七种方块,每种四种状态
  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];                                                //地图数组,包括二十行十列,存方块的颜色类型

  24. const int colors[]={0,Blue,Red,Magenta,Green,Yellow};                  //颜色数组,声明要用到的颜色
  25.        
  26. BOX this,next;                                                // 定义两个结构变量,当前方块和下个方块

  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(){                                          //显示总分函数
  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){                                                   //延时函数
  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){                                           //显示方块函数,参数是一个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(){                                          //清除方块函数
  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(){                                                          //清除下一个函数
  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(){                                                   //创建下一个函数
  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;                                   //随机状态
  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){           // 方块越过边界或者已经被占即为已碰撞
  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(){                                        //翻转方块
  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(){                                        //显示地图函数
  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黑下载附件…………
复制代码

所有资料51hei提供下载:
【02】宝马1768_LCD俄罗斯方块(2014.05.28).rar (96.9 KB, 下载次数: 31)
回复

使用道具 举报

ID:507806 发表于 2019-4-9 14:09 | 显示全部楼层
打开程序怎么在keil中不能运行啊
回复

使用道具 举报

ID:251735 发表于 2019-4-11 18:00 | 显示全部楼层
顶一下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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