I-O DATA DEV2 / Mbed 2 deprecated game01

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 DigitalOut led(LED1);
00004 //InterruptIn event(USER_BUTTON);
00005 DigitalIn button(USER_BUTTON);
00006 RawSerial pc(PA_2, PA_3,115200 );
00007 
00008 DigitalIn l_button(D4,PullUp); 
00009 DigitalIn r_button(D3,PullUp); 
00010 
00011 InterruptIn event(USER_BUTTON);
00012 Ticker      interrput;
00013 
00014 
00015 char bdata[20][20]={
00016  2,2,2,2,2 ,2,2,2,2,2, 2,2,2,2,2 ,2,2,2,2,2,  
00017  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00018  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00019  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00020  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00021  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00022  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00023  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00024  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00025  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00026  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00027  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00028  2,0,0,0,0 ,0,1,0,0,1, 1,0,1,0,0 ,0,0,0,0,2, 
00029  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00030  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00031  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00032  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00033  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00034  2,0,0,0,0 ,0,0,0,0,0, 0,0,0,0,0 ,0,0,0,0,2, 
00035  2,2,2,2,2 ,2,2,2,2,2, 2,2,2,2,2 ,2,2,2,2,2,  
00036 };
00037   
00038 
00039 
00040 void cur_down()
00041 {
00042         pc.printf("\033B");
00043 }
00044 
00045 void cur_up()
00046 {
00047         pc.printf("\033A");
00048 }
00049 
00050 
00051 
00052 void cur_left()
00053 {
00054         pc.printf("\033D");
00055 }
00056 
00057 void cur_right()
00058 {
00059         pc.printf("\033C");
00060 }
00061 
00062 void cur_xy(int x,int y)
00063 {
00064     pc.printf("\033[%d;%dH",y,x);
00065 }
00066 
00067 void clear_all()
00068 {
00069     pc.printf("\033[2J\033[6m");//clear , under line
00070     cur_xy(0,0);
00071 }
00072 
00073 void init_bd()
00074 {
00075     int x;
00076     int  y;
00077     pc.printf("\033[2J");//clear 
00078     for(y=0;y<20;y++) {
00079         cur_xy(1,y+1);
00080         for(x=0;x<20;x++) {
00081             if (bdata[y][x]==2) pc.printf("H"); 
00082             else if (bdata[y][x]==1) pc.printf("@"); 
00083             else pc.printf(" ");
00084         }
00085     }
00086 }
00087 
00088 int    dx=1;//横移動
00089 int    dy=1;//縦移動
00090 
00091 /* GPIO 割り込み */
00092 void pressed()
00093 {
00094     //pc.printf("Button pressed\n");
00095     dx = -dx;//横 動きを逆に
00096 }
00097 
00098 /* タイマー割り込み */
00099 void intTimer()
00100 {
00101     led = !led;
00102 }
00103 
00104 int main()
00105 {
00106     int    i;
00107     int    x;
00108     int    y;
00109     clear_all();                       //画面クリア   
00110     event.fall(&pressed);              //ボタン押した時、 立下りエッジ割り込みの関数設定
00111     interrput.attach(&intTimer, 0.5f); //0.5秒タイマーで割り込みの関数設定
00112     
00113     pc.printf("\nStart example\n");
00114     init_bd();//ブロックデータの初期化
00115     x=2;//横 初期位置
00116     y=0;//縦 初期位置
00117     i=0;
00118     while(1) {
00119         //範囲越え判定  
00120         if (y>=17) dy=-1;
00121         if (x>=19) dx=-1;
00122         if (y<=0) dy=1;
00123         if (x<=0) dx=1;
00124         //移動計算
00125         x=x+dx;
00126         y=y+dy;
00127         cur_xy(x+1,y+1);     //カーソル座標設定
00128         if (bdata[y][x]==1) { //当たり判定
00129              bdata[y][x]=0; //ブロックをなくす
00130              pc.printf(" "); //@を消す
00131              cur_xy(x+1,y+1);//カーソルを戻す
00132              pc.printf("\007");//ベル音
00133         }
00134         
00135         wait(0.1f);
00136         //ボタン操作
00137 //        if (button==0) dx = -dx;
00138 //       dx=0;
00139 //       if (l_button==0) dx= -1;
00140 //       else if (r_button==0) dx=  1;
00141 
00142          //パドル表示
00143           if (x<=1){
00144                cur_xy(1,19);  //カーソル座標設定
00145                pc.printf("HXX ");
00146           } else if (x>=17) { 
00147               cur_xy(17,19);  //カーソル座標設定
00148               pc.printf(" XXH");
00149           } else {
00150                cur_xy(x,19);//カーソル
00151                pc.printf(" XX ");
00152           }
00153           cur_xy(x+1,y+1);//カーソルを戻す
00154         i++;
00155         if (i==100) __disable_irq(); // 禁止
00156         else if(i==200) __enable_irq(); // 許可
00157           
00158     }  
00159     
00160 }
00161 
00162