硬件准备
ADSP-EDU-BF533:BF533开发板
AD-HP530ICE:ADI DSP仿真器
软件准备
Visual DSP++软件
硬件链接
MEMS三轴加速度传感器
我做了一个三轴加速度传感器的子卡,插在这个板子上,然后写了一些有意思的应用程序。
代码实现功能
代码实现了通过 MEMS 控制贪吃蛇游戏。运行代码后,在液晶屏上看到贪吃蛇游戏界面,这时只要轻微倾斜板卡,贪吃蛇会开始移动,随着板卡倾斜的方向同,贪吃蛇前进方向会改变。控制时,板卡倾斜角度不要太大,每次通过倾斜改变蛇的方向后,要将板卡重新置于水平状态,再进行下次控制操作。游戏过关或结束后,可以看到当前分数,通过按板卡右侧的任意四个按键,进行重新开始游戏。
代码使用说明
代码将读取的 MEMS 坐标值,将坐标转为倾斜等级,当倾斜等级大于 3 时,为有效的倾斜操作,然后根据获取的数据,设置有效键值,将有效键值传给贪吃蛇函数,控制贪吃蛇前进方向。
key_get(); //获取倾斜等级值。
if(up1>=3) //判断是否为有效倾斜操作
{
Key_Value = 1; //设置键值
}
if(down1>=3)
{
Key_Value = 4;
}
if(left1>=3)
{
Key_Value = 2;
}
if(right1>=3)
{
Key_Value = 3;
}
Run_SNAKE(); //运行贪吃蛇游戏函数
代码实验步骤
- 将板卡连接仿真器,将 MEMS 子卡板正确插入扩展接口,板卡上电,运行 VDSP 软件并连接板卡。
- 将工程 BF53x_MEMS_SNAKE.dpj 载入 VDSP 软件,编译并运行。
- 轻微倾斜板卡,开始贪吃蛇游戏。
通过倾斜板卡,控制贪吃蛇进行吃果子游戏。
程序源码
#include <cdefBF533.h>
#include"adxl345.h"
#define DELAY_DATA 500
void SPIinit(void)
{
*pSPI_BAUD = 50;
*pSPI_FLG |=FLS2;
*pSPI_CTL = 0x1001|CPHA| CPOL|EMISO ;
*pSPI_CTL = (*pSPI_CTL | SPE);
}
unsigned char spi_byte_rw(unsigned char value)
{
unsigned char incoming=0;
while(!(*pSPI_STAT & SPIF));
*pSPI_TDBR = value;
while(*pSPI_STAT & RXS)
incoming = *pSPI_RDBR;
return(incoming);
}
adxl345_write(unsigned char data,unsigned char address)
{
*pSPI_FLG &= ~FLG2;
delay(DELAY_DATA);
spi_byte_rw(address|0x40);
spi_byte_rw(data);
delay(DELAY_DATA);
*pSPI_FLG |= FLG2;
delay(DELAY_DATA);
}
unsigned char adxl345_read(unsigned char address)
{
unsigned char read_data;
*pSPI_FLG &= ~FLG2;
delay(DELAY_DATA);
spi_byte_rw(address|0xc0);
delay(DELAY_DATA);
read_data = spi_byte_rw(0xff);
delay(DELAY_DATA);
read_data = spi_byte_rw(0xff);
delay(DELAY_DATA);
*pSPI_FLG |= FLG2;
delay(DELAY_DATA);
return read_data;
}
unsigned char adxl345_read_id(void)
{
unsigned char id = 0;
id = adxl345_read(DEVID);
return id;
}
void adxl345_init (void) //ADXL345初始化设置
{
adxl345_write(0xfe,OFSX); //X\Y\Z轴校正偏移
adxl345_write(0xfe,OFSY);
adxl345_write(0x08,OFSZ);
adxl345_write(0x77,ACT_INACT_CTL); //X\Y\Z轴使能
adxl345_write(0x01,BW_RATE); //功率选择及输出数据速率
adxl345_write(0x38,POWER_CTL); //测量、待机及测量模式控制
adxl345_write(0x0b,DATA_FORMAT); //数据及通信形式控制
adxl345_write(0x03,THRESH_ACT);
adxl345_write(0x03,THRESH_INACT);
adxl345_write(0x01,TIME_INACT); //加速度时间阈值
adxl345_write(0x77,ACT_INACT_CTL); //使能控制
adxl345_write(0xa0,THRESH_FF); //自由落体加速度阈值
adxl345_write(0xff,TIME_FF); //自由落体时间阈值
adxl345_write(0x00,TAP_AXES);
adxl345_write(0x77,ACT_TAP_STATUS);
adxl345_write(0x0a,BW_RATE);
adxl345_write(0x38,POWER_CTL);
}
unsigned short adxl345_read_xyzdat(unsigned short *buffer)
{
unsigned short tem_x = 0,tem_y = 0,tem_z = 0;
unsigned char i;
signed short dlXDiff0,dlXDiff1,dlXDiff2;
signed short dlYDiff0,dlYDiff1,dlYDiff2;
signed short dlZDiff0,dlZDiff1,dlZDiff2;
unsigned short ptx[9];
unsigned short pty[9];
unsigned short ptz[9];
unsigned short pax[3];
unsigned short pay[3];
unsigned short paz[3];
for(i=0;i<9;i++)
{
ptx[i] =(adxl345_read(DATAX1)<<8|adxl345_read(DATAX0))+256;
pty[i] =(adxl345_read(DATAY1)<<8|adxl345_read(DATAY0))+256;
ptz[i] =(adxl345_read(DATAZ1)<<8|adxl345_read(DATAZ0))+256;
}
///
pax[0]=(ptx[0]+ptx[1]+ptx[2])/3;
pax[1]=(ptx[3]+ptx[4]+ptx[5])/3;
pax[2]=(ptx[6]+ptx[7]+ptx[8])/3;
dlXDiff0 = pax[ 0 ] - pax[ 1 ];
dlXDiff1 = pax[ 1 ] - pax[ 2 ];
dlXDiff2 = pax[ 2 ] - pax[ 0 ];
dlXDiff0 = dlXDiff0 > 0 ? dlXDiff0 : -dlXDiff0;
dlXDiff1 = dlXDiff1 > 0 ? dlXDiff1 : -dlXDiff1;
dlXDiff2 = dlXDiff2 > 0 ? dlXDiff2 : -dlXDiff2;
if ( dlXDiff0 < dlXDiff1 )
{
if ( dlXDiff2 < dlXDiff0 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x = ( ( pax[ 0 ] + pax[ 1 ] ) >> 1 );
}
}
else if ( dlXDiff2 < dlXDiff1 )
{
tem_x = ( ( pax[ 0 ] + pax[ 2 ] ) >> 1 ) ;
}
else
{
tem_x= ( ( pax[ 1 ] + pax[ 2 ] ) >> 1 ) ;
}
///
pay[0]=(pty[0]+pty[1]+pty[2])/3;
pay[1]=(pty[3]+pty[4]+pty[5])/3;
pay[2]=(pty[6]+pty[7]+pty[8])/3;
dlYDiff0 = pay[ 0 ] - pay[ 1 ];
dlYDiff1 = pay[ 1 ] - pay[ 2 ];
dlYDiff2 = pay[ 2 ] - pay[ 0 ];
dlYDiff0 = dlYDiff0 > 0 ? dlYDiff0 : -dlYDiff0;
dlYDiff1 = dlYDiff1 > 0 ? dlYDiff1 : -dlYDiff1;
dlYDiff2 = dlYDiff2 > 0 ? dlYDiff2 : -dlYDiff2;
if ( dlYDiff0 < dlYDiff1 )
{
if ( dlYDiff2 < dlYDiff0 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y = ( ( pay[ 0 ] + pay[ 1 ] ) >> 1 );
}
}
else if ( dlYDiff2 < dlYDiff1 )
{
tem_y = ( ( pay[ 0 ] + pay[ 2 ] ) >> 1 ) ;
}
else
{
tem_y= ( ( pay[ 1 ] + pay[ 2 ] ) >> 1 ) ;
}
//
paz[0]=(ptz[0]+ptz[1]+ptz[2])/3;
paz[1]=(ptz[3]+ptz[4]+ptz[5])/3;
paz[2]=(ptz[6]+ptz[7]+ptz[8])/3;
dlZDiff0 = paz[ 0 ] - paz[ 1 ];
dlZDiff1 = paz[ 1 ] - paz[ 2 ];
dlZDiff2 = paz[ 2 ] - paz[ 0 ];
dlZDiff0 = dlZDiff0 > 0 ? dlZDiff0 : -dlZDiff0;
dlZDiff1 = dlZDiff1 > 0 ? dlZDiff1 : -dlZDiff1;
dlZDiff2 = dlZDiff2 > 0 ? dlZDiff2 : -dlZDiff2;
if ( dlZDiff0 < dlZDiff1 )
{
if ( dlZDiff2 < dlZDiff0 )
{
tem_z = ( ( paz[ 0 ] + paz[ 2 ] ) >> 1 ) ;
}
else
{
tem_z = ( ( paz[ 0 ] + paz[ 1 ] ) >> 1 );
}
}
else if ( dlZDiff2 < dlZDiff1 )
{
tem_z = ( ( paz[ 0 ] + paz[ 2 ] ) >> 1 ) ;
}
else
{
tem_z= ( ( paz[ 1 ] + paz[ 2 ] ) >> 1 ) ;
}
*buffer++ = tem_x;
*buffer++ = tem_y;
*buffer = tem_z;
}
#include <cdefBF533.h>
void Set_PLL(int pmsel,int pssel)
{
int new_PLL_CTL;
*pPLL_DIV = pssel;
asm(“ssync;”);
new_PLL_CTL = (pmsel & 0x3f) << 9;
*pSIC_IWR |= 0xffffffff;
if (new_PLL_CTL != *pPLL_CTL)
{
*pPLL_CTL = new_PLL_CTL;
asm(“ssync;”);
asm(“idle;”);
}
}
void Init_SDRAM(void)
{
*pEBIU_SDRRC = 0x00000817;
*pEBIU_SDBCTL = 0x00000013;
*pEBIU_SDGCTL = 0x0091998d;
ssync();
}
void Init_EBIU(void)
{
*pEBIU_AMBCTL0 = 0x7bb07bb0;
*pEBIU_AMBCTL1 = 0x7bb07bb0;
*pEBIU_AMGCTL = 0x000f;
}
void Init_Timers0(int dat)
{
*pTIMER0_CONFIG = 0x0019;
*pTIMER0_WIDTH = dat;
*pTIMER0_PERIOD = 2000;
}
void Enable_Timers0(void)
{
*pTIMER_ENABLE|= 0x0001;
asm(“ssync;”);
}
void Stop_Timers0(void)
{
*pTIMER_ENABLE &= ~0x0001;
}
void delay(volatile int tem)
{
volatile int i;
while(tem–)
for(i=6; i>0; i–);
}
#include <cdefBF533.h>
#include “i2c.h”
#define CORE_CLK_IN 25 * 1000 * 1000
#define SET_PF(pf)
do{
*pFIO_FLAG_S = (pf);
ssync();
}while(0)
#define CLR_PF(pf)
do{
*pFIO_FLAG_C = (pf);
ssync();
}while(0)
#define SET_PF_OUTPUT(pf)
do{
*pFIO_INEN &= ~(pf);
*pFIO_DIR |= (pf);
ssync();
}while(0)
#define SET_PF_INPUT(pf)
do{
*pFIO_DIR &= ~(pf);
*pFIO_INEN |= (pf);
ssync();
}while(0)
int get_core_clk(void)
{
int tempPLLCTL;
int _DF;
int VCO;
int MSEL1;
tempPLLCTL = *pPLL_CTL;
MSEL1 = ((tempPLLCTL & 0x7E00) >> 9);
_DF = tempPLLCTL & 0x0001;
VCO = MSEL1 * __CORE_CLK_IN__;
if(_DF == 1)
VCO /= 2;
return VCO;
}
void delay_ns(unsigned int core_clock, unsigned long long count)
{
count *= core_clock;
count /= 1000000000;
while(count–);
}
int _get_sdata(i2c_device * dev)
{
return ((*pFIO_FLAG_D & dev->sdata) ? 1 : 0);
}
void i2c_init(i2c_device * dev)
{
dev->core_clock = get_core_clk();
dev->delay_ns = delay_ns;
*pFIO_DIR |= dev->sclk | dev->sdata;
ssync();
}
void i2c_deinit(i2c_device * dev)
{
dev->sclk = 0;
dev->sdata = 0;
*pFIO_DIR &= ~(dev->sclk | dev->sdata);
ssync();
}
void i2c_start(i2c_device * dev)
{
SET_PF_OUTPUT(dev->sdata);
SET_PF_OUTPUT(dev->sclk);
SET_PF(dev->sdata);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->low_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
void i2c_stop(i2c_device * dev)
{
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_OUTPUT(dev->sdata);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_INPUT(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns);
}
int i2c_read_ack(i2c_device * dev)
{
int ret = 0;
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/3);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/3);
ret = _get_sdata(dev);
delay_ns(dev->core_clock, dev->high_ns/3);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
SET_PF_OUTPUT(dev->sdata);
return ret;
}
int i2c_wait_slave(i2c_device * dev, unsigned int time_out)
{
int ret;
int count = time_out * 2 / dev->high_ns;
SET_PF_INPUT(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/2);
do{
ret = *pFIO_FLAG_D & dev->sclk;
if(ret)
break;
delay_ns(dev->core_clock, dev->high_ns/2);
}while(count--);
SET_PF_OUTPUT(dev->sclk);
return !ret;
}
void i2c_write_ack(i2c_device * dev)
{
SET_PF_OUTPUT(dev->sdata);
CLR_PF(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/2);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
int i2c_write(i2c_device * dev, unsigned char value, int need_ack)
{
int ret = -1;
unsigned char index;
SET_PF_OUTPUT(dev->sdata);
//send 8 bits to slave
for(index = 0; index < 8; index++){
//send one bit to the i2c bus
if((value<<index) & 0x80){
SET_PF(dev->sdata);
} else {
CLR_PF(dev->sdata);
}
delay_ns(dev->core_clock, dev->low_ns/2);
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns/2);
}
if(need_ack){
ret = i2c_read_ack(dev);
}
return ret;
}
int i2c_read(i2c_device * dev, unsigned char * value, int send_ack)
{
unsigned char index;
*value = 0x00;
SET_PF_INPUT(dev->sdata);
delay_ns(dev->core_clock, dev->high_ns/2);
//get 8 bits from the device
for(index = 0; index < 8; index++){
SET_PF(dev->sclk);
delay_ns(dev->core_clock, dev->high_ns/2);
*value <<= 1;
*value |= _get_sdata(dev);
delay_ns(dev->core_clock, dev->high_ns/2);
CLR_PF(dev->sclk);
delay_ns(dev->core_clock, dev->low_ns);
}
// send ack to slave
if(send_ack){
i2c_write_ack(dev);
}
return *value;
}
#include <cdefBF533.h>
#include “cpld.h”
extern unsigned char DisplayBuffer[272][1440] ;
extern unsigned char DisplayBuffer_565[272][960] ;
extern unsigned char TempBuffer_img[272][960] ;
extern unsigned char Key_Value;
unsigned char led_temp_dat = 0xff;
unsigned int tem_x = 0,tem_y=0;
unsigned char buf[3];
unsigned char up1,down1,left1,right1;
unsigned char center =2;
unsigned short buf_data[3];
void key_get(void)
{
adxl345_read_xyzdat(buf_data);
tem_x = buf_data[0];
tem_y = buf_data[1];
//
if(tem_x>0&&tem_x<42)
{
up1 = 1;
down1 = 0;
}
if(tem_x>41&&tem_x<83)
{
up1 = 2;
down1 = 0;
}
if(tem_x>82&&tem_x<124)
{
up1 = 3;
down1 = 0;
}
if(tem_x>123&&tem_x<165)
{
up1 = 4;
down1 = 0;
}
if(tem_x>164&&tem_x<206)
{
up1 = 5;
down1 = 0;
}
if(tem_x>205&&tem_x<236)
{
up1 = 6;
down1 = 0;
}
//
if((tem_y>235&&tem_y<275)&&(tem_x>235&&tem_x<275))
{
center = 0;
up1=0;
down1=0;
left1=0;
right1=0;
}
else
{
center = 2;
}
if((tem_y>235&&tem_y<275)&&((tem_x>0&&tem_x<235)||(tem_x>275&&tem_x<600)))
{
center = 2;
// up1=0;
// down1=0;
left1=0;
right1=0;
}
if((tem_x>235&&tem_x<275)&&((tem_y>0&&tem_y<235)||(tem_y>275&&tem_y<600)))
{
center = 2;
up1=0;
down1=0;
// left1=0;
// right1=0;
}
if(tem_x>274&&tem_x<308)
{
down1 = 6;
up1 = 0;
}
if(tem_x>307&&tem_x<349)
{
down1 = 5;
up1 = 0;
}
if(tem_x>348&&tem_x<390)
{
down1 = 4;
up1 = 0;
}
if(tem_x>389&&tem_x<431)
{
down1 = 3;
up1 = 0;
}
if(tem_x>430&&tem_x<472)
{
down1 = 2;
up1 = 0;
}
if(tem_x>471&&tem_x<600)
{
down1 = 1;
up1 = 0;
}
///
if(tem_y>0&&tem_y<42)
{
left1 = 1;
right1 = 0;
}
if(tem_y>41&&tem_y<83)
{
left1 = 2;
right1 = 0;
}
if(tem_y>82&&tem_y<124)
{
left1 = 3;
right1 = 0;
}
if(tem_y>123&&tem_y<165)
{
left1 = 4;
right1 = 0;
}
if(tem_y>164&&tem_y<206)
{
left1 = 5;
right1 = 0;
}
if(tem_y>205&&tem_y<236)
{
left1 = 6;
right1 = 0;
}
///
if(tem_y>274&&tem_y<308)
{
right1 = 6;
left1 = 0;
}
if(tem_y>307&&tem_y<349)
{
right1 = 5;
left1 = 0;
}
if(tem_y>348&&tem_y<390)
{
right1 = 4;
left1 = 0;
}
if(tem_y>389&&tem_y<431)
{
right1 = 3;
left1 = 0;
}
if(tem_y>430&&tem_y<472)
{
right1 = 2;
left1 = 0;
}
if(tem_y>471&&tem_y<600)
{
right1 = 1;
left1 = 0;
}
}
void main(void)
{
int i;
Set_PLL(16,4);
Init_EBIU();
Init_SDRAM();
LCDBK_Disable();
ExtSPI0_Enable();
LED_Enable();
SPIinit();
adxl345_init();
InitDMA();
InitPPI();
InitTimer();
PPI_TMR_DMA_Enable();
Init_Timers0(1999);//1~1999 控制背光亮度
Enable_Timers0();
LCD_Enable();
LCDBK_Enable();
Init_SNAKE();
while(1)
{
for(i=0;i<8;i++)
{
key_get();
if(up1>=3)
{
Key_Value = 1;
}
if(down1>=3)
{
Key_Value = 4;
}
if(left1>=3)
{
Key_Value = 2;
}
if(right1>=3)
{
Key_Value = 3;
}
Run_SNAKE();
*pLED_DAT = ~(1<<i);
}
}
}
#include <stdlib.h>
#include"cpld.h"
int rand(void);
struct
{
unsigned char X;
unsigned char Y;
unsigned char Yes;
}Food; //食物结构体
#define SNAKE_Max_Long 8 //最大长度
struct
{
unsigned char X[SNAKE_Max_Long];
unsigned char Y[SNAKE_Max_Long];
unsigned char Long;
unsigned char Life;
unsigned char Score; //蛇的分数
unsigned char Level; //蛇的等级
}Snake; //蛇结构体
unsigned char Key_V_temp=0; //取出上次按键值
unsigned char Key_Value=0;
#define Key_Up 1
#define Key_Down 4
#define Key_Left 2
#define Key_Right 3
#define Key_Run 8
extern section(“sdram0_bank1”) unsigned char DisplayBuffer[272][1440];
extern section(“sdram0_bank1”) unsigned char DisplayBuffer_565[272][960];
void snake_delay(unsigned int n)
{
unsigned int i;
unsigned int j;
for(i=0;i<n;i++)
for(j=0;j<1000;j++) ;
}
unsigned char keyvalue ;
unsigned char key_read(void)
{
unsigned char keydata;
unsigned char keydata_temp;
keydata = ~*pKEYBOARD_DAT;
if(keydata != 0)
{
if(keydata&0x10) //右上
{
keyvalue = Key_Run;
}
else if(keydata&0x20) //右左
{
keyvalue = Key_Run;
}
else if(keydata&0x40) //右下
{
keyvalue = Key_Run;
}
else if(keydata&0x80) //右右
{
keyvalue = Key_Run;
}
else keyvalue = 0;
return keyvalue;
}
}
//=================================================================
//函数名称:Trun_On_Point Trun_Off_Point (unsigned char x,unsigned char y)
//函数功能:点亮或者熄灭 一个点。用于显示蛇身体
//入口参数:无
//出口参数:无
//特别说明:此 Trun_On,函数换成其他的就可以兼容其他的液晶
// 入88 点阵 12864 等
//=================================================================
void Trun_On_Point (int x,int y,unsigned int color)
{
Rect(x10,y*10,10,10, color);
}
void Trun_Off_Point (int x,int y)
{
Rect(x10,y10,10,10, 0);
}
//=================================================================
//函数名称:void Init_SNAKE(void)
//函数功能:绘制游戏界面,初始化游戏等级等参数
//入口参数:无
//出口参数:无
//=================================================================
void Init_SNAKE(void)
{
unsigned char i;
Rect(0,0,10,272, 0xffffff); //绘制边框
Rect(470,0,10,272, 0xffffff);
Rect(0,0,480,10, 0xffffff);
Rect(0,260,480,12, 0xffffff);
//***********初始化蛇参数********************//
Snake.Long=2; //定义初始化蛇的长度
Snake.Life=0; //初始化蛇还活着
Snake.Score=0;
Food.Yes=1;
Key_Value=0;
Key_V_temp = 0;
for(i=0;i<Snake.Long;i++) //将蛇给赋值
{
Snake.X[i]=i+2;
Snake.Y[i]=2;
}
for(i=0;i<Snake.Long;i++) //将蛇给画出来
{
Trun_On_Point(Snake.X[i],Snake.Y[i],0xff0000);
}
}
int all_Score = 0;
//=================================================================
//函数名称:void Display_dead(unsigned char i)
//函数功能:显示死亡界面
//入口参数:无
//出口参数:无
//=================================================================
void Display_dead(unsigned char i)
{
unsigned char buffer[10] = 0;
memset(DisplayBuffer,‘\0’,391680);
Glib_disp_hzk16_v(210,128,“游戏结束”,0xffff00);
Glib_disp_hzk16_v(210,152,“得分:”,0xffff00);
all_Score += Snake.Score;
buffer[3] = all_Score%10+0x30;
buffer[2] = all_Score/10%10+0x30;
buffer[1] = all_Score/100%10+0x30;
buffer[0] = all_Score/1000%10+0x30;
Glib_disp_ascii16x8_v(262,152,buffer,0xffff00);
RGB888_RGB565(DisplayBuffer,391680,DisplayBuffer_565);
while(1)
{
Key_Value = 0;
Key_Value = key_read();
if(Key_Value == Key_Run)
{
memset(DisplayBuffer_565,‘\0’,2261120);
Init_SNAKE();
break;
}
}
}
//=================================================================
//函数名称:void Display_Pass(unsigned char i)
//函数功能:显示过关界面
//入口参数:无
//出口参数:无
//=================================================================
void Display_Pass(unsigned char i)
{
unsigned char buffer[10] = 0;
memset(DisplayBuffer,‘\0’,391680);
Glib_disp_hzk16_v(210,136,“游戏开始”,0xffff00);
Glib_disp_hzk16_v(210,120,“第”,0xffff00);
buffer[1] = (Snake.Level+2)%10+0x30;
buffer[0] = (Snake.Level+2)/10%10+0x30;
Glib_disp_ascii16x8_v(234,120,buffer,0xffff00);
Glib_disp_hzk16_v(258,120,“关”,0xffff00);
Glib_disp_hzk16_v(210,152,“得分:”,0xffff00);
all_Score += Snake.Score;
buffer[3] = all_Score%10+0x30;
buffer[2] = all_Score/10%10+0x30;
buffer[1] = all_Score/100%10+0x30;
buffer[0] = all_Score/1000%10+0x30;
Glib_disp_ascii16x8_v(262,152,buffer,0xffff00);
RGB888_RGB565(DisplayBuffer,391680,DisplayBuffer_565);
Snake.Level++;
while(1)
{
Key_Value = key_read();
if(Key_Value == Key_Run)
{
memset(DisplayBuffer_565,‘\0’,2261120);
Init_SNAKE();
break;
}
}
}
//=================================================================
//函数名称:void Run_SNAKE(void)
//函数功能:蛇运动函数
//入口参数:无
//出口参数:无
//=================================================================
void Run_SNAKE(void)
{
unsigned char i=0;
/*********************根据按键选择蛇的运动方向*******************/
if((Key_Value==Key_Right&&Key_V_temp!=Key_Left)||(Key_Value==Key_Left&&Key_V_temp==Key_Right)) //
{
Trun_Off_Point(Snake.X[0],Snake.Y[0]); //先把蛇尾给熄灭掉
for(i=0;i<Snake.Long-1;i++) //蛇身都是它前一段的蛇身的位置
{
Snake.X[i]=Snake.X[i+1];
Snake.Y[i]=Snake.Y[i+1];
}
Snake.X[Snake.Long-1]=Snake.X[Snake.Long-2]+1;
Snake.Y[Snake.Long-1]=Snake.Y[Snake.Long-2]+0; //蛇头向你要求的方向移动
for(i=0;i<Snake.Long;i++) //这个让蛇显示出来的函数
{
Trun_On_Point(Snake.X[i],Snake.Y[i],0xff0000);
}
Key_V_temp=Key_Right;
}
else if((Key_Value==Key_Left&&Key_V_temp!=Key_Right)||(Key_Value==Key_Right&&Key_V_temp==Key_Left))
{
Trun_Off_Point(Snake.X[0],Snake.Y[0]); //先把蛇尾给熄灭掉
for(i=0;i<Snake.Long-1;i++) //蛇身都是它前一段的蛇身的位置
{
Snake.X[i]=Snake.X[i+1];
Snake.Y[i]=Snake.Y[i+1];
}
Snake.X[Snake.Long-1]=Snake.X[Snake.Long-2]-1;
Snake.Y[Snake.Long-1]=Snake.Y[Snake.Long-2]+0; //蛇头向你要求的方向移动
for(i=0;i<Snake.Long;i++) //这个让蛇显示出来的函数
{
Trun_On_Point(Snake.X[i],Snake.Y[i],0xff0000);
}
Key_V_temp=Key_Left;
}
else if(( Key_Value==Key_Up&&Key_V_temp!=Key_Down)||( Key_Value==Key_Down&&Key_V_temp==Key_Up))
{
Trun_Off_Point(Snake.X[0],Snake.Y[0]); //先把蛇尾给熄灭掉
for(i=0;i<Snake.Long-1;i++) //蛇身都是它前一段的蛇身的位置
{
Snake.X[i]=Snake.X[i+1];
Snake.Y[i]=Snake.Y[i+1];
}
Snake.X[Snake.Long-1]=Snake.X[Snake.Long-2]+0;
Snake.Y[Snake.Long-1]=Snake.Y[Snake.Long-2]-1; //蛇头向你要求的方向移动
for(i=0;i<Snake.Long;i++) //这个让蛇显示出来的函数
{
Trun_On_Point(Snake.X[i],Snake.Y[i],0xff0000);
}
Key_V_temp=Key_Up;
}
else if(( Key_Value==Key_Down&&Key_V_temp!=Key_Up)||( Key_Value==Key_Up&&Key_V_temp==Key_Down))
{
Trun_Off_Point(Snake.X[0],Snake.Y[0]); //先把蛇尾给熄灭掉
for(i=0;i<Snake.Long-1;i++) //蛇身都是它前一段的蛇身的位置
{
Snake.X[i]=Snake.X[i+1];
Snake.Y[i]=Snake.Y[i+1];
}
Snake.X[Snake.Long-1]=Snake.X[Snake.Long-2]+0;
Snake.Y[Snake.Long-1]=Snake.Y[Snake.Long-2]+1; //蛇头向你要求的方向移动
for(i=0;i<Snake.Long;i++) //这个让蛇显示出来的函数
{
Trun_On_Point(Snake.X[i],Snake.Y[i],0xff0000);
}
Key_V_temp=Key_Down;
}
if(Snake.Life==1)
{
Key_Value = 0;
Key_V_temp = 0;
}
/****************根据按键选择蛇的运动程序结束*******************/
/
/************************判断蛇是否死亡***********************/
if(Snake.X[Snake.Long-1]>=47||Snake.Y[Snake.Long-1]>=26)//判蛇头是否撞到墙壁
{
Snake.Life=1;
}
if(Snake.X[Snake.Long-1]<1||Snake.Y[Snake.Long-1]<1)//判蛇头是否撞到墙壁
{
Snake.Life=1;
}
for(i=3;i<Snake.Long;i++)//从第三节开始判断蛇头是否咬到自己
{
if(Snake.X[i]==Snake.X[0]&&Snake.Y[i]==Snake.Y[0])
{
Snake.Life=1;
}
}
if(Snake.Life==1) //if the snake over
{
i=Key_Value;
Key_Value = 0;
Display_dead( i);
}
/******************判断蛇是否死亡程序结束***********************/
/
/************************判断蛇是否最长了**********************/
if(Snake.Long==SNAKE_Max_Long)
{
i=Key_Value;
Key_Value = 0;
Display_Pass(i);
}
/
/*************************判蛇是否吃到食物**********************/
if(Snake.X[Snake.Long-1]==Food.X&&Snake.Y[Snake.Long-1]==Food.Y)//
{
Snake.Long++;//蛇节数加1
Snake.X[Snake.Long-1]=Food.X;
Snake.Y[Snake.Long-1]=Food.Y;
Snake.Score+=10;
Food.Yes=1; //食物标志置1
}
/
/***************************放果子程序*************************/
if((Snake.Life!=1) || (Snake.Long==SNAKE_Max_Long))
{
if(Food.Yes==0)
{
Trun_On_Point(Food.X,Food.Y,0xff00ff);
}
if(Food.Yes==1)
{
while(1)
{
//
do{
Food.X = rand();
}while((Food.X > 46) || (Food.X < 1));
do{
Food.Y = rand();
}while((Food.Y > 25) || (Food.Y < 1));
/
for(i=0;i<Snake.Long;i++) //判断产生的食物坐标是否和蛇身重合
{
if((Food.X==Snake.X[i])&&(Food.X==Snake.Y[i]))
break;
}
if(i==Snake.Long)
{
Food.Yes=0;
break; //产生有效的食物坐标
}
}
}
}
/
/**************************延时用于调整游戏速度************************/
if(Snake.Level>12)
{
snake_delay(0);
}
else
snake_delay(6000-Snake.Level*500);
/
}