|
基于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"};
- // 聲明驅(qū)動庫中定義的tDisplay顯示設(shè)備對象。
- 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;
- // 提前聲明按鈕按下時的處理凼數(shù)。
- 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)
- {
- // 設(shè)置畫筆為黃色
- 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);
- }
- //蟲子運(yùn)動
- 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)
- {
- // 畫一個實(shí)心圓
- 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)
- {
- // 畫一個實(shí)心圓
- 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)
- {
- // 畫一個實(shí)心圓
- 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)
- {
- // 畫一個實(shí)心圓
- 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)
- {
- //設(shè)置時鐘
- SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
- SYSCTL_XTAL_16MHZ);
-
- //初始化顯示驅(qū)動
- PinoutSet();
- Kitronix320x240x16_SSD2119Init();
- //初始化StellarisWare圖形庫上下文
- GrContextInit(&sContext, &g_sKitronix320x240x16_SSD2119);
- TouchScreenInit(); // 初始化觸摸驅(qū)動程序。
- TouchScreenCallbackSet(WidgetPointerMessage);// 設(shè)置觸摸驅(qū)動凼數(shù)的回調(diào)凼數(shù)為圖形庫的處理凼數(shù)。
-
-
- 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();
- }
-
-
- }
復(fù)制代碼
所有資料51hei提供下載:
觸摸屏版豆人.rar
(3.24 MB, 下載次數(shù): 9)
2018-4-6 03:40 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|