Here's my entire code. Ok I understand that maybe I should keep the time variables as unsigned longs, but why is it that I can't apply my deltaTime to my player character unless I keep everything floats? Is there something going wrong in the conversion?
#include <Arduboy.h>
Arduboy arduboy;
const unsigned char background[] PROGMEM = {0x81, 0x00, 0x12, 0x40, 0x4, 0x11, 0x00, 0x4,};
const unsigned char player[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x34, 0xfc, 0x8f, 0x34, 0x6, 0x36, 0x8e, 0x94, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2, 0x1e, 0xe6, 0xa3, 0xda, 0x83, 0xc4, 0xb8, 0x00, 0x00, 0x00, 0x00,};
bool aBuffer;
bool showBG;
float playerX;
float playerY;
float oldTime = 0;
void setup() {
arduboy.begin();
arduboy.clear();
ResetGame();
}
void loop() {
float deltaTime = CalculateDeltaTime();
arduboy.clear();
// Draw Background.
if (showBG) {
for (int i = 0; i < 128; i += 8) {
for (int j = 0; j < 64; j += 8) {
arduboy.drawBitmap(i, j, background, 8, 8, WHITE);
}
}
}
// Draw player character.
arduboy.fillRect(playerX + 4, playerY, 8, 16, BLACK);
arduboy.drawBitmap(playerX, playerY, player, 16, 16, WHITE);
if (arduboy.pressed(LEFT_BUTTON))
playerX -= deltaTime / 4;
if (arduboy.pressed(RIGHT_BUTTON))
playerX += deltaTime / 4;
if (arduboy.pressed(UP_BUTTON))
playerY -= deltaTime / 4;
if (arduboy.pressed(DOWN_BUTTON))
playerY += deltaTime / 4;
if (arduboy.pressed(A_BUTTON) and !aBuffer) {
showBG = !showBG;
aBuffer = true;
}
if (arduboy.pressed(A_BUTTON) and arduboy.pressed(B_BUTTON))
ResetGame();
if (arduboy.notPressed(A_BUTTON))
aBuffer = false;
arduboy.display();
}
void ResetGame()
{
aBuffer = false;
showBG = true;
playerX = 5;
playerY = 10;
return;
}
float CalculateDeltaTime(){
float currentTime = millis();
float deltaTime = currentTime - oldTime;
oldTime = currentTime;
return deltaTime;
}