2019.01.17 Thursday
Arduboy 自由落下
今日気がついたんだけど、Arduboyって左がAボタンなのな。
ファミコンと逆で混乱したわ。
今日はキャラクターを自由落下させてみた。
さらに、ボタンで落下に抗うことが出来る。
#include "Arduboy.h"
// make an instance of arduboy used for many functions
Arduboy arduboy;
PROGMEM const unsigned char player[] = {
0x00, 0x00, 0x00, 0xc0, 0x20, 0x10, 0xc8, 0x08,
0x08, 0xc8, 0x10, 0x20, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0x04, 0x08, 0x14, 0x14,
0x14, 0x14, 0x08, 0x04, 0x03, 0x00, 0x00, 0x00,
};
// This function runs once in your game.
// use it for anything that needs to be set only once in your game.
void setup() {
// initiate arduboy instance
arduboy.begin();
}
double g = 1;
double dt = 0.1;
void loop() {
// pause render until it's time for the next frame
if (!(arduboy.nextFrame()))
return;
// first we clear our screen to black
arduboy.clear();
//ここにロジック書く////////////////////////////
static double y = 10;//, y = 10;
static double v = 0;
static double t = 0;
t+=dt;
if( arduboy.pressed(A_BUTTON) == true )
{
v = 5;
t = 0;
}
y = g * t * dt - v*dt + y;
arduboy.drawBitmap(16,y,player,16,16,1);
// then we finaly we tell the arduboy to display what we just wrote to the display
arduboy.display();
}
loop関数が呼ばれるごとに時刻tを進めてるけど、位置は前回更新時との差分を足しこむ方が都合がよいので、高校物理で習う自由落下の式x = ...ではなく
dx = gtdt+vdt
を使っている。