嵌入式系统课程设计——贪吃蛇_第1页
嵌入式系统课程设计——贪吃蛇_第2页
嵌入式系统课程设计——贪吃蛇_第3页
嵌入式系统课程设计——贪吃蛇_第4页
嵌入式系统课程设计——贪吃蛇_第5页
已阅读5页,还剩38页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、word/* *;*-;*-;* 文件作者 : ARM开发小组;* 创立日期 : 2004/06/25;*-;*-;*;*/#include ".incconfig.h"#include<stdlib.h>#include<time.h>#include<stdio.h>#defineSTACKSIZE256OS_STK_DATA stk;extern GUI_FONT CHINESE_FONT12;extern GUI_FONT CHINESE_FONT16;extern GUI_FONT GUI_Font8x16;OS_EVENT *

2、Key_Mbox;/* 分配各任务的堆栈容量 */OS_STK Stack_Task_1STACKSIZE;OS_STK Stack_Task_2STACKSIZE;OS_STK Stack_Task_3STACKSIZE;OS_STK Stack_Task_4STACKSIZE*3;/*/#define MAX_X 200 /蛇的最大长度#define MAX_Y 200#define UP 1 /蛇的方向#define DOWN 2#define LEFT 3#define RIGHT 4#define ROW 22 /表格行数#define COLUMN 22 /表格列数/定义蛇的各项信

3、息struct Snake S32 xMAX_X; S32 yMAX_Y; S32 length; S32 direction; S32 life;snake;struct Food int x;/*食物的横坐标*/ int y;/*食物的纵坐标*/ int yes;/*判断是否要出现食物的变量*/food;/*食物的结构体*/S16 map_12222 =/ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

4、 0, 0, 0, 0, 0, 0, 0, 0 ,/0 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ,/1 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0 ,/2 0, 0, 1, 0, 0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, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 ,/9 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/10 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

12、 0, 0, 1 ,/11 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1 ,/12 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/13 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/14 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/15 1, 0,

13、0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/16 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/17 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/18 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/19 1, 0, 0, 0, 0, 0, 0, 0, 0,

14、 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ,/20 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /21 ; int oldDire = 4; /记录前一个方向int score = 0; /分数int gametime = 99;/时间int mapSelect = 0;/关卡int gamespeed = 0;/难度,速度int key_F = 0;/标记F键int setUnit = 0;/设置游戏项,0表示关卡,1表示难度int foodIndex = 0; /第几个

15、食物int mySrand = 0;void Draw_GUI();/绘制游戏界面void Random_Food();/产生食物void GameOver();/结束游戏void NewGame();/新游戏void PrScore();/输出新得分void DspTime(INT32U);/显示倒计时void DrawMap();/画障碍物void CheckMap();/ 判断蛇是否撞到障碍物void Set_SelectFace();/游戏设置void DrawSetForm();/游戏设置键void Task_2();/*- 函数名称 : Task_4(void *pdata)- 函

16、数说明 : GUI任务,优先级为56- 输入参数 : pdata- 输出参数 : 无*/void Task_4(void *pdata)I32 k = 0;int i = 0;int j = 0;int x = 0;int y = 0;snake.life = 0; /活着 snake.x0=100; snake.y0=40; /蛇头 snake.x1=80; snake.y1=40; /第一节蛇 snake.x2=60; snake.y2=40; snake.length=3; /节数 snake.direction = RIGHT; food.yes=0; while(1) if(scor

17、e >= 100) score = 0; mapSelect +; Set_Color(GUI_GREEN); Set_Font(&CHINESE_FONT16); Disp_String (CN_start"恭喜你,进入下一关!"CN_end,240,240); OSTimeDly(200); NewGame(); if(key_F = 1)/是否进入游戏设置 /OSTimeDly(100); OSTimeDly(50000); if(snake.x0 = (food.x*20)+20 && snake.y0=(food.y*20)+20)/

18、*吃到食物以后*/Set_Color(GUI_WHITE);/用白色去除蛇的的最后一节Fill_Rect(food.x*20)+20,(food.y*20)+20,(food.x*20)+40,(food.y*20)+40);snake.length+;/蛇的身体长一节food.yes=0;/画面上需要出现新的食物score += 10;PrScore();/输出新得分 x = snake.xsnake.length-1;y = snake.ysnake.length-1; for(i=snake.length-1;i>0;i-)/蛇的每个环节往前!snake.xi=snake.xi-1

19、;snake.yi=snake.yi-1;Set_Color(GUI_GREEN);/画出蛇for(j=0;j<snake.length;j+)Fill_Rect(snake.xj,snake.yj,snake.xj+20,snake.yj+20); if(food.yes=0) / 画食物 Random_Food(); Uart_Printf(" random x=%xn",food.x); Uart_Printf(" random y=%xn",food.y); Set_Color(GUI_RED);/设置食物颜色 红色 Fill_Circle

20、(food.x*20)+30,(food.y*20)+30,10); /画食物,为红色的圆 switch(snake.direction) /蛇的方向 case UP: if(oldDire != DOWN) snake.y0 -= 20; oldDire = snake.direction; else snake.y0 += 20; break; case DOWN: if(oldDire != UP) snake.y0 += 20; oldDire = snake.direction; else snake.y0 -= 20; break; case LEFT: if(oldDire !=

21、 RIGHT) snake.x0 -= 20; oldDire = snake.direction; else snake.x0 += 20; break; case RIGHT: if(oldDire != LEFT) snake.x0 += 20; oldDire = snake.direction; else snake.x0 -= 20; break; default: break; Uart_Printf("snake.x0=%d* snake.y0=%dn",snake.x0,snake.y0); for(i=3;i<snake.length;i+)/是否

22、撞到自己了,因为蛇头为两节,第三节不可能拐过来if(snake.xi=snake.x0&&snake.yi=snake.y0) snake.life=1; break; CheckMap();/ 判断蛇死亡条件 if(snake.x0<20)snake.x0=440;else if(snake.x0 > 440)snake.x0=20;else if(snake.y0 > 440)snake.y0=20;else if(snake.y0 <20)snake.y0=440; if(gametime <=0)/时间到了 snake.life=1; /蛇

23、死 Uart_Printf(" >>>>>>>>>>>>>>>>>>>>>>snake.life =%dn",snake.life);Uart_Printf(" >>>>>>>>>>>>>>>>>>>>>>snake.direction =%dn",snake.direction);if(sna

24、ke.life = 1)/蛇是否死了 GameOver();/本次游戏结束 OSTimeDly(50000); if(gamespeed = 0)/易 for(k=0;k<10000000;k+); else if(gamespeed = 1)/中 for(k=0;k<1000000;k+); else if(gamespeed = 2)/难 for(k=0;k<100000;k+); else for(k=0;k<10000000;k+); Set_Color(GUI_WHITE);/用灰色去除蛇的的最后一节Fill_Rect(x,y,x+20,y+20);OSTim

25、eDly(10); /*- 函数名称 : Task_3- 函数说明 : 任务3,向串口发送数据- 输入参数 : pdata- 输出参数 : 无*/void Task_3(void *pdata) for(;) if(key_number!=0xff) Uart_Printf("key_number=%xn",key_number); /任务的干得活儿就是向超级终端发送内容 key_number=0xff; OSTimeDly(30); /延时100个节拍 /OSTimeDly(100); void Task_1(void *pdata) INT8U err; INT8U *

26、Key_P; INT8U Key_Val; int ys = 0; char cmapSelect2; Timer1_Init(); /翻开时钟节拍,让操作系统跑起来 / OSTaskCreate(Task_2, (void *)0, (OS_STK *)&Stack_Task_2(STACKSIZE) - 1, 9); /在任务里创立另一个任务/ OSTaskCreate(Task_3, (void *)0, (OS_STK *)&Stack_Task_3(STACKSIZE) - 1, 8); /在任务里创立另一个任务 /在任务里创立另一个任务 for(;) Key_P =

27、 OSMboxPend(Key_Mbox, 0, &err); /等待任务间的通信邮箱内的键值指针 Key_Val = *Key_P; switch(Key_Val) /int temp_mapSelect = 0;/设置时,选择的关卡/int temp_gamespeed = 0;/设置时,选择的难度case 0x05 : /GUI_StoreKey(GUI_KEY_UP); if(key_F = 1) if(setUnit = 0) /选择关卡 mapSelect = 1; /mapSelect +; /if(mapSelect = 3) /mapSelect = 2; else

28、if(setUnit = 1) /选择难易程度 gamespeed = 1; /gamespeed +; /if(gamespeed = 3) /gamespeed = 2; DrawSetForm(); break; case 0x06 : /上 /GUI_StoreKey(GUI_KEY_UP); if(key_F = 1) setUnit = 0; DrawSetForm(); snake.direction = UP; break; case 0x04 : /下 /GUI_StoreKey(GUI_KEY_DOWN); if(key_F = 1) setUnit = 1; DrawSe

29、tForm(); snake.direction = DOWN; break; case 0x01 : /右 /GUI_StoreKey(GUI_KEY_RIGHT); if(key_F = 1) if(setUnit = 0) mapSelect = 2; /mapSelect +; /if(mapSelect = 3) /mapSelect = 2; else if(setUnit = 1) gamespeed = 2; /gamespeed +; /if(gamespeed = 3) /gamespeed = 2; DrawSetForm(); snake.direction = RIG

30、HT; break; case 0x09 : /左 /GUI_StoreKey(GUI_KEY_LEFT); if(key_F = 1) if(setUnit = 0) mapSelect = 0; /mapSelect -; /if(mapSelect = -1) /mapSelect = 0; else if(setUnit = 1) gamespeed = 0; /gamespeed -; /if(gamespeed = -1) /gamespeed = 0; DrawSetForm(); snake.direction = LEFT; break; case 0x0F : /进入菜单

31、if(key_F = 0) key_F = 1; Set_SelectFace(); DrawSetForm(); Uart_Printf(" key_F =%dn",key_F); break; case 0x0B : /进入游戏界面 if(key_F = 1) key_F = 0; score = 0; gametime = 99; NewGame(); OSTimeDlyResume(8); else Draw_GUI(); /游戏界面初始化 PrScore();/初始化分数 DspTime(99);/初始化倒计时 DrawMap(); OSTaskCreate(Ta

32、sk_2, (void *)0, (OS_STK *)&Stack_Task_2(STACKSIZE) - 1, 4); OSTaskCreate(Task_4, (void *)0, (OS_STK *)&Stack_Task_4(STACKSIZE*3) - 1, 8);/创立任务4 break; /*case 0x0E : /重新开始游戏 score = 0; gametime = 99; NewGame(); break;*/ case 0x00: GUI_StoreKey(GUI_KEY_ENTER); break; default: break; OSTimeDly

33、(1); void Task_2(void *pdata) /记时INT8U hours = 0;INT8U minutes = 0;INT8U seconds = 1;INT16U milli = 0;while(1) OSTimeDly(300);if(snake.life = 0)gametime -;DspTime(gametime);PrScore();void Draw_GUI() /绘制游戏界面int i=0; int j=0; Set_Color(GUI_WHITE);/游戏背景 白 Fill_Rect(20,20,640,480); Set_Color(GUI_BLACK);

34、/设置墙 黑色 Set_Font(&CHINESE_FONT16); Fill_Rect(0,0,20,640); /左墙 Fill_Rect(0,0,640,20);/ 上墙 Fill_Rect(0,460,640,480); /下墙 Fill_Rect(460,0,480,480); /右墙 Set_Color(GUI_BLUE);/右边显示栏 蓝色 Fill_Rect(480,20,620,460); Set_Color(GUI_BLACK);/设置墙 黑色 Fill_Rect(620,0,640,480); /右边框 Set_BkColor(GUI_BLUE); Set_Col

35、or(GUI_WHITE); Disp_String (CN_start"关卡:"CN_end,500,60); Disp_String (CN_start"难易程度:"CN_end,500,110); Disp_String (CN_start"分数:"CN_end,500,250); Disp_String (CN_start"倒计时:"CN_end,500,360); void Random_Food() srand(mySrand+); food.x=rand()%22; srand(mySrand+3);

36、 food.y=rand()%22; food.yes = 1; switch(mapSelect)case 0: Uart_Printf("*case 0*foodIndex = %d n",foodIndex);break;case 1:Uart_Printf("*case 1*map_1food.xfood.y = %d n",map_1food.xfood.y);if(map_1food.yfood.x = 1) Uart_Printf("*1111*foodIndex = %d n",foodIndex); Random_F

37、ood(); Uart_Printf("*2222*foodIndex = %d n",foodIndex); break;case 2:if(map_2food.yfood.x = 1) Random_Food(); Uart_Printf("*case 2*foodIndex = %d n",foodIndex);break;default:break; Uart_Printf("*FOOD*food.x = %d n",food.x); Uart_Printf("*FOOD*food.y = %d n",food.y); void GameOver() /游戏结束Set_Font(&CHINESE_FONT16);Disp_String (CN_start"游戏结束!"CN_

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论