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

10 totalPointを表示する

BreakOut.h
#ifndef BREAKOUT_H
#define BREAKOUT_H

#include "Glcd.h"
#include "pushButton.h"
#define BlockY 3
#define BlockX 6

class BreakOut : public Glcd, public pushButton
{
  public:
    BreakOut();
    void drawBar(int diff);
    void drawBall();
    void gameOver();
  protected:
  private:
    int bar_x, bar_y;
    int ball_x, ball_y;
    int Xdirection, Ydirection;
    unsigned char bar[18];
    unsigned char clbar[18];
    char buffer[6];
    int n;
    int totalPoint;
    static unsigned char block[];
    static unsigned char wall[];
    static unsigned char ball[];
    static unsigned char BLOCKS[BlockY][BlockX];
    void initGame();
    void gameClear();
};

#endif // BREAKOUT_H
 1 
 2 
 3 
 4 
 5 
 6 ブロックのyの大きさ
 7 ブロックのxの大きさ
 8 
 9 
10 
11 
12 
13 ボールを跳ね返すバーを描く
14 ボールを描く
15 gameOverを表示する
16 
17 
18 バーのx, y座標
19 ボールのx, y座標
20 ボールの移動方向
21 バーのグラフィックデータ
22 バーを消すグラフィックデータ
23 ポイント数を文字にするためのバッファー
24 バーのデータ数
25 ゲームの総得点
26 ブロックのグラフィックデータ
27 壁のグラフィックデータ
28 ボールのグラフィックデータ
29 ブロックの存在データ
30 初期化をメソッドに独立化
31 ゲームクリアをした時の画面を表示
32 
33 
34 


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};
unsigned char BreakOut::BLOCKS[BlockY][BlockX];

BreakOut::BreakOut()
{
  //ctor
  n = 18;
  for(int i=0; i<n; i++) {
    bar[i] = 0x0f;
    clbar[i] = 0;
  }
  initGame();
  totalPoint=0;
}

void BreakOut::initGame()
{
  bar_x = 42;
  bar_y = 7;

  for(int i=0; i<8; i++) {
    draw(96, i, wall, 4);
  }
  for(int y=0; y<BlockY; y++)
    for(int x=0; x<96; x+=16)
      draw(x, y, block, 16);
  for(int y=0; y<BlockY; y++) {
    for(int x=0; x<BlockX; x++) BLOCKS[y][x]=1;
  }
  ball_x=rand()%88;
  ball_y=6;
  Xdirection=Ydirection=0;
  drawBar(0);
  print(100, 2, "point");
}
  // バー描画
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;
    }
  }
  if(ball_y<=7 && Ydirection==0){ // up
    --ball_y;
    int x = (ball_x+4)/16;  // 真ん中
    if(ball_y<0) {
      ball_y=0;
      Ydirection=1; // down
    }
    else if(ball_y<3) {
      if(BLOCKS[ball_y][x]) {
        BLOCKS[ball_y][x] =0;
        draw((ball_x+4)/16*16, ball_y, clbar, 16);
        Ydirection=1; // down
      }
    }
  }
  else if(ball_y>=0 && Ydirection==1){  //  down
    if(++ball_y>=7) {
      Ydirection=0;  // up
      ball_y=7;
      gameOver();

    }
    if(ball_y==6){
      if(bar_x<=ball_x+8 && ball_x<bar_x+n){
        Ydirection=0;  // up
      }
    }
    if(ball_y<3) {
      int x = (ball_x+4)/16;  // 真ん中
      if(BLOCKS[ball_y][x]) {
        BLOCKS[ball_y][x] =0;
        draw((ball_x+4)/16*16, ball_y, clbar, 16);
        Ydirection=1; // down
      }
    }
  }
  draw(ball_x, ball_y, ball, 8 );
  int point=0;
  for(int y=0; y<BlockY; y++) {
    for(int x=0; x<BlockX; x++) if(BLOCKS[y][x]) point++;
  }
  if(point==0) gameClear();
  else {
    sprintf(buffer, "%d",BlockX*BlockY-point+totalPoint );
    print(100, 3, buffer);
  }
}

void BreakOut::gameOver()
{
  clearAll();
  print(40, 3, "Game Over");
  print(40, 4, "point:");
  print(75, 4, buffer);
  while(getKey()!=1);
  clearAll();
  initGame();
  totalPoint=0;
}

void BreakOut::gameClear()
{
  clearAll();
  print(24, 2, "***************");
  print(24, 3, "congratulations");
  print(45, 4, "All Clear");
  totalPoint +=BlockX*BlockY;
  sprintf(buffer, "%d", totalPoint);
  print(24, 5, "Total point:");
  print(89, 5, buffer);
  print(24, 6, "***************");
  while(getKey()!=1);
  clearAll();
  initGame();
}
 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 point表示
44 
45 
46 バーの移動(上下左右)
47 
48 diffドット移動させる
49 
50 
51 
52 
53 
54 
55 ボールを描画する
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 ブロックが消えたことを記録する
82 ブロックを消す
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100
101
102
103 
104 
105 
106 
107 
108 ボールを描く
109 
110 
111 ポイントの計算
112 
113 ゲームクリアしていたらオメデトウ
114 そうでなかったら
115 残念でした
116 取得得点の表示
117 
118 
119 
120 ゲームオーバーを表示する
121 
122 
123 
124 
125 
126 ESCを押すと再ゲーム
127 
128 
129 一番初めから頑張る
130 
131 
132 ゲームクリアしていた場合
133 
134 
135 
136 
137 
138 
139 総得点の表示
140 
141 
142 
143 ESCを押すと再ゲーム
144 
145 
146 

メインプログラム



main.cpp
#include "mbed.h"
#include "BreakOut.h"
// pushButton classの導入
// ボールを表示
// ボールをx方向に動かす
// ボールをy方向に動かす
// バーで跳ね返す
// ブロックを壊す
// 下に落ちたらgameOverにする
// ESC でgameOverにする
// 点数の表示
// congratulationsを表示する
// totalPointを表示する

int main() {
  BreakOut game;
  game.drawBar(0);
  while(true){
    switch(game.getKey())
    {
      case 1: game.gameOver();
              break;
      case 2: game.drawBar(-6);  // left
              break;
      case 4: game.drawBar(6);   // right
              break;
    }
    game.drawBall();
    wait_ms(100);
  }
}
 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
10 
11 
12 
13 
14 
15 
16 ゲームらしくオブジェクト名をgameにする
17 バーを描く
18 
19 キーコードを読み込む
20 
21 
22 
23 バーを左に6ドット移動させる
24 
25 バーを右に6ドット移動させる
26 
27 
28 ボールを描く
29 
30 
31