Arduboyの当たり判定

Arduboyには当たり判定用メソッドcollide()が用意されている。
点と矩形、矩形同士の当たり判定が行える。

f:id:raohu69:20180318200526p:plain

f:id:raohu69:20180318200517p:plain

#include <Arduboy2.h>
Arduboy2 arduboy;

void setup()
{
  arduboy.begin();
  arduboy.setFrameRate(60);
  arduboy.clear();
}

void loop()
{
  if (!arduboy.nextFrame()) return;
  arduboy.clear();

  static Rect player = { 0, 0, 20, 15 };
  if (arduboy.pressed(LEFT_BUTTON)) {
    player.x -= 1;
  } else if (arduboy.pressed(RIGHT_BUTTON)) {
    player.x += 1;
  }
  if (arduboy.pressed(UP_BUTTON)) {
    player.y -= 1;
  } else if (arduboy.pressed(DOWN_BUTTON)) {
    player.y += 1;
  }
  arduboy.drawRect(player.x, player.y, player.width, player.height, WHITE);

  // 点の表示
  Point point = { 32, 32 };
  arduboy.drawPixel(point.x, point.y, WHITE);

  // 点と矩形の当たり判定
  if (arduboy.collide(point, player)) {
    arduboy.print("Point HIT!");
  }

  // 矩形の表示
  Rect rect = { 64, 32, 10, 20 };
  arduboy.fillRect(rect.x, rect.y, rect.width, rect.height, WHITE);

  // 矩形と矩形の当たり判定
  if (arduboy.collide(rect, player)) {
    arduboy.print("Rect HIT!");
  }

  arduboy.display();
}