贪吃蛇实验(3)

2018-12-05 21:44

TFTLCD_DrawHorizontalLine(120,150,120,GREEN);//往上按钮 TFTLCD_DrawHorizontalLine(120,150,150,GREEN); TFTLCD_DrawVerticalLine(120,150,120,GREEN); TFTLCD_DrawVerticalLine(120,150,150,GREEN); TFTLCD_ShowString(125,130,\,WHITE,BLACK);

TFTLCD_DrawHorizontalLine(70,100,170,GREEN);//往左按钮 TFTLCD_DrawHorizontalLine(70,100,200,GREEN); TFTLCD_DrawVerticalLine(170,200,70,GREEN); TFTLCD_DrawVerticalLine(170,200,100,GREEN); TFTLCD_ShowString(75,180,\,WHITE,BLACK);

TFTLCD_DrawHorizontalLine(170,200,170,GREEN);//往右按钮 TFTLCD_DrawHorizontalLine(170,200,200,GREEN); TFTLCD_DrawVerticalLine(170,200,170,GREEN); TFTLCD_DrawVerticalLine(170,200,200,GREEN); TFTLCD_ShowString(175,180,\,WHITE,BLACK);

SysCtlDelay(1000*(20000000/100));//

TFTLCD_FillBlock(0, 240, 0, 400, BLACK); } void

Rand()//产生一个随机数组 {

uint32_t i;

srand((unsigned) time(NULL)); for(i=0;i<50;i++) {

a[i].x=rand()*10 % 200+20;//20~220 a[i].y=rand()*10 % 300+40;//20~340 } }

voidmain() {

sc=5;//蛇长度 uint32_t j,i;

uint32_t num=0;//随机数组的编号 SYSCTL_RCGCGPIO_R |= (

SYSCTL_RCGCGPIO_R13 |

SYSCTL_RCGCGPIO_R12 | SYSCTL_RCGCGPIO_R11 |SYSCTL_RCGCGPIO_R10| SYSCTL_RCGCGPIO_R7 | SYSCTL_RCGCGPIO_R3 );

GPIO_PORTN_DIR_R = 0x03; GPIO_PORTM_DIR_R = 0x28; GPIO_PORTH_AHB_DIR_R = 0x0c; GPIO_PORTP_DIR_R = 0x00; GPIO_PORTD_AHB_DIR_R = 0x02; //GPIO_PORTF_AHB_DIR_R = 0x11; GPIO_PORTL_DIR_R = 0x0f;

GPIO_PORTN_DEN_R = 0x0f; GPIO_PORTM_DEN_R = 0x28; GPIO_PORTH_AHB_DEN_R = 0x0c; GPIO_PORTP_DEN_R = 0x04; GPIO_PORTD_AHB_DEN_R = 0x03; GPIO_PORTL_DEN_R = 0x0f; //uint32_t mb=0;

// The FPU should be enabled because some compilers will use floating- // point registers, even for non-floating-point code. If the FPU is not // enabled this will cause a fault. This also ensures that floating- // point operations could be added to this anumlication and would work // correctly and use the hardware floating-point unit. Finally, lazy // stacking is enabled for interrupt handlers. This allows floating- // point instructions to be used within interrupt handlers, but at the // expense of extra stack usage. FPUEnable();

FPULazyStackingEnable(); // Run from the PLL at 120 MHz.

g_ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); //

// Enable interrupts to the processor.

//

IntMasterEnable(); //

// Enable the SysTick Interrupt. //

SysTickIntEnable(); //

// Enable SysTick. //

SysTickEnable(); ConfigureUART(); EPIGPIOinit();

TFT_400x240_OTM4001Ainit(g_ui32SysClock); // Open BackLight.

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0); GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0, GPIO_PIN_0); pingmu(); IntMasterEnable();

Rand();//根据系统时间产生随机数 //食物点初始化 food.x=50; food.y=200; //蛇初始化 SnackInit(); while(1) {

TFTLCD_DrawPoint(food.x,food.y,WHITE); //在屏幕下方显示吃到的食物的个数

TFTLCD_ShowString(0,350,\,WHITE,BLACK); TFTLCD_ShowData(180,350,sum,WHITE,BLACK); //判断,蛇头不能碰到蛇身 for(i=0;i

if(snack[sc].x==snack[i].x&&snack[sc].y==snack[i].y) {

TFTLCD_FillBlock(1, 240, 0, 400, BLACK);

}

TFTLCD_ShowString(50,150,\,WHITE,BLACK); SysCtlDelay(1000*(20000000/300)); TFTLCD_FillBlock(1, 240, 0, 400, BLACK); SnackInit(); start=0; sc=5; k=0; s=300; sum=0; }

if(start!=1) {

//开始游戏提示 }

identify_key(); if(key==6) start=1;

//判断方向,防止反向运行 if(key!=0) {

if((k==5||k==6)&&key==7)

k=5;

TFTLCD_ShowString(60,120,\,WHITE,BLACK); TFTLCD_ShowString(55,160,\,WHITE,BLACK); TFTLCD_ShowString(55,200,\,WHITE,BLACK); identify_key(); if(key==6)

{

TFTLCD_FillBlock(0, 240, 0, 400, BLACK);

SnackInit();

}

else

if(k==7&&key==5)

k=7;

else

}

if(k==2&&key==10)

k=2;

else

if(k==10&&key==2)

k=10;

else

k=key;

if(start==1) {

if(sum>= 10)//吃到十个食物时判定为游戏成功 {

TFTLCD_FillBlock(1, 240, 0, 400, BLACK); //清屏

TFTLCD_ShowString(50,150,\,WHITE,BLACK); SysCtlDelay(1000*(20000000/300));

TFTLCD_FillBlock(1, 240, 0, 400, BLACK); SnackInit(); start=0; sc=5; k=0; s=300; sum=0; }

if(k==5||k==6)//按下N5按钮或开始游戏时向下走 {

SnackDown();

if(snack[sc].x==food.x&&snack[sc].y==food.y)

{

j=sc;

sc=sc+1;

snack[sc].x=snack[j].x; snack[sc].y=snack[j].y+10;

TFTLCD_DrawLine(snack[j],snack[sc],GREEN);

food.x=a[num].x; food.y=a[num].y;

TFTLCD_DrawPoint(food.x,food.y,WHITE);


贪吃蛇实验(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:博雅汉语初级起步篇生词1-42

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: