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

06 ボールを動かす

ボールを左右の壁の間で動かします。

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);
    void drawBall();
  protected:
  private:
    int bar_x, bar_y;
    int ball_x, ball_y;
    int Xdirection, Ydirection;
    unsigned char bar[18];
    unsigned char clbar[18];
    int n;
    static unsigned char block[];
    static unsigned char wall[];
    static unsigned char ball[];
};

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


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
};
unsigned char BreakOut::ball[]=
{0x3c, 0x7e, 0xff, 0xff, 0xff, 0xff,0x7e,0x3c};

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);
  ball_x=0;
  ball_y=6;
  Xdirection=Ydirection=0;
}
  // バー描画
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 );
}

void BreakOut::drawBall()
{
  draw(ball_x, ball_y, clbar, 8 );
  if(ball_x<=88 && Xdirection==0) {
    ball_x += 4;
    if(ball_x>88) {
      Xdirection=1; // right
      ball_x=82;
    }
  }
  if(ball_x>0 && Xdirection==1){
    ball_x -= 4;
    if(ball_x<=0) {
      Xdirection=0; //  left
      ball_x=0;
    }
  }
  draw(ball_x, ball_y, ball, 8 );
}
 1 
 2 ブロックのグラフィックデータ
 3 
 4 
 5 壁のグラフィックデータ
 6 
 7 
 8 
 9 ボールのグラフィックデータ
10 
11 
12 コンストラクタ
13 
14 
15 バーの初期位置
16 
17 
18 バーのグラフィックデータ作成
19 
20 
21 
22 
23 壁を描く
24 
25 
26 
27 ブロックを描く
28 ボールの初期位置
29 
30 ボールの初期移動方向(右、上)
31 
32 
33 バーを描く
34 
35 
36 
37 
38 
39 
40 
41 
42 バーの移動(左右のみ)
43 
44 
45 
46 4ドット移動させる
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 

メインプログラム



main.cpp
#include "mbed.h"
#include "BreakOut.h"
// pushButton classの導入
// ボールを表示
// ボールをx方向に動かす
// ボールをy方向に動かす

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.drawBar(-6);  // left
              break;
      case 4: LCD.drawBar(6);   // right
              break;
      default: LCD.drawChar(100, 1, k+0x30); break;
    }
    LCD.drawBall();
    wait_ms(10);
  }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 バーを描く
11 
12 キーコードを読み込む
13 
14 
15 
16 
17 
18 バーを左に6ドット移動させる
19 
20 バーを右に6ドット移動させる
21 
22 
23 
24 ボールを描く
25 
26 
27