找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2203|回复: 0
收起左侧

基于Cortex_M3的触摸屏版吃豆人源码

[复制链接]
ID:197722 发表于 2018-4-2 19:08 | 显示全部楼层 |阅读模式
基于Cortex_M3的触摸屏版吃豆人  

mcu源程序如下:
  1. #include "inc/lm3s9b96.h"
  2. #include "inc/hw_types.h"
  3. #include "driverlib/sysctl.h"
  4. #include "driverlib/timer.h"
  5. #include "inc/hw_memmap.h"
  6. #include "inc/hw_ints.h"
  7. #include "driverlib/interrupt.h"
  8. #include "driverlib/sysctl.h"
  9. #include "driverlib/uart.h"
  10. #include "math.h"

  11. #include "inc/hw_types.h"
  12. #include "inc/hw_timer.h"
  13. #include "inc/hw_gpio.h"
  14. #include "inc/hw_sysctl.h"
  15. #include "inc/hw_ints.h"
  16. #include "inc/hw_memmap.h"
  17. #include "inc/hw_nvic.h"

  18. #include "driverlib/interrupt.h"
  19. #include "driverlib/sysctl.h"
  20. #include "driverlib/rom.h"
  21. #include "driverlib/debug.h"
  22. #include "driverlib/gpio.h"
  23. #include "grlib/grlib.h"
  24. #include "grlib/widget.h"
  25. #include "grlib/canvas.h"
  26. #include "grlib/pushbutton.h"
  27. #include "drivers/kitronix320x240x16_ssd2119_8bit.h"
  28. #include "drivers/touch.h"
  29. #include "drivers/set_pinout.h"
  30. #include "driverlib/systick.h"
  31. #include "driverlib/timer.h"
  32. #include "inc/hw_timer.h"

  33. tBoolean g_press1 = false;
  34. tBoolean g_press2 = false;
  35. tBoolean g_press3 = false;
  36. tBoolean g_press4 = false;
  37. char p[9]={"00:00:00"};
  38. // 声明驱动库中定义的tDisplay显示设备对象。
  39. extern const tDisplay g_sKitronix320x240x16_SSD2119;
  40. // 提前声明要使用到的控件。(后文为建立控件树可能会相互引用)
  41. //extern tCanvasWidget g_sBackground;
  42. //extern tCanvasWidget g_sGame;
  43. extern tPushButtonWidget g_sDown;
  44. extern tPushButtonWidget g_sLeft;
  45. extern tPushButtonWidget g_sUp;
  46. extern tPushButtonWidget g_sRight;
  47. // 提前声明按钮按下时的处理凼数。
  48. void On_downPress(tWidget *pWidget);
  49. void On_leftPress(tWidget *pWidget);
  50. void On_upPress(tWidget *pWidget);
  51. void On_rightPress(tWidget *pWidget);



  52. //作为选择下的方块按钮
  53. RectangularButton(g_sDown,WIDGET_ROOT,&g_sLeft,0,
  54.                                                                         &g_sKitronix320x240x16_SSD2119, 270, 30, 40, 40,
  55.                                                                         (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
  56.                                                                         PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
  57.                                                                         ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
  58.                                                                         &g_sFontCmss22b, "R", 0, 0, 0, 0,
  59.                                                                         On_downPress);
  60. //作为选择左的方块按钮                                                                       
  61. RectangularButton(g_sLeft,WIDGET_ROOT,&g_sUp,0,
  62.                                                                         &g_sKitronix320x240x16_SSD2119, 270, 80, 40, 40,
  63.                                                                         (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
  64.                                                                         PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
  65.                                                                         ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
  66.                                                                         &g_sFontCmss22b, "D", 0, 0, 0, 0,
  67.                                                                         On_leftPress);
  68. //作为选择上的方块按钮                                                                       
  69. RectangularButton(g_sUp, WIDGET_ROOT,&g_sRight,0,
  70.                                                                         &g_sKitronix320x240x16_SSD2119, 270, 130, 40, 40,
  71.                                                                         (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
  72.                                                                         PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
  73.                                                                         ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
  74.                                                                         &g_sFontCmss22b, "L", 0, 0, 0, 0,
  75.                                                                         On_upPress);               
  76. //作为选择右的方块按钮                                                                       
  77. RectangularButton(g_sRight,WIDGET_ROOT,0,0,
  78.                                                                         &g_sKitronix320x240x16_SSD2119, 270, 180, 40, 40,
  79.                                                                         (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
  80.                                                                         PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
  81.                                                                         ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
  82.                                                                         &g_sFontCmss22b, "U", 0, 0, 0, 0,
  83.                                                                         On_rightPress);       
  84.                                                                                


  85. #define KEY_PERIPH SYSCTL_PERIPH_GPIOJ
  86. #define KEY_PORT  GPIO_PORTJ_BASE
  87. #define KEY_PIN  GPIO_PIN_7

  88. //吃豆人定义变量
  89. float man_x,man_y;//定义吃豆人的圆心位置
  90. float man_r=9;//定义吃豆人的半径
  91. float mdu;//定义吃豆人嘴的角度
  92. float mouth_x,mouth_y;//定义吃豆人嘴的边

  93. float warm_x,warm_y;//定义虫子位置
  94. float pac_x,pac_y;//定义豆子位置
  95. int flag=-1;
  96. int score;
  97. int i,j;
  98. //声明绘图上下文
  99. tContext sContext;
  100. tRectangle pRect;

  101. int move_x,move_y;
  102. //定义方向:1--下;2--左;3--上;4--右

  103. void delayms(int k)
  104. {
  105.         int i,j;
  106.         for(i=k;i>0;i--)
  107.                 for(j=20000;j>0;j--)        ;
  108. }

  109. //定义迷宫形状
  110. //1--墙 0--有豆子 2--曾有豆子,已被吃掉
  111.         unsigned char maze[16][12]=
  112.                                                          {{1,1,1,1,1,1,1,1,1,1,1,1},
  113.                                                                 {1,0,0,0,0,0,0,0,0,0,0,1},
  114.                                                                 {1,0,1,0,0,0,0,0,0,0,0,1},
  115.                                                                 {1,0,1,0,0,0,0,0,0,0,0,1},
  116.                                                                 {1,0,1,1,1,1,0,1,1,1,0,1},
  117.                                                                 {0,0,1,0,0,0,0,0,1,1,0,0},
  118.                                                                 {1,0,1,0,1,0,0,0,0,0,0,1},
  119.                                                                 {1,0,0,0,1,1,1,0,1,1,0,1},
  120.                                                                 {1,0,0,0,1,1,1,0,1,1,0,1},
  121.                                                                 {1,0,0,0,1,0,1,0,0,1,1,1},
  122.                                                           {1,0,0,0,0,0,0,1,0,0,0,1},
  123.                                                                 {1,0,0,0,0,0,0,1,0,1,1,1},
  124.                                                                 {1,1,1,1,1,1,1,1,1,1,1,1},
  125.                                                                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  126.                                                                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  127.                                                                 {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
  128.                                                         };



  129.                                                        
  130. //迷宫初始化
  131. void maze_init(void)
  132. {
  133.         //int x1,y1;
  134.                 GrContextForegroundSet(&sContext, ClrBlue);                                       
  135.                 for(i=0;i<13;i++)
  136.                         for(j=0;j<12;j++)
  137.                 {
  138.                         if(maze[i][j]==1)
  139.                         {
  140.                                 pRect.sXMin = i*20;
  141.                                 pRect.sYMin = j*20;
  142.                                 pRect.sXMax = i*20+20;
  143.                                 pRect.sYMax = j*20+20;
  144.                                
  145.                                 GrRectFill(&sContext, &pRect);
  146.                                         // 确保图形被绘制在屏幕上
  147.                                 GrFlush(&sContext);
  148.                         }
  149.                 }
  150.                
  151. }


  152. //豆子初始化
  153. void pac_init(void)
  154. {
  155.                 // 设置画笔为黄色
  156.                 GrContextForegroundSet(&sContext, ClrYellow);                                       
  157.                 for(i=0;i<13;i++)
  158.                         for(j=0;j<12;j++)
  159.                 {
  160.                         if(maze[i][j]==0)
  161.                         {
  162.                                 pac_x=i*20+10;
  163.                                 pac_y=j*20+10;
  164.                                 GrCircleFill(&sContext, pac_x, pac_y,3);
  165.                                         // 确保图形被绘制在屏幕上
  166.                                 GrFlush(&sContext);
  167.                         }
  168.                 }
  169.                
  170. }


  171. //虫子初始化
  172. void warm(float warm_x,float warm_y)
  173. {
  174.                 GrContextForegroundSet(&sContext, ClrPink);
  175.                 GrCircleFill(&sContext, warm_x, warm_y,6);
  176.        
  177.                 pRect.sXMin = warm_x;
  178.                 pRect.sYMin = warm_y-3;
  179.                 pRect.sXMax = warm_x+4;
  180.                 pRect.sYMax = warm_y+3;       
  181.                 GrContextForegroundSet(&sContext, ClrPink);
  182.                 GrRectFill(&sContext, &pRect);
  183.         // 确保图形被绘制在屏幕上
  184.                 GrFlush(&sContext);

  185. }

  186. //虫子运动
  187. void warm_move(void)
  188. {
  189.         move_x=(warm_x-10)/20;
  190.         move_y=(warm_y-10)/20;
  191.        
  192.         while(maze[move_x][move_y]!=1)
  193.         {
  194.                 GrContextForegroundSet(&sContext, ClrRed);
  195.                         GrCircleFill(&sContext,50, 50,9);
  196.                         GrContextForegroundSet(&sContext, ClrBlack);
  197.                         GrCircleFill(&sContext,warm_x, warm_y,9);
  198.                         GrContextForegroundSet(&sContext, ClrYellow);
  199.                         GrCircleFill(&sContext,warm_x, warm_y,3);
  200.                         warm_x=warm_x+20;
  201.                         warm_y=warm_y;
  202.                         warm(warm_x,warm_y);
  203.                         move_x=(warm_x-10)/20;
  204.                         move_y=(warm_y-10)/20;
  205.                         delayms(200);
  206.         }

  207. }

  208. void pac_man_down1(float man_x,float man_y)
  209. {
  210.                 // 画一个实心圆
  211.                         GrContextForegroundSet(&sContext, ClrYellow);
  212.                         GrCircleFill(&sContext, man_x, man_y,9);
  213.                 //画吃豆人的嘴
  214.                        
  215.                         for(mdu=-45;mdu<45;mdu++)
  216.                         {
  217.                                 //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));       
  218.                                 mouth_x=man_x+9;
  219.                                 mouth_y=man_y+9*sin(mdu*3.1415/180);
  220.                                 GrContextForegroundSet(&sContext,ClrBlack);
  221.                                 GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
  222.                         }
  223.                         //GrLineDraw(&sContext,80,80,90,70);       
  224.                         // 确保图形被绘制在屏幕上
  225.                         GrFlush(&sContext);

  226. }
  227. void pac_man_left2(float man_x,float man_y)
  228. {
  229.                 // 画一个实心圆
  230.                         GrContextForegroundSet(&sContext, ClrYellow);
  231.                         GrCircleFill(&sContext, man_x, man_y,9);
  232.                 //画吃豆人的嘴
  233.                        
  234.                         for(mdu=-45;mdu<45;mdu++)
  235.                         {
  236.                                 //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));       
  237.                                 mouth_x=man_x+9*sin(mdu*3.1415/180);
  238.                         mouth_y=man_y+9;
  239.                                 GrContextForegroundSet(&sContext,ClrBlack);
  240.                                 GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
  241.                         }
  242.                         //GrLineDraw(&sContext,80,80,90,70);       
  243.                         // 确保图形被绘制在屏幕上
  244.                         GrFlush(&sContext);

  245. }
  246. void pac_man_up3(float man_x,float man_y)
  247. {
  248.                 // 画一个实心圆
  249.                         GrContextForegroundSet(&sContext, ClrYellow);
  250.                         GrCircleFill(&sContext, man_x, man_y,9);
  251.                 //画吃豆人的嘴
  252.                        
  253.                         for(mdu=-45;mdu<45;mdu++)
  254.                         {
  255.                                 //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));       
  256.                                 mouth_x=man_x-9;
  257.                         mouth_y=man_y+9*sin(mdu*3.1415/180);
  258.                                 GrContextForegroundSet(&sContext,ClrBlack);
  259.                                 GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
  260.                         }
  261.                         //GrLineDraw(&sContext,80,80,90,70);       
  262.                         // 确保图形被绘制在屏幕上
  263.                         GrFlush(&sContext);

  264. }
  265. void pac_man_right4(float man_x,float man_y)
  266. {
  267.                 // 画一个实心圆
  268.                         GrContextForegroundSet(&sContext, ClrYellow);
  269.                         GrCircleFill(&sContext, man_x, man_y,9);
  270.                 //画吃豆人的嘴
  271.                        
  272.                         for(mdu=-45;mdu<45;mdu++)
  273.                         {
  274.                                 //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));       
  275.                                 mouth_x=man_x+9*sin(mdu*3.1415/180);
  276.                         mouth_y=man_y-9;
  277.                                 GrContextForegroundSet(&sContext,ClrBlack);
  278.                                 GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
  279.                         }
  280.                         //GrLineDraw(&sContext,80,80,90,70);       
  281.                         // 确保图形被绘制在屏幕上
  282.                         GrFlush(&sContext);

  283. }



  284. //改方向为下
  285. void On_downPress(tWidget *pWidget)
  286. {

  287.         g_press1=true;
  288.         move_x=(man_x+10)/20;
  289.         move_y=(man_y-10)/20;
  290.         while(maze[move_x][move_y]!=1)
  291.         {
  292.                         if(maze[move_x][move_y]==0)
  293.                         {
  294.                                         maze[move_x][move_y]=2;
  295.                                         score++;
  296.                                        
  297.                         }
  298.                         GrContextForegroundSet(&sContext, ClrBlack);
  299.                         GrCircleFill(&sContext, man_x, man_y,9);
  300.                         man_x=man_x+20;
  301.                         man_y=man_y;
  302.                         pac_man_down1(man_x,man_y);
  303.                         move_x=(man_x+10)/20;
  304.                         move_y=(man_y-10)/20;
  305.                         delayms(200);       
  306.         }
  307. }

  308. //改方向为下
  309. void On_leftPress(tWidget *pWidget)
  310. {
  311.         g_press2=true;
  312.         move_x=(man_x-10)/20;
  313.         move_y=(man_y+10)/20;
  314.         while(maze[move_x][move_y]!=1)
  315.         {
  316.                 if(maze[move_x][move_y]==0)
  317.                         {
  318.                                         maze[move_x][move_y]=2;
  319.                                         score++;
  320.                                        
  321.                         }
  322.                         GrContextForegroundSet(&sContext, ClrBlack);
  323.                         GrCircleFill(&sContext, man_x, man_y,9);
  324.                         man_x=man_x;
  325.                         man_y=man_y+20;
  326.                         pac_man_left2(man_x,man_y);
  327.                         move_x=(man_x-10)/20;
  328.                         move_y=(man_y+10)/20;
  329.                         delayms(200);
  330.         }

  331. }
  332. //改方向为下
  333. void On_upPress(tWidget *pWidget)
  334. {
  335.         g_press3=true;
  336.         move_x=(man_x-30)/20;
  337.         move_y=(man_y-10)/20;
  338.         while(maze[move_x][move_y]!=1)
  339.         {
  340.                 if(maze[move_x][move_y]==0)
  341.                         {
  342.                                         maze[move_x][move_y]=2;
  343.                                         score++;
  344.                                        
  345.                         }
  346.                         GrContextForegroundSet(&sContext, ClrBlack);
  347.                         GrCircleFill(&sContext, man_x, man_y,9);
  348.                         man_x=man_x-20;
  349.                         man_y=man_y;
  350.                
  351.                         pac_man_up3(man_x,man_y);
  352.                         move_x=(man_x-30)/20;
  353.                         move_y=(man_y-10)/20;
  354.                
  355.                         delayms(200);
  356.                
  357.         }       
  358. }
  359. //改方向为下
  360. void On_rightPress(tWidget *pWidget)
  361. {
  362.         g_press4=true;
  363.         move_x=(man_x-10)/20;
  364.         move_y=(man_y-30)/20;
  365.         while(maze[move_x][move_y]!=1)
  366.         {
  367.                 if(maze[move_x][move_y]==0)
  368.                         {
  369.                                         maze[move_x][move_y]=2;
  370.                                         score++;
  371.                                        
  372.                         }
  373.                 GrContextForegroundSet(&sContext, ClrBlack);
  374.                 GrCircleFill(&sContext, man_x, man_y,9);
  375.                 man_x=man_x;
  376.                 man_y=man_y-20;
  377.                 pac_man_right4(man_x,man_y);
  378.                 move_x=(man_x-10)/20;
  379.                 move_y=(man_y-30)/20;
  380.                 delayms(200);
  381.         }
  382.                
  383. }

  384. int main(void)
  385. {
  386.    //设置时钟
  387.     SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
  388.                        SYSCTL_XTAL_16MHZ);
  389.    
  390.                 //初始化显示驱动
  391.     PinoutSet();
  392.     Kitronix320x240x16_SSD2119Init();
  393.         //初始化StellarisWare图形库上下文
  394.     GrContextInit(&sContext, &g_sKitronix320x240x16_SSD2119);
  395.         TouchScreenInit(); // 初始化触摸驱动程序。
  396.   TouchScreenCallbackSet(WidgetPointerMessage);// 设置触摸驱动凼数的回调凼数为图形库的处理凼数。
  397.        
  398.                   
  399.                 IntMasterEnable( );
  400.          // 将g_sBackground以下的控件加入控件树。
  401.   WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sDown);
  402.   // 绘制控件树中的所有控件。
  403.   WidgetPaint(WIDGET_ROOT);       
  404.                 //界面初始化
  405.                 maze_init();
  406.                 pac_init();
  407.         //吃豆人初始化
  408.                 score=0;
  409.                 man_x=30;
  410.                 man_y=130;
  411.                 pac_man_down1(30,130);
  412.                 warm(30,30);
  413.                 warm_move();
  414.                
  415.         while(1)
  416.         {
  417.                
  418.           // 处理所有控件事件。
  419.     WidgetMessageQueueProcess();
  420.         }

  421.        

  422. }

复制代码

所有资料51hei提供下载:
触摸屏版豆人.rar (3.24 MB, 下载次数: 9)



回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表