程序运行后,用板子上的五向按键控制。
单片机源程序如下:
- #include <stdlib.h>
- #include <stdint.h>
- #include "lcd_api.h"
- #include "ili_lcd_general.h"
- #include "LPC17xx.h"
- typedef struct //方块结构体,包括类型,状态,坐标,颜色
- {
- uint8_t type; //方块的类型(LJITOSZ)
- uint8_t state; //方块的状态(0、90、180、270°旋转)
- int16_t x,y; //方块的坐标
- uint8_t colorType; //颜色类型
- }BOX;
- const uint8_t box[56]= //方块数组,包括七种方块,每种四种状态
- {
- 0x80,0x0e,0x46,0x04,0x70,0x01,0x20,0x62,/*J*/
- 0x20,0x0e,0x44,0x06,0x70,0x04,0x60,0x22,/*L*/
- 0x60,0x0c,0x64,0x02,0x30,0x06,0x40,0x26,/*s*/
- 0xc0,0x06,0x62,0x04,0x60,0x03,0x20,0x46,/*z*/
- 0x40,0x0e,0x64,0x04,0x70,0x02,0x20,0x26,/*T*/
- 0x44,0x44,0xf0,0x00,0x22,0x22,0x00,0x0f,/*I*/
- 0xc0,0x0c,0x66,0x00,0x30,0x03,0x00,0x66,/*O*/
- };
- uint8_t map[10][20]; //地图数组,包括二十行十列,存方块的颜色类型
- const int colors[]={0,Blue,Red,Magenta,Green,Yellow}; //颜色数组,声明要用到的颜色
-
- BOX this,next; // 定义两个结构变量,当前方块和下个方块
- uint16_t score=0; //全局变量总分
- uint8_t toButtom,downTimes; //标记是否落到底
- void mapInit(){ //初始化地图
- lcd_Initializtion();
- DrawYLine(161,0,319,Red);
- WriteChineseString(176,32,0,3,White,Black);
- WriteChineseString(176,160,3,3,White,Black);
- WriteEnglishString(176,192,"00000",White,Black);
- }
- void showScore(){ //显示总分函数
- WriteEnglishWord(176,192,score/10000+48,White,Black);
- WriteEnglishWord(184,192,(score/1000)%10+48,White,Black);
- WriteEnglishWord(192,192,(score/100)%10+48,White,Black);
- WriteEnglishWord(200,192,(score/10)%10+48,White,Black);
- WriteEnglishWord(208,192,score%10+48,White,Black);
- }
- void delay(uint8_t k){ //延时函数
- int i,j;
- for(j=0;j<k;j++){
- for(i=0;i<900000;i++);
- }
- }
- void disBox(BOX b){ //显示方块函数,参数是一个BOX
- uint8_t i,j,k,l;
- i=b.type*8+b.state*2;
- l=box[i];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- DrawBox(b.x+k,b.y+j,colors[b.colorType]);
- if(b.x!=11){
- map[b.x+k][b.y+j]=b.colorType;
- }
- }
- l>>=1;
- }
- }
- l=box[i+1];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- DrawBox(b.x+k,b.y+j+2,colors[b.colorType]);
- if(b.x!=11){
- map[b.x+k][b.y+j+2]=b.colorType;
- }
- }
- l>>=1;
- }
- }
- }
- void clearBox(){ //清除方块函数
- uint8_t i,j,k,l;
- i=this.type*8+this.state*2;
- l=box[i];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- CoverBox(this.x+k,this.y+j);
- map[this.x+k][this.y+j]=0;
- }
- l>>=1;
- }
- }
- l=box[i+1];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- CoverBox(this.x+k,this.y+j+2);
- map[this.x+k][this.y+j+2]=0;
- }
- l>>=1;
- }
- }
- }
- void clearNext(){ //清除下一个函数
- uint8_t x,y;
- for(x=11;x<15;x++){
- for(y=5;y<9;y++){
- CoverBox(x,y);
- }
- }
- }
- void createNext(){ //创建下一个函数
- clearNext();
- next.x=11;
- next.y=5;
- next.colorType=rand()%5+1; //随机颜色
- next.type=rand()%7; //随机种类
- next.state=rand()%4; //随机状态
- disBox(next);
- }
- int16_t checkCrack(){ //检测是否碰撞
- uint8_t isCrack = 0;
- uint8_t i,j,k,l;
- int16_t x,y;
- i=this.type*8+this.state*2;
- l=box[i];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- x=this.x+k;
- y=this.y+j;
- if(x<0||x>9||y>19||map[x][y]!=0){ // 方块越过边界或者已经被占即为已碰撞
- isCrack = 1;
- break;
- }
- }
- l>>=1;
- }
- }
- if(!isCrack){
- l=box[i+1];
- for(j=0;j<2;j++){
- for(k=0;k<4;k++){
- if(l&0x01){
- x=this.x+k;
- y=this.y+j+2;
- if(x<0||x>9||y>19||map[x][y]!=0){
- isCrack = 1;
- break;
- }
- }
- l>>=1;
- }
- }
- }
- return isCrack;
- }
- void putIntoMap(){ //把方块放入地图
- this=next;
- this.x=3;
- this.y=0;
- }
- void moveLeft(){ //向左移动
- clearBox();
- this.x--;
- if(checkCrack()){
- this.x++;
- }
- disBox(this);
- }
- void moveRight(){ //向右移动
- clearBox();
- this.x++;
- if(checkCrack()){
- this.x--;
- }
- disBox(this);
- }
- void refreshMap(){ //刷新地图
- uint8_t i,j;
- FillRect(0,0,159,319,Black);
- for(i=0;i<20;i++){
- for(j=0;j<10;j++){
- if(map[j][i]!=0){
- DrawBox(j,i,colors[map[j][i]]);
- }
- }
- }
- }
- void checkMap(){ //检查是否需要消行
- int8_t i,j,k;
- for(i=19;i>=0;i--){
- k=0;
- for(j=0;j<10;j++){
- if(map[j][i]!=0){
- k++;
- }
- }
- if(k==10){ //如果一行上全部被占位即为需要消行
- score++;
- showScore();
- for(j=i;j>0;j--){
- for(k=0;k<10;k++){
- map[k][j]=map[k][j-1];
- }
- }
- for(k=0;k<10;k++){
- map[k][0]=0;
- }
- i++;
- refreshMap();
- }
- }
- }
- void moveDown(){ //下移方块
- clearBox();
- this.y++;
- if(checkCrack()){
- toButtom = 1;
- this.y--;
- disBox(this);
- checkMap();
- }else{
- disBox(this);
- }
- downTimes=0;
- }
- void moveTurn(){ //翻转方块
- clearBox();
- this.state=(++this.state)%4;
- if(checkCrack()){
- this.state=(--this.state)%4;
- }
- disBox(this);
- delay(5);
- }
- void showMap(){ //显示地图函数
- uint16_t key;
- createNext();
- while(1){
- putIntoMap();
- createNext();
- if(!checkCrack()){
- disBox(this);
- }else{
- WriteEnglishString(50,150,"GAME OVER!",White,Black);
- break;
- }
- while(1){
- key = ((LPC_GPIO1->FIOPIN >> 26)&0x0f);
- switch(key){
- case 0x07: //上
- moveTurn();
- break;
- case 0x0e: //下
- moveDown();
- break;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
【02】宝马1768_LCD俄罗斯方块(2014.05.28).rar
(96.9 KB, 下载次数: 31)
|