12864贪吃蛇

  • A6_498197
    了解作者
  • 68.7KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-21 04:08
    上传日期
51单片机fc手柄的12864贪吃蛇的程序,12864型号为QC12864B
贪食蛇 2.0.zip
  • 贪食蛇 2.0
  • Listings
  • main.lst
    15.3KB
  • FC.lst
    590B
  • 12864贪食蛇.m51
    35.2KB
  • lcd12864.lst
    13.6KB
  • Objects
  • 12864贪食蛇.build_log.htm
    1KB
  • lcd12864.obj
    14.4KB
  • 12864贪食蛇.hex
    9.2KB
  • 12864贪食蛇.plg
    180B
  • 12864贪食蛇.lnp
    151B
  • uart.h
    1.8KB
  • 12864贪食蛇2.0.uvgui.陈涌楠
    69.8KB
  • uart.c
    2.4KB
  • 12864贪食蛇2.0.uvgui_Administrator.bak
    70.6KB
  • 12864贪食蛇.uvgui.Administrator
    137.4KB
  • 12864贪食蛇2.0.uvopt
    5.6KB
  • 12864贪食蛇2.0_uvopt.bak
    5.6KB
  • 12864贪食蛇2.0.uvgui.Administrator
    71.5KB
  • 12864贪食蛇2.0.uvproj
    14.1KB
  • lcd12864.h
    1.3KB
  • 12864贪食蛇2.0_uvproj.bak
    14.1KB
  • lcd12864.c
    7.5KB
  • main.c
    8.7KB
内容介绍
/* 贪食蛇程序 2016年10月6日20:02:37 */ #include "reg52.h" #include "lcd12864.h" #include "FC.H" #include "stdlib.h" #define snake_UP 0x01 #define snake_DOWN 0xfe #define snake_LEFT 0x02 #define snake_RIGHT 0xfd #define snakeInitLength 2 #define snakeInitSideX 70 #define snakeInitSideY 30 #define snakeInitDir snake_RIGHT #define arraysize 100 #define LEFT_X 63 #define TOP_Y 0 #define RIGHT_X 127 #define BOTTOM_Y 63 unsigned char xdata stepArray[arraysize]={0}; unsigned char xdata dirArray[arraysize]={0}; unsigned char front_array,tail_array;//存放头尾数据的数组编号 unsigned char front_dir,tail_dir;//头尾移动的方向 unsigned char frontdat,taildat; //头尾的段数 char tail_x,tail_y,front_x,front_y;//头尾的坐标 unsigned char food_x,food_y;//食物坐标 unsigned int score=0; unsigned int time_speed; unsigned char scoreString[5]={0}; unsigned char flag_food=0,flag_skip=0; unsigned int speed = 1; unsigned char power = 1; void toString(unsigned char* str) //将分数转换为ask码的形式 { str[0] = score/1000+0x30; str[1] = score%1000/100+0x30; str[2] = score%100/10+0x30; str[3] = score%10+0x30; } //生成食物 void CreateFood() { do { food_x = rand ()%LEFT_X+LEFT_X+1; food_y = rand ()%(BOTTOM_Y-1); }while(LCD12864_Readpoint(food_x,food_y)); //读点是为了让食物的点在蛇身之外 power = rand()%2+1; time_speed = (250-speed*10)/power; LCD12864_Drawpoint(food_x,food_y,DISPLAY); //将点在12864显示出来 } //画界面 void drawGUI() { unsigned char i=0; LCD12864_String(0,0," "); LCD12864_String(0,1," "); LCD12864_String(0,2," "); LCD12864_String(0,3," "); toString(scoreString); LCD12864_String(0,0,"分数: "); LCD12864_String(0,1,scoreString); LCD12864_String(0,2,"称号: "); LCD12864_String(0,3,"小虾米 "); for(i=LEFT_X;i<RIGHT_X;i++) //画上下边框 { LCD12864_Drawpoint(i,TOP_Y,DISPLAY); LCD12864_Drawpoint(i,BOTTOM_Y,DISPLAY); } for(i=0;i<BOTTOM_Y;i++) //画左右边框 { LCD12864_Drawpoint(LEFT_X,i,DISPLAY); LCD12864_Drawpoint(RIGHT_X,i,DISPLAY); } //画蛇 for(i=0;i<snakeInitLength;i++) { LCD12864_Drawpoint(i+snakeInitSideX,snakeInitSideY,DISPLAY); } //食物 do { food_x = rand ()%LEFT_X+LEFT_X+1; food_y = rand ()%(BOTTOM_Y-1); }while(LCD12864_Readpoint(food_x,food_y)); //power = rand()%2+1; time_speed = (250-speed*10); LCD12864_Drawpoint(food_x,food_y,DISPLAY); } //初始化 void Snake_Init() { score = 0; flag_food=0; flag_skip=0; speed = 1; power = 1; front_x = snakeInitSideX + snakeInitLength - 1; front_y = snakeInitSideY; tail_x = snakeInitSideX; tail_y = snakeInitSideY; stepArray[0] = snakeInitLength; dirArray[0] = snakeInitDir; tail_array = front_array = 0; time_speed = (250-speed*10)/power; front_dir = dirArray[0]; tail_dir = dirArray[0]; frontdat = stepArray[0]; taildat = stepArray[0]; drawGUI(); } //游戏结束界面 void GAMEOVER() { LCD12864_clearGDR(); toString(scoreString); LCD12864_String(0,0," "); LCD12864_String(0,1," "); LCD12864_String(0,2," "); LCD12864_String(0,3," "); LCD12864_String(0,0," GAME OVER "); LCD12864_String(0,1," SCROE: "); LCD12864_String(5,1,scoreString); LCD12864_String(0,2," START "); while(1) { if(FC_ReadDATA() == FC_START) break; } Snake_Init(); } //称号获得 void LEVEL_Name() { switch (score) { case 0 : LCD12864_String(0,3,"小虾米 ");speed =1; break; case 4 : LCD12864_String(0,3,"大虾米 ");speed =2; break; case 8 : LCD12864_String(0,3,"大豆虫 ");speed =3; break; case 12: LCD12864_String(0,3,"小青蛇 ");speed =4; break; case 20: LCD12864_String(0,3,"青蛇 ");speed =5; break; case 40: LCD12864_String(0,3,"白蛇精 ");speed =6; break; case 80: LCD12864_String(0,3,"白素贞 ");speed =7; break; case 160:LCD12864_String(0,3,"蛇界食神");speed =8; break; case 320 : LCD12864_String(0,3,"小白龙 ");speed =9; break; case 640:LCD12864_String(0,3,"逆天白龙 ");speed =10; break; case 1000:LCD12864_String(0,3,"通天神龙");speed =11; break; default: break; } } //画出贪食蛇 void drawSnake(unsigned char* stepArray,unsigned char* dirArray) { `````````` switch (front_dir) { case snake_UP : front_y--;if(front_y<(TOP_Y+1))front_y = (BOTTOM_Y-1); break; case snake_DOWN : front_y++;if(front_y>(BOTTOM_Y-1))front_y = (TOP_Y+1); break; case snake_LEFT : front_x--;if(front_x< (LEFT_X+1))front_x = (RIGHT_X-1); break; case snake_RIGHT: front_x++;if(front_x>(RIGHT_X-1))front_x = (LEFT_X+1); break; default: break; } frontdat = stepArray[front_array]; frontdat++; frontdat = frontdat%128; stepArray[front_array] = frontdat; //判断是食物还是自己身体 if(LCD12864_Readpoint(front_x,front_y)) //判断前进方向上有无点 { //判断是否为食物 if((front_x==food_x)&&(front_y==food_y)) { flag_food = 1; score++; toString(scoreString); LCD12864_String(0,1,scoreString); LEVEL_Name(); } else { flag_food = 0; GAMEOVER(); } } //画头 LCD12864_Drawpoint(front_x,front_y,DISPLAY); //消尾 if(flag_skip == 0) { LCD12864_Drawpoint(tail_x,tail_y,NOTDISPLAY); taildat = stepArray[tail_array]; taildat--; if(taildat == 0) { tail_array++; tail_array %= arraysize; //重新获取尾巴的段数和方向 taildat = stepArray[tail_array]; tail_dir = dirArray[tail_array]; }; stepArray[tail_array] = taildat; switch (tail_dir) { case snake_UP : tail_y--;if(tail_y<(TOP_Y+1))tail_y = (BOTTOM_Y-1); break; case snake_DOWN : tail_y++;if(tail_y>(BOTTOM_Y-1))tail_y = (TOP_Y+1); break; case snake_LEFT : tail_x--;if(tail_x<(LEFT_X+1))tail_x = (RIGHT_X-1); break; case snake_RIGHT: tail_x++;if(tail_x>(RIGHT_X-1))tail_x = (LEFT_X+1); break; } if(flag_food==1) { //判断前面是食物 if((front_x==food_x)&&(front_y==food_y)) { flag_food = 0; flag_skip = 1; CreateFood(); } } }else flag_skip = 0; } //FC手柄扫描 unsigned char FC_scan() { unsigned char dat; dat = FC_ReadDATA(); if(dat != 0xff) { if((dat&~FC_UP) ==0) return snake_UP; if((dat&~FC_DOWN)==0) return snake_DOWN; if((dat&~FC_LEFT)==0) return snake_LEFT; if((dat&~FC_RIGHT)==0) return snake_RIGHT; } return 0; } //手柄数据处理函数 void MakeFCscan(unsigned char* stepArray,unsigned char* dirArray) { unsigned char fcdat; fcdat = FC_scan(); //手
评论
    相关推荐
    • 51单片机
      51单片机
    • 51单片机教程
      来自平凡单片机网站(word)
    • 51单片机程序
      LED流水灯实验是能较好的引导单片机爱好者入门学习,该程序实现了LED间隔10ms循环点亮
    • 51单片机程序
      包含51单片机的基础程序,以及综合程序。供初学者学习,及编写一定模块程序
    • 51单片机万年历
      该资源为51单片机资源,采用DS1302为时钟源,用LCD1602进行显示,压缩包里面还有protuse的仿真图,从程序到仿真齐全。
    • 51单片机WIFI
      利用wifi对51单片机进行控制,内附原理图及PCB板制作
    • 51单片机相关
      哈哈很经典的单片机开发仿真,对于学单片机不想花很多钱的同学来说是一个不错的选择。
    • 51单片机学习
      适合想学习51单片机的新人,能够快速学习,里面有多种实例可以方便学习。另外有很多视频,如果想要的话可以发邮件到:www.gaoju1024@qq.com
    • 51单片机教程
      51单片机的初级教程,里面包含单片机的各个模块的详细介绍,以及各个模块的验证程序,能够学会单片机的操作
    • 单片机课件 51单片机
      课件 51单片机 C程序设计