グラフィックLCDを使ってみよう

05 バーを動かす



BreakOut.h
#ifndef BREAKOUT_H
#define BREAKOUT_H

#include "Glcd.h"
#include "pushButton.h"

class BreakOut : public Glcd, public pushButton
{
  public:
    BreakOut();
    void drawBar(int diff);
  protected:
  private:
    int bar_x;
    int bar_y;
    unsigned char bar[18];
    unsigned char clbar[18];
    int n;
    static unsigned char block[];
    static unsigned char wall[];
};

#endif // BREAKOUT_H
 1 
 2 
 3 
 4 
 5 
 6 
 7 pushButtonクラスから派生させる
 8 
 9 
10 
11 ボールを跳ね返すバーを描く(引数が1つ)
12 
13 
14 バーのx座標
15 バーのy座標
16 バーのグラフィックデータ
17 バーを消すグラフィックデータ
18 バーのデータ数
19 ブロックのグラフィックデータ
20 壁のグラフィックデータ
21 
22 
23 


BreakOut.cpp
#include "BreakOut.h"
unsigned char BreakOut::block[]=
{0x00, 0x7e, 0x56, 0x6a, 0x56, 0x6a, 0x56, 0x6a,
 0x56, 0x6a, 0x56, 0x6a, 0x56, 0x6a, 0x7e,0x00};
unsigned char BreakOut::wall[]=
{
  0xff, 0xff, 0xff, 0xff
};

BreakOut::BreakOut()
{
  //ctor
  bar_x = 42;
  bar_y = 7;
  n=18;
  for(int i=0; i<n; i++) {
    bar[i]=0x0f;
    clbar[i]=0;
  }
  for(int i=0; i<8; i++) {
    draw(96, i, wall, 4);
  }
  for(int y=0; y<3; y++)
    for(int x=0; x<96; x+=16)
      draw(x, y, block, 16);
}
  // バー描画
void BreakOut::drawBar(int diff)
{
  int x = bar_x+diff;
  if(x<0 || 80<=x) return;
   draw(bar_x, bar_y, clbar, n );
   bar_x += diff;
   draw(bar_x, bar_y, bar, n );
}
 1 
 2 ブロックのグラフィックデータ
 3 
 4 
 5 壁のグラフィックデータ
 6 
 7 
 8 
 9 
10 コンストラクタ
11 
12 
13 pushButtonクラスに移動させる
14 バーの初期位置
15 
16 
17 バーのグラフィックデータ作成
18 
19 壁を描く
20 
21 
22 
23 ブロックを描く
24 
25 pushButtonクラスに移動させる
26 
27 
28 
29 
30 
31 
32 
33 バーを描く
34 
35 
36 

メインプログラム



main.cpp
#include "mbed.h"
#include "BreakOut.h"
// pushButton classの導入

int main() {
  BreakOut LCD;
  LCD.drawBar(0);
  while(true){
    switch(int k= LCD.getKey())
    {
      case 1: LCD.print(100, 0, "ESC  ");
              wait_ms(200);
              LCD.clear(100,0,3*5);
              break;
      case 2: LCD.print(100, 0, "left ");
              wait_ms(20);
              LCD.clear(100,0,4*5);
              LCD.drawBar(-6);
              break;
      case 4: LCD.print(100, 0, "right");
              wait_ms(20);
              LCD.clear(100,0,5*5);
              LCD.drawBar(6);
              break;
      default:
              k +=0x30;
              LCD.drawChar(100, 1, k); break;
    }
    wait_ms(10);
  }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 バーを描く
 8 
 9 キーコードを読み込む
10 
11 
12 
13 
14 
15 
16 
17 
18 バーを左に6ドット移動させる
19 
20 
21 
22 
23 バーを右に6ドット移動させる
24 
25 
26 
27