■gb.collidePointRect関数
Gamebuimo.hライブラリのgb.collidePointRect関数は、指定された点が矩形内にあるかどうかをチェックします。
■使用例
gb.collidePointRect関数を使ったArduinoIDEのプログラム例は図1の通りです。
このプログラムを実行するとsetup関数中のgb.begin関数でGamebuinoオブジェクトを初期化し、gb.titleScreen関数で画面に「collidePointRect」と表示します。
スタート画面でキーボードの「K」(GamebuinoのA)ボタンを押すと画面左上に「Move with arrows」と表示されポイントをカーソルキーで動かすことが可能になります。
黒枠内にポイントが移動したとgb.collidePointRect関数で判定されると「Colliding」と画面に表示されます。
参考に図1プログラムをHEXファイルに変換し、それをSimbuino4Webでエミュレートした結果を図2に示します。
#include <SPI.h> #include <Gamebuino.h> Gamebuino gb; byte player_x = 20; byte player_y = 20; byte oponent_x = LCDWIDTH / 2; byte oponent_y = LCDHEIGHT / 2; byte oponent_w = 17; byte oponent_h = 12; void setup() { gb.begin(); gb.titleScreen(F("collidePointRect")); } void loop() { if (gb.update()) { gb.display.println(F("\n Move with arrows")); if (gb.buttons.pressed(BTN_C)) { gb.titleScreen(F("collidePointRect")); } //update player if (gb.buttons.repeat(BTN_RIGHT, 1)) { player_x = player_x + 1; } if (gb.buttons.repeat(BTN_LEFT, 1)) { player_x = player_x - 1; } if (gb.buttons.repeat(BTN_DOWN, 1)) { player_y = player_y + 1; } if (gb.buttons.repeat(BTN_UP, 1)) { player_y = player_y - 1; } //update collisions if (gb.collidePointRect(player_x, player_y, oponent_x, oponent_y, oponent_w, oponent_h)) { gb.sound.playTick(); gb.display.print(F(" Colliding")); } //draw oponent gb.display.fillRect(oponent_x, oponent_y, oponent_w, oponent_h); //draw player gb.display.setColor(INVERT); gb.display.drawPixel(player_x, player_y); gb.display.setColor(BLACK); //draw a few lines to help seeing the pixel: gb.display.drawLine(0, player_y, 3, player_y); gb.display.drawLine(player_x, 0, player_x, 3); gb.display.drawLine(player_x, LCDHEIGHT - 3, player_x, LCDHEIGHT); gb.display.drawLine(LCDWIDTH - 3, player_y, LCDWIDTH, player_y); } }
図1:プログラム例
図2:プログラム実行結果
■構文
gb.collidePointRect(x1、y1、x2、y2、w、h)
■パラメータ
x1 :点の水平座標。
y1 :点の垂直座標です。
x2 :矩形の左上隅の水平座標。
y2 :矩形の左上隅の垂直座標。
w :長方形の幅。
h :矩形の高さ
■戻り値
true(点が矩形の内側にある場合)、false(点が矩形の内側にない場合)
励みになりますのでよければクリック下さい(^o^)/