没有蓝牙,不要蓝牙控制的,最好是把下面的程序改一下,谢谢大神
#include <stdint.h>
#include "msp430.h"
#include "HAL_Buttons.h"
#include "HAL_Board.h"
#include "HAL_Dogs102x6.h"
#include "HAL_Menu.h"
#include "HAL_Wheel.h"
#include "Snake.h"
#include "Random.h"
#include "CTS_Layer.h"
#include "Menu.h"
#include "Tetris.h"
unsigned char Snake_Temp=0;
unsigned char Snake_downflag=0;
unsigned char Score_bai=48,Score_shi=48,Score_ge=48;//设置得分score的初始值“000”
unsigned char Best_bai=48,Best_shi=48,Best_ge=48;//设置最高分best的初始值“000”
//================================================================================
void spot (void)//游戏场景布局
{
uint8_t a;
//绘制最外层的框子
for(a=0;a<102;a++)
{
Dogs102x6_pixelDraw(a, 0, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(a, 1, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(a, 62, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(a, 63, DOGS102x6_DRAW_NORMAL);
}//横向的两行
for(a=1;a<63;a++)
{
Dogs102x6_pixelDraw(0, a, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(1, a, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(2, a, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(99, a, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(100, a, DOGS102x6_DRAW_NORMAL);
Dogs102x6_pixelDraw(101, a, DOGS102x6_DRAW_NORMAL);
}//竖向的两行
}
/*
*
*/
void Display3x3Pixel(uint8_t x,uint8_t y,uint8_t mode)
{
x=3*x+3;
y=3*y+2;
Dogs102x6_pixelDraw(x, y, mode);
Dogs102x6_pixelDraw(x+1, y, mode);
Dogs102x6_pixelDraw(x+2, y, mode);
Dogs102x6_pixelDraw(x, y+1, mode);
Dogs102x6_pixelDraw(x+1, y+1, mode);
Dogs102x6_pixelDraw(x+2, y+1, mode);
Dogs102x6_pixelDraw(x, y+2, mode);
Dogs102x6_pixelDraw(x+1, y+2, mode);
Dogs102x6_pixelDraw(x+2, y+2, mode);
}
/*
*
*/
void SnakeGame(void)
{
uint8_t newsnake_X[999] = {10,11,12};
uint8_t newsnake_Y[999] = {10,10,10};//用数组表示贪吃蛇,初始化贪吃蛇
uint8_t snake_X[999] = {10,11,12};
uint8_t snake_Y[999] = {10,10,10};//用于记录上一条贪吃蛇的状态,用于贪吃蛇的更新
int8_t head_X = 10;
int8_t head_Y = 10;//蛇头
uint8_t food_X = 4;
uint8_t food_Y = 10;//食物初始化
uint8_t snake_size=3;//蛇长
unsigned short score,Best=0;//初始化得分,最高分
unsigned char dead=0;//贪吃蛇死亡状态变量
unsigned int food_X_create;
unsigned int food_Y_create;//产生的新食物的位置
uint8_t Temp_reg=3;//触摸按键键值的记忆变量,用于与每次的键值作对比,防止出现蛇后退的现象
signed char dX,dY;
dX=-1;
dY=0;//位置偏移量,控制蛇的上下左右方向变化
buttonsPressed = 0;
Dogs102x6_clearScreen();
spot();
uint8_t i;
while (!(buttonsPressed & BUTTON_S2))
{
//Dogs102x6_clearScreen();
//spot();
//画出贪吃蛇
for(i=0;i<snake_size;i++)
{
Display3x3Pixel(newsnake_X[i], newsnake_Y[i], DOGS102x6_DRAW_NORMAL);
}
//画出食物
Display3x3Pixel(food_X, food_Y, DOGS102x6_DRAW_NORMAL);
Snake_Temp=Key;
if(Snake_downflag)
{
if(Snake_Temp==1&&Temp_reg!=2)//向上运动
{
dX=0;
dY=-1;
Temp_reg=Snake_Temp;
}
else if(Snake_Temp==2&&Temp_reg!=1)//向下运动
{
dX=0;
dY=1;
Temp_reg=Snake_Temp;
}
else if(Snake_Temp==3&&Temp_reg!=4)//向左运动
{
dX=-1;
dY=0;
Temp_reg=Snake_Temp;
}
else if(Snake_Temp==4&&Temp_reg!=3)//向右运动
{
dX=1;
dY=0;
Temp_reg=Snake_Temp;
}
clear_background();
spot();
head_X=head_X+dX;
head_Y=head_Y+dY;
//吃到实物
if ((head_X==food_X)&&(head_Y==food_Y))
{
food_X_create=(food_X_create+head_X+head_Y)%32;
food_Y_create=(food_X_create+head_X+head_Y)%20;
snake_size++;//蛇的长度加1
newsnake_X[0] = head_X;
newsnake_Y[0] = head_Y;
for(i = 0; i<snake_size; i++)
{
newsnake_X[i+1] = snake_X[i];
newsnake_Y[i+1] = snake_Y[i];
}
for(i = 0; i<snake_size; i++)
{
snake_X[i] = newsnake_X[i];
snake_Y[i] = newsnake_Y[i];
}
food_X= food_X_create;
food_Y= food_Y_create;
score=snake_size-3;//根据蛇长(或吃到食物的个数)来计算出得分
//得到score的千百十各位,并转化为ASCII码(+48)
Score_bai=score/100+48;
Score_shi=(score/10)%10+48;
Score_ge=score%10+48;
//如果当前得分大于最高分Best,则更新最高分
if(score>=Best)
{
Best=score;
}
//得到best的千百十各位,并转化为ASCII码(+48)
Best_bai=Best/100+48;
Best_shi=(Best/10)%10+48;
Best_ge=Best%10+48;
}
//没有吃到食物的操作
else
{
for(i = 0; i<(snake_size-1); i++)
{
newsnake_X[i+1] = snake_X[i];
newsnake_Y[i+1] = snake_Y[i];
}
newsnake_X[0] = head_X;
newsnake_Y[0] = head_Y;
for(i = 0; i<snake_size; i++)
{
snake_X[i] = newsnake_X[i];
snake_Y[i] = newsnake_Y[i];
}
}
//蛇头碰到蛇身或边界,蛇死亡
for(i=2;i<=snake_size;i++)
{
if((head_X==newsnake_X[i])&&(head_Y==newsnake_Y[i]))
{
dead=1;
}
else
{
dead= 0;
}
}
if ((head_X<0)||(head_X==32)||(head_Y<0)||(head_Y==20))
{
dead=1;
}
Snake_downflag=0;
if(dead==1)
{
Snake_Game_Over();
delay_ms(2000);
//Main();
break;
}
}
}
}
void clear_background(void)
{
char i,j;
for(i=2;i<62;i++)
for(j=3;j<99;j++)
Dogs102x6_pixelDraw(j, i, 1);
}
void Snake_Game_Over()
{
Dogs102x6_clearScreen();
Dogs102x6_stringDraw(3, 0, " Game Over ", DOGS102x6_DRAW_NORMAL);
}
#include <stdint.h>
#include "msp430.h"
#include "HAL_PMM.h"
#include "HAL_UCS.h"
#include "HAL_Board.h"
#include "HAL_Buttons.h"
#include "HAL_Cma3000.h"
#include "HAL_Dogs102x6.h"
#include "HAL_Menu.h"
#include "HAL_Wheel.h"
#include "LPM.h"
#include "PMM.h"
#include "Random.h"
#include "CTS_Layer.h"
#include "Tetris.h"
#include "Menu.h"
#define MAXHANG 20 //游戏显示行数
#define MAXLIE 16 //游戏显示列数
#define MapWide 48 //地图宽
#define MapHigh 60 //地图高
#define ON 0
#define OFF 1
#define BIT(n) (1<<(n))
uchar Gameover=0;
uchar temp=0;
uchar m=1;
vola
tile uint cubeMap[MAXHANG]; //地图显示存存
typedef struct{
uchar const * box; //方块图形字模数据指针
uchar cube : 4; //方块形态(占用4个位域)
uchar state : 4; //方块状态(占用4个位域)
char row; //方块所在行
char column; //方块所在列
} block;
block this;
uchar liang=1;
uchar Speed=1,Score=0;
uint timeA=0;
uchar downflag=0;
uchar randomNumber=0;
const uchar cube[]=
{
0,4,0xe,0, 0,2,6,2, 0,7,2,0, 4,6,4,0,
0,8,0xe,0, 0,4,4,0xc, 0,0,0xe,2, 0,6,4,4,
0,0xe,8,0, 0,4,4,6, 0,1,7,0, 6,2,2,0,
0,0xc,6,0, 0,2,6,4, 0,6,3,0, 2,6,4,0,
0,6,0xc,0, 0,4,6,2, 0,3,6,0, 4,6,2,0,
0,0xf,0,0, 4,4,4,4, 0,0,0xf,0, 2,2,2,2,
0,6,6,0, 0,6,6,0, 0,6,6,0, 0,6,6,0
};
void delay_ms(uint z)//毫秒级延时函数
{
uint i,m;
for(i=z;i>0;i--)
for(m=4500;m>0;m--)
{
_NOP();
}
}
/*
*
*/
void DisplayRamClear()
{
uchar i;
for(i=0;i<20;i++)
{
cubeMap[i]=0x0;
}
}
/*
*
*/
void Number2BitDisplay(uchar Row,uchar Column,uchar Number)
{
uchar tempShi,tempGe;
tempShi=Number/10;
tempGe=Number%10;
Dogs102x6_charDraw(Row, Column,tempShi+'0',ON);
Dogs102x6_charDraw(Row, Column+6,tempGe+'0',ON);
}
/*
*
*/
void GameBackground()
{
uchar i;
for(i=0;i<MapHigh+1;i++)
{
Dogs102x6_pixelDraw(0, i, ON);
Dogs102x6_pixelDraw(MapWide+1, i, ON);
}
for(i=0;i<MapWide+2;i++)
{
Dogs102x6_pixelDraw(i, 0, ON);
Dogs102x6_pixelDraw(i, MapHigh+1, ON);
}
for(i=MapWide+3;i<102;i++)
{
Dogs102x6_pixelDraw(i, 34, ON);
Dogs102x6_pixelDraw(i, 36, ON);
}
DisplayRamClear();
Dogs102x6_stringDraw(0, 51, "Author:",ON);
Dogs102x6_stringDraw(1, 51, " Liuzw",ON);
Dogs102x6_stringDraw(2, 51, "Number:",ON);
Dogs102x6_stringDraw(3, 51, " 1301-20",ON);
Dogs102x6_stringDraw(3, 10, "START",ON);
Dogs102x6_stringDraw(5, 51, "NEXT: ",ON);
Dogs102x6_stringDraw(6, 51, "SPEED:--",ON);
Dogs102x6_stringDraw(7, 51, "SCORE:--",ON);
Number2BitDisplay(6,87,Speed);
Number2BitDisplay(7,87,Score);
}
/*
*
*/
void GameMapClear()
{
uchar i,j;
for(i=0;i<MapHigh;i++)
{
for(j=0;j<MapWide;j++)
{
Dogs102x6_pixelDraw(j+1, i+1, OFF);
}
}
}
/*
*
*/
void SystemInit()
{
WDTCTL = WDTPW + WDTHOLD;
P8DIR|=BIT1+BIT2;
P8OUT|=BIT1+BIT2;
P1DIR|=BIT0+BIT1+BIT2+BIT3+BIT4+BIT5;
P1OUT|=BIT0+BIT1+BIT2+BIT3+BIT4+BIT5;
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_1 + 609; // Set DCO Multiplier for 25MHz(这里设置为20MHz适应液晶的SPI
通信)
// (N + 1) * FLLRef = Fdco
// (762 + 1) * 32768 = 25MHz
// Set FLL Div = fDCOCLK/2
__bic_SR_register(SCG0); // Enable the FLL control loop
TA1CCTL0 = CCIE; // CCR0 interrupt enabled
TA1CCR0 = 328-1;
TA1CTL = TASSEL_1 + MC_1 + TACLR; // ACLK, upmode, clear TAR
_BIS_SR(GIE);
Dogs102x6_init();
Dogs102x6_clearScreen();
Dogs102x6_backlightInit();
Dogs102x6_setContrast(11);
Dogs102x6_setBacklight(liang);
}
/*
*
*/
void Display3x3Pixels(uchar x,uchar y,uchar mode)
{
x=3*x+1;
y=3*y+1;
Dogs102x6_pixelDraw(x, y, mode);
Dogs102x6_pixelDraw(x+1, y, mode);
Dogs102x6_pixelDraw(x+2, y, mode);
Dogs102x6_pixelDraw(x, y+1, mode);
Dogs102x6_pixelDraw(x+1, y+1, mode);
Dogs102x6_pixelDraw(x+2, y+1, mode);
Dogs102x6_pixelDraw(x, y+2, mode);
Dogs102x6_pixelDraw(x+1, y+2, mode);
Dogs102x6_pixelDraw(x+2, y+2, mode);
}
/*
*
*/
void ChangeRamBit(uchar Row,uchar Column,uchar mode)
{
uint tempData;
tempData=(0x8000>>(Column%16));
if(mode==ON)
cubeMap[Row]|=tempData;
else
cubeMap[Row]&=~tempData;
}
/*
*
*/
uchar ReadDisplayRamBit(uchar Row,uchar Column)
{
uint tempData;
tempData=(0x8000>>(Column%16));
if(cubeMap[Row]&tempData)
return 1;
else
return 0;
}
/*
*
*/
void showNextCube(uchar const * p,uchar x,uchar y)
#define NexPoinX 27 //下一个图形显示的X坐标
#define NexPoinY 12 //下一个图形显示的Y坐标
{
uchar i,j,temp;
for(i=0;i<3;i++)
{
temp=0x08;
for(j=0;j<4;j++)
{
if(p[i] & temp)
Display3x3Pixels(x+j,y+i,ON);
else
Display3x3Pixels(x+j,y+i,OFF);
temp>>=1;
}
}
}
/*
*
*/
void showCubeMap(void)
{
uchar hang,lie;
for(hang=0;hang<MAXHANG;hang++)
{
if(cubeMap[hang]!=0) //跳过某行全为零的地方
for(lie=0;lie<MAXLIE;lie++)
{
if(cubeMap[hang]&(0x8000>>(lie%16)))
Display3x3Pixels(lie,hang,ON);
}
}
}
/*
*
*/
void createCube(void)
{
static uchar next=0;
this.cube=next;
next=randomNumber%7; //产生随机数
this.row=0; //初始行为0
this.column=6; //初始列为6
this.state=0; //初始状态为0
this.box=cube+16*this.cube; //设定方块指针起始地址
showNextCube(cube+16*next,NexPoinX,NexPoinY); //提示区显示下一个方块
timeA=0;
downflag=0;
}
/*
*
*/
void writeCubeToMap(uchar mode)
{
uchar tempData,tempRow,tempColumn,i,j,k=0;
for(i=0;i<4;i++)
{
tempData=this.box[i];
if(tempData==0)
continue;
for(j=0;j<4;j++)
{
if(tempData&0x08)
{
tempRow=this.row+k;
tempColumn=this.column+j;
ChangeRamBit(tempRow,tempColumn,mode);
Display3x3Pixels(tempColumn,tempRow,mode);
}
tempData<<=1;
}
k++;
}
}
/*
*
*/
uchar checkClask(void)
{
uchar tempData,tempRow,tempColumn,i,j,k=0;
for(i=0;i<4;i++)
{
tempData=this.box[i];
if(tempData==0) //跳过全为零的行
continue;
for(j=0;j<4;j++)
{
if(tempData&0x08)
{
tempRow=this.row+k;
tempColumn=this.column+j;
if (
ReadDisplayRamBit(tempRow,tempColumn) //与边上的方块接触
||(tempRow>=MAXHANG) //到了最底部
||(tempColumn>=MAXLIE) //到了最右边
||(tempColumn==255) //到了最左边
)
return 1; //返回1,停止移动
}
tempData<<=1;
}
k++;
}
return 0; //返回0,可以继续移动
}
/*
*
*/
void CheckMap()
{
uchar fullFlag=0;
static uchar tempScore=0,tempSpeed=0;
char i,j;
for(i=MAXHANG-1;i>0;i--)
{
if(cubeMap[i]==0xffff)
{
fullFlag++;
for(j=i;j>0;j--)
{
if(j>0)
cubeMap[j]=cubeMap[j-1];
else
cubeMap[j]=0;
}
i++;
}
}
tempScore=Score;
tempSpeed=Speed;
switch(fullFlag)
{
case 1:Score++;break;
case 2:Score+=2;break;
case 3:Score+=4;break;
case 4:Score+=6;break;
default:break;
}
if(Score>99)
{
Speed++;
if(Speed>9)
Speed=1;
Score=0;
}
if(tempScore!=Score)
Number2BitDisplay(7,87,Score);
if(tempSpeed!=Speed)
Number2BitDisplay(6,87,Speed);
if(fullFlag)
{
GameMapClear();
showCubeMap();
}
}
/*
*
*/
void moveLeft(void)
{
writeCubeToMap(OFF);
this.column--;
if(checkClask())
this.column++;
writeCubeToMap(ON);
}
/*
*
*/
void moveRigh(void)
{
writeCubeToMap(OFF);
this.column++;
if(checkClask())
this.column--;
writeCubeToMap(ON);
}
/*
*
*/
uchar moveDown(void)
{
writeCubeToMap(OFF);
this.row++;
if(checkClask()) //到底
{
this.row--;
writeCubeToMap(ON);
CheckMap(); //是否消行
return 1;
}
else
{
writeCubeToMap(ON);
return 0;
}
}
/*
*
*/
void cubeRotation(void)
{
uchar temp;
temp=this.state;
writeCubeToMap(OFF);
this.state=++this.state%4;
this.box=cube+16*this.cube+4*this.state;
if(checkClask())
{
this.state=temp;
this.box=cube+16*this.cube+4*this.state;
}
writeCubeToMap(ON);
}
/*
*
*/
void TetrisGameOver()
{
DisplayRamClear();
GameMapClear();
Dogs102x6_stringDraw(3, 13, "GAME",ON);
Dogs102x6_stringDraw(4, 13, "OVER",ON);
}
/*
*
*/
void TetrisGame()
{
Key=0;
buttonsPressed = 0;
Gameover=0;
temp=0;
while (!(buttonsPressed & BUTTON_S2))
{
static uchar Flag=0;
if(Flag==0)
{
createCube();
if(checkClask())
{
TetrisGameOver();
delay_ms(100);
GameMapClear();
Gameover=1;
break;
}
else
writeCubeToMap(ON);
Flag=1;
}
temp=Key;
switch(temp)
{
case 1:cubeRotation();
break;
case 2:while(moveDown()==0);Flag=0;
break;
case 3:moveLeft();
break;
case 4:moveRigh();
break;
default:
break;
}
Key=0;
temp=0;
if(downflag&&Flag)
{
downflag=0;
if(moveDown())
{
Flag=0;
}
}
}
}
12864与1602写程序都是相似的 都是从51的移植过来的 现在为了方便都是用12864串行显示
我写的那个程序也是从51单片机上移植过来的,而且在51上成功的显示了,但是移植过来之后就显示不了了,我也不知道是哪里出了问题
一周热门 更多>