|
程序運行后,用板子上的五向按鍵控制。
單片機源程序如下:
- #include <stdlib.h>
- #include <stdint.h>
- #include "lcd_api.h"
- #include "ili_lcd_general.h"
- #include "LPC17xx.h"
- typedef struct //方塊結(jié)構(gòu)體,包括類型,狀態(tài),坐標,顏色
- {
- uint8_t type; //方塊的類型(LJITOSZ)
- uint8_t state; //方塊的狀態(tài)(0、90、180、270°旋轉(zhuǎn))
- int16_t x,y; //方塊的坐標
- uint8_t colorType; //顏色類型
- }BOX;
- const uint8_t box[56]= //方塊數(shù)組,包括七種方塊,每種四種狀態(tài)
- {
- 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]; //地圖數(shù)組,包括二十行十列,存方塊的顏色類型
- const int colors[]={0,Blue,Red,Magenta,Green,Yellow}; //顏色數(shù)組,聲明要用到的顏色
-
- BOX this,next; // 定義兩個結(jié)構(gòu)變量,當(dāng)前方塊和下個方塊
- 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(){ //顯示總分函數(shù)
- 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){ //延時函數(shù)
- int i,j;
- for(j=0;j<k;j++){
- for(i=0;i<900000;i++);
- }
- }
- void disBox(BOX b){ //顯示方塊函數(shù),參數(shù)是一個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(){ //清除方塊函數(shù)
- 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(){ //清除下一個函數(shù)
- uint8_t x,y;
- for(x=11;x<15;x++){
- for(y=5;y<9;y++){
- CoverBox(x,y);
- }
- }
- }
- void createNext(){ //創(chuàng)建下一個函數(shù)
- clearNext();
- next.x=11;
- next.y=5;
- next.colorType=rand()%5+1; //隨機顏色
- next.type=rand()%7; //隨機種類
- next.state=rand()%4; //隨機狀態(tài)
- 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){ // 方塊越過邊界或者已經(jīng)被占即為已碰撞
- 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(){ //翻轉(zhuǎn)方塊
- clearBox();
- this.state=(++this.state)%4;
- if(checkCrack()){
- this.state=(--this.state)%4;
- }
- disBox(this);
- delay(5);
- }
- void showMap(){ //顯示地圖函數(shù)
- 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黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
【02】寶馬1768_LCD俄羅斯方塊(2014.05.28).rar
(96.9 KB, 下載次數(shù): 31)
2018-5-7 20:45 上傳
點擊文件名下載附件
|
|