基于Cortex_M3的触摸屏版吃豆人
mcu源程序如下:
- #include "inc/lm3s9b96.h"
- #include "inc/hw_types.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/timer.h"
- #include "inc/hw_memmap.h"
- #include "inc/hw_ints.h"
- #include "driverlib/interrupt.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/uart.h"
- #include "math.h"
- #include "inc/hw_types.h"
- #include "inc/hw_timer.h"
- #include "inc/hw_gpio.h"
- #include "inc/hw_sysctl.h"
- #include "inc/hw_ints.h"
- #include "inc/hw_memmap.h"
- #include "inc/hw_nvic.h"
- #include "driverlib/interrupt.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/rom.h"
- #include "driverlib/debug.h"
- #include "driverlib/gpio.h"
- #include "grlib/grlib.h"
- #include "grlib/widget.h"
- #include "grlib/canvas.h"
- #include "grlib/pushbutton.h"
- #include "drivers/kitronix320x240x16_ssd2119_8bit.h"
- #include "drivers/touch.h"
- #include "drivers/set_pinout.h"
- #include "driverlib/systick.h"
- #include "driverlib/timer.h"
- #include "inc/hw_timer.h"
- tBoolean g_press1 = false;
- tBoolean g_press2 = false;
- tBoolean g_press3 = false;
- tBoolean g_press4 = false;
- char p[9]={"00:00:00"};
- // 声明驱动库中定义的tDisplay显示设备对象。
- extern const tDisplay g_sKitronix320x240x16_SSD2119;
- // 提前声明要使用到的控件。(后文为建立控件树可能会相互引用)
- //extern tCanvasWidget g_sBackground;
- //extern tCanvasWidget g_sGame;
- extern tPushButtonWidget g_sDown;
- extern tPushButtonWidget g_sLeft;
- extern tPushButtonWidget g_sUp;
- extern tPushButtonWidget g_sRight;
- // 提前声明按钮按下时的处理凼数。
- void On_downPress(tWidget *pWidget);
- void On_leftPress(tWidget *pWidget);
- void On_upPress(tWidget *pWidget);
- void On_rightPress(tWidget *pWidget);
- //作为选择下的方块按钮
- RectangularButton(g_sDown,WIDGET_ROOT,&g_sLeft,0,
- &g_sKitronix320x240x16_SSD2119, 270, 30, 40, 40,
- (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
- PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
- ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
- &g_sFontCmss22b, "R", 0, 0, 0, 0,
- On_downPress);
- //作为选择左的方块按钮
- RectangularButton(g_sLeft,WIDGET_ROOT,&g_sUp,0,
- &g_sKitronix320x240x16_SSD2119, 270, 80, 40, 40,
- (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
- PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
- ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
- &g_sFontCmss22b, "D", 0, 0, 0, 0,
- On_leftPress);
- //作为选择上的方块按钮
- RectangularButton(g_sUp, WIDGET_ROOT,&g_sRight,0,
- &g_sKitronix320x240x16_SSD2119, 270, 130, 40, 40,
- (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
- PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
- ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
- &g_sFontCmss22b, "L", 0, 0, 0, 0,
- On_upPress);
- //作为选择右的方块按钮
- RectangularButton(g_sRight,WIDGET_ROOT,0,0,
- &g_sKitronix320x240x16_SSD2119, 270, 180, 40, 40,
- (PB_STYLE_OUTLINE | PB_STYLE_TEXT_OPAQUE | PB_STYLE_TEXT |
- PB_STYLE_FILL | PB_STYLE_RELEASE_NOTIFY),
- ClrDarkBlue, ClrRed, ClrWhite, ClrBlue,
- &g_sFontCmss22b, "U", 0, 0, 0, 0,
- On_rightPress);
-
- #define KEY_PERIPH SYSCTL_PERIPH_GPIOJ
- #define KEY_PORT GPIO_PORTJ_BASE
- #define KEY_PIN GPIO_PIN_7
- //吃豆人定义变量
- float man_x,man_y;//定义吃豆人的圆心位置
- float man_r=9;//定义吃豆人的半径
- float mdu;//定义吃豆人嘴的角度
- float mouth_x,mouth_y;//定义吃豆人嘴的边
- float warm_x,warm_y;//定义虫子位置
- float pac_x,pac_y;//定义豆子位置
- int flag=-1;
- int score;
- int i,j;
- //声明绘图上下文
- tContext sContext;
- tRectangle pRect;
- int move_x,move_y;
- //定义方向:1--下;2--左;3--上;4--右
- void delayms(int k)
- {
- int i,j;
- for(i=k;i>0;i--)
- for(j=20000;j>0;j--) ;
- }
- //定义迷宫形状
- //1--墙 0--有豆子 2--曾有豆子,已被吃掉
- unsigned char maze[16][12]=
- {{1,1,1,1,1,1,1,1,1,1,1,1},
- {1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,1,0,0,0,0,0,0,0,0,1},
- {1,0,1,0,0,0,0,0,0,0,0,1},
- {1,0,1,1,1,1,0,1,1,1,0,1},
- {0,0,1,0,0,0,0,0,1,1,0,0},
- {1,0,1,0,1,0,0,0,0,0,0,1},
- {1,0,0,0,1,1,1,0,1,1,0,1},
- {1,0,0,0,1,1,1,0,1,1,0,1},
- {1,0,0,0,1,0,1,0,0,1,1,1},
- {1,0,0,0,0,0,0,1,0,0,0,1},
- {1,0,0,0,0,0,0,1,0,1,1,1},
- {1,1,1,1,1,1,1,1,1,1,1,1},
- {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
- {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
- {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},
- };
-
- //迷宫初始化
- void maze_init(void)
- {
- //int x1,y1;
- GrContextForegroundSet(&sContext, ClrBlue);
- for(i=0;i<13;i++)
- for(j=0;j<12;j++)
- {
- if(maze[i][j]==1)
- {
- pRect.sXMin = i*20;
- pRect.sYMin = j*20;
- pRect.sXMax = i*20+20;
- pRect.sYMax = j*20+20;
-
- GrRectFill(&sContext, &pRect);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- }
-
- }
- //豆子初始化
- void pac_init(void)
- {
- // 设置画笔为黄色
- GrContextForegroundSet(&sContext, ClrYellow);
- for(i=0;i<13;i++)
- for(j=0;j<12;j++)
- {
- if(maze[i][j]==0)
- {
- pac_x=i*20+10;
- pac_y=j*20+10;
- GrCircleFill(&sContext, pac_x, pac_y,3);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- }
-
- }
- //虫子初始化
- void warm(float warm_x,float warm_y)
- {
- GrContextForegroundSet(&sContext, ClrPink);
- GrCircleFill(&sContext, warm_x, warm_y,6);
-
- pRect.sXMin = warm_x;
- pRect.sYMin = warm_y-3;
- pRect.sXMax = warm_x+4;
- pRect.sYMax = warm_y+3;
- GrContextForegroundSet(&sContext, ClrPink);
- GrRectFill(&sContext, &pRect);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- //虫子运动
- void warm_move(void)
- {
- move_x=(warm_x-10)/20;
- move_y=(warm_y-10)/20;
-
- while(maze[move_x][move_y]!=1)
- {
- GrContextForegroundSet(&sContext, ClrRed);
- GrCircleFill(&sContext,50, 50,9);
- GrContextForegroundSet(&sContext, ClrBlack);
- GrCircleFill(&sContext,warm_x, warm_y,9);
- GrContextForegroundSet(&sContext, ClrYellow);
- GrCircleFill(&sContext,warm_x, warm_y,3);
- warm_x=warm_x+20;
- warm_y=warm_y;
- warm(warm_x,warm_y);
- move_x=(warm_x-10)/20;
- move_y=(warm_y-10)/20;
- delayms(200);
- }
- }
- void pac_man_down1(float man_x,float man_y)
- {
- // 画一个实心圆
- GrContextForegroundSet(&sContext, ClrYellow);
- GrCircleFill(&sContext, man_x, man_y,9);
- //画吃豆人的嘴
-
- for(mdu=-45;mdu<45;mdu++)
- {
- //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));
- mouth_x=man_x+9;
- mouth_y=man_y+9*sin(mdu*3.1415/180);
- GrContextForegroundSet(&sContext,ClrBlack);
- GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
- }
- //GrLineDraw(&sContext,80,80,90,70);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- void pac_man_left2(float man_x,float man_y)
- {
- // 画一个实心圆
- GrContextForegroundSet(&sContext, ClrYellow);
- GrCircleFill(&sContext, man_x, man_y,9);
- //画吃豆人的嘴
-
- for(mdu=-45;mdu<45;mdu++)
- {
- //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));
- mouth_x=man_x+9*sin(mdu*3.1415/180);
- mouth_y=man_y+9;
- GrContextForegroundSet(&sContext,ClrBlack);
- GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
- }
- //GrLineDraw(&sContext,80,80,90,70);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- void pac_man_up3(float man_x,float man_y)
- {
- // 画一个实心圆
- GrContextForegroundSet(&sContext, ClrYellow);
- GrCircleFill(&sContext, man_x, man_y,9);
- //画吃豆人的嘴
-
- for(mdu=-45;mdu<45;mdu++)
- {
- //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));
- mouth_x=man_x-9;
- mouth_y=man_y+9*sin(mdu*3.1415/180);
- GrContextForegroundSet(&sContext,ClrBlack);
- GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
- }
- //GrLineDraw(&sContext,80,80,90,70);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- void pac_man_right4(float man_x,float man_y)
- {
- // 画一个实心圆
- GrContextForegroundSet(&sContext, ClrYellow);
- GrCircleFill(&sContext, man_x, man_y,9);
- //画吃豆人的嘴
-
- for(mdu=-45;mdu<45;mdu++)
- {
- //GrLineDraw(&sContext,man_x,man_y,man_x+man_r,80+10*sin(mdu*3.1415/180));
- mouth_x=man_x+9*sin(mdu*3.1415/180);
- mouth_y=man_y-9;
- GrContextForegroundSet(&sContext,ClrBlack);
- GrLineDraw(&sContext,man_x,man_y,mouth_x,mouth_y);
- }
- //GrLineDraw(&sContext,80,80,90,70);
- // 确保图形被绘制在屏幕上
- GrFlush(&sContext);
- }
- //改方向为下
- void On_downPress(tWidget *pWidget)
- {
- g_press1=true;
- move_x=(man_x+10)/20;
- move_y=(man_y-10)/20;
- while(maze[move_x][move_y]!=1)
- {
- if(maze[move_x][move_y]==0)
- {
- maze[move_x][move_y]=2;
- score++;
-
- }
- GrContextForegroundSet(&sContext, ClrBlack);
- GrCircleFill(&sContext, man_x, man_y,9);
- man_x=man_x+20;
- man_y=man_y;
- pac_man_down1(man_x,man_y);
- move_x=(man_x+10)/20;
- move_y=(man_y-10)/20;
- delayms(200);
- }
- }
- //改方向为下
- void On_leftPress(tWidget *pWidget)
- {
- g_press2=true;
- move_x=(man_x-10)/20;
- move_y=(man_y+10)/20;
- while(maze[move_x][move_y]!=1)
- {
- if(maze[move_x][move_y]==0)
- {
- maze[move_x][move_y]=2;
- score++;
-
- }
- GrContextForegroundSet(&sContext, ClrBlack);
- GrCircleFill(&sContext, man_x, man_y,9);
- man_x=man_x;
- man_y=man_y+20;
- pac_man_left2(man_x,man_y);
- move_x=(man_x-10)/20;
- move_y=(man_y+10)/20;
- delayms(200);
- }
- }
- //改方向为下
- void On_upPress(tWidget *pWidget)
- {
- g_press3=true;
- move_x=(man_x-30)/20;
- move_y=(man_y-10)/20;
- while(maze[move_x][move_y]!=1)
- {
- if(maze[move_x][move_y]==0)
- {
- maze[move_x][move_y]=2;
- score++;
-
- }
- GrContextForegroundSet(&sContext, ClrBlack);
- GrCircleFill(&sContext, man_x, man_y,9);
- man_x=man_x-20;
- man_y=man_y;
-
- pac_man_up3(man_x,man_y);
- move_x=(man_x-30)/20;
- move_y=(man_y-10)/20;
-
- delayms(200);
-
- }
- }
- //改方向为下
- void On_rightPress(tWidget *pWidget)
- {
- g_press4=true;
- move_x=(man_x-10)/20;
- move_y=(man_y-30)/20;
- while(maze[move_x][move_y]!=1)
- {
- if(maze[move_x][move_y]==0)
- {
- maze[move_x][move_y]=2;
- score++;
-
- }
- GrContextForegroundSet(&sContext, ClrBlack);
- GrCircleFill(&sContext, man_x, man_y,9);
- man_x=man_x;
- man_y=man_y-20;
- pac_man_right4(man_x,man_y);
- move_x=(man_x-10)/20;
- move_y=(man_y-30)/20;
- delayms(200);
- }
-
- }
- int main(void)
- {
- //设置时钟
- SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
- SYSCTL_XTAL_16MHZ);
-
- //初始化显示驱动
- PinoutSet();
- Kitronix320x240x16_SSD2119Init();
- //初始化StellarisWare图形库上下文
- GrContextInit(&sContext, &g_sKitronix320x240x16_SSD2119);
- TouchScreenInit(); // 初始化触摸驱动程序。
- TouchScreenCallbackSet(WidgetPointerMessage);// 设置触摸驱动凼数的回调凼数为图形库的处理凼数。
-
-
- IntMasterEnable( );
- // 将g_sBackground以下的控件加入控件树。
- WidgetAdd(WIDGET_ROOT, (tWidget *)&g_sDown);
- // 绘制控件树中的所有控件。
- WidgetPaint(WIDGET_ROOT);
- //界面初始化
- maze_init();
- pac_init();
- //吃豆人初始化
- score=0;
- man_x=30;
- man_y=130;
- pac_man_down1(30,130);
- warm(30,30);
- warm_move();
-
- while(1)
- {
-
- // 处理所有控件事件。
- WidgetMessageQueueProcess();
- }
-
-
- }
复制代码
所有资料51hei提供下载:
触摸屏版豆人.rar
(3.24 MB, 下载次数: 9)
|