Here's all my code. It's split into several files so sorry if it's difficult to figure out. I've also got to post it in a couple of replies as there's a 9000 character limit! There are also some bits I have commented out while I'm trying to fix up the level art stuff.
PART 1:
Main code:
#include "Player.cpp"
#include "Constants.h"
#include "LevelMap.cpp"
#include "Tile.cpp"
Arduboy2 arduboy;
Player player1;
LevelMap levelMap = new LevelMap(1);
int gameState = 0;
bool buttonA = false;
bool buttonB = false;
bool buttonLeft = false;
bool buttonRight = false;
void setup() {
arduboy.begin();
arduboy.setFrameRate(60);
arduboy.clear();
player1.Reset();
}
void loop() {
if (!arduboy.nextFrame()) // Check time for next frame.
return;
switch (gameState) {
case 0: // Title screen.
DrawTitleScreen();
if (arduboy.pressed(A_BUTTON) and !buttonA) {
gameState = 1;
buttonA = true;
}
break;
case 1: // Gameplay.
if (arduboy.pressed(RIGHT_BUTTON) and player1.velocityX < player1.velocityXMax) {
player1.velocityX += player1.velocityStep;
player1.directionX = 1;
}
if (arduboy.pressed(LEFT_BUTTON) and player1.velocityX > -player1.velocityXMax) {
player1.velocityX += -player1.velocityStep;
player1.directionX = -1;
}
// Jump:
if (arduboy.pressed(A_BUTTON) and !buttonA) {
player1.velocityY = -3 * 214;
buttonA = true;
}
UpdateGameObjects();
DrawGameObjects();
// Reset game:
if (arduboy.pressed(B_BUTTON) and !buttonB) {
gameState = 0;
player1.Reset();
buttonB = true;
}
break;
}
if (arduboy.notPressed(A_BUTTON))
buttonA = false;
if (arduboy.notPressed(B_BUTTON))
buttonB = false;
}
void UpdateGameObjects() {
//player1.Update(levelMap); // Update player.
}
void DrawGameObjects() {
arduboy.clear(); // Clear the screen.
levelMap.Draw();
player1.Draw(); // Draw player.
//arduboy.setCursor(0, 0);
//arduboy.print();
arduboy.display(); // Copy bitmap from frame buffer to display.
}
void DrawTitleScreen() {
arduboy.clear(); // Clear the screen.
arduboy.setCursor(48, 28);
arduboy.print("Sprite");
arduboy.display(); // Copy bitmap from frame buffer to display.
}
Bitmaps.h:
#pragma once
#include<Arduino.h>
// Player sprite.
const unsigned char playerRunRight[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0x00, 0x34, 0xfe, 0x8e, 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,
// Frame 1:
0x00, 0x00, 0x00, 0x00, 0x68, 0xf8, 0x1e, 0x68, 0xc, 0x6c, 0x1c, 0x24, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x2, 0xbf, 0xcd, 0xc6, 0xb4, 0x86, 0x89, 0xf1, 0x80, 0x00, 0x00, 0x00,
// Frame 2:
0x00, 0x00, 0x00, 0x10, 0x3a, 0xfc, 0x8c, 0x36, 0x6, 0x36, 0x8c, 0x9c, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf0, 0xfc, 0x66, 0x23, 0x5a, 0x43, 0xc4, 0xe8, 0x70, 0x00, 0x00, 0x00,
// Frame 3:
0x00, 0x00, 0x00, 0xa, 0x1e, 0x7e, 0x46, 0x9b, 0x3, 0x9b, 0x46, 0x4c, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x7c, 0x7e, 0x33, 0x11, 0x2d, 0x21, 0x62, 0x74, 0x38, 0x18, 0x00, 0x00,
// Frame 4:
0x00, 0x00, 0x00, 0x6, 0xf, 0x3f, 0xa3, 0xcd, 0x81, 0xcd, 0x23, 0x27, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x1c, 0x3e, 0x1f, 0x19, 0x8, 0x16, 0x10, 0x11, 0x3a, 0x1c, 0xe, 0x00, 0x00,
// Frame 5:
0x00, 0x00, 0x00, 0x4, 0xf, 0x3f, 0xa3, 0xcd, 0x81, 0xcd, 0x23, 0x27, 0x18, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x18, 0x3e, 0x3f, 0x19, 0x8, 0x16, 0x10, 0x31, 0x3a, 0x1c, 0xc, 0x00, 0x00,};
const unsigned char playerRunLeft[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0x60, 0x94, 0x8e, 0x36, 0x6, 0x34, 0x8e, 0xfe, 0x34, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xb8, 0xc4, 0x83, 0xda, 0xa3, 0xe6, 0x1e, 0x2, 0x00, 0x00, 0x00, 0x00,
// Frame 1:
0x00, 0x00, 0x00, 0xc0, 0x24, 0x1c, 0x6c, 0xc, 0x68, 0x1e, 0xf8, 0x68, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x80, 0xf1, 0x89, 0x86, 0xb4, 0xc6, 0xcd, 0xbf, 0x2, 0x00, 0x00, 0x00, 0x00,
// Frame 2:
0x00, 0x00, 0x00, 0x60, 0x9c, 0x8c, 0x36, 0x6, 0x36, 0x8c, 0xfc, 0x3a, 0x10, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x70, 0xe8, 0xc4, 0x43, 0x5a, 0x23, 0x66, 0xfc, 0xf0, 0x00, 0x00, 0x00, 0x00,
// Frame 3:
0x00, 0x00, 0x00, 0x30, 0x4c, 0x46, 0x9b, 0x3, 0x9b, 0x46, 0x7e, 0x1e, 0xa, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x38, 0x74, 0x62, 0x21, 0x2d, 0x11, 0x33, 0x7e, 0x7c, 0x30, 0x00, 0x00, 0x00,
// Frame 4:
0x00, 0x00, 0x00, 0x18, 0x27, 0x23, 0xcd, 0x81, 0xcd, 0xa3, 0x3f, 0xf, 0x6, 0x00, 0x00, 0x00,
0x00, 0x00, 0xe, 0x1c, 0x3a, 0x11, 0x10, 0x16, 0x8, 0x19, 0x1f, 0x3e, 0x1c, 0x00, 0x00, 0x00,
// Frame 5:
0x00, 0x00, 0x00, 0x18, 0x27, 0x23, 0xcd, 0x81, 0xcd, 0xa3, 0x3f, 0xf, 0x4, 0x00, 0x00, 0x00,
0x00, 0x00, 0xc, 0x1c, 0x3a, 0x31, 0x10, 0x16, 0x8, 0x19, 0x3f, 0x3e, 0x18, 0x00, 0x00, 0x00,};
const unsigned char playerIdleRight[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0x00, 0x34, 0xfe, 0x8e, 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,
// Frame 1:
0x00, 0x00, 0x00, 0x00, 0x34, 0xfe, 0x8e, 0x34, 0x6, 0x36, 0x8e, 0x94, 0x60, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1a, 0x3c, 0xcc, 0xe7, 0x96, 0x87, 0xc4, 0xf8, 0x00, 0x00, 0x00, 0x00,
// Frame 2:
0x00, 0x00, 0x00, 0x00, 0x68, 0xf8, 0x1e, 0x68, 0xc, 0x6c, 0x1c, 0x24, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x34, 0x4f, 0xcd, 0xe6, 0x84, 0x86, 0xc9, 0xf1, 0x30, 0x00, 0x00, 0x00,
// Frame 3:
0x00, 0x00, 0x00, 0x00, 0x68, 0xfc, 0x1c, 0x68, 0xc, 0x6c, 0x1c, 0x28, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x8, 0x3f, 0xe7, 0xb2, 0xca, 0x82, 0xc5, 0xb9, 0x20, 0x00, 0x00, 0x00,};
const unsigned char playerIdleLeft[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0x60, 0x94, 0x8e, 0x36, 0x6, 0x34, 0x8e, 0xfe, 0x34, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xb8, 0xc4, 0x83, 0xda, 0xa3, 0xe6, 0x1e, 0x2, 0x00, 0x00, 0x00, 0x00,
// Frame 1:
0x00, 0x00, 0x00, 0x60, 0x94, 0x8e, 0x36, 0x6, 0x34, 0x8e, 0xfe, 0x34, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xf8, 0xc4, 0x87, 0x96, 0xe7, 0xcc, 0x3c, 0x1a, 0x00, 0x00, 0x00, 0x00,
// Frame 2:
0x00, 0x00, 0x00, 0xc0, 0x24, 0x1c, 0x6c, 0xc, 0x68, 0x1e, 0xf8, 0x68, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0xf1, 0xc9, 0x86, 0x84, 0xe6, 0xcd, 0x4f, 0x34, 0x00, 0x00, 0x00, 0x00,
// Frame 3:
0x00, 0x00, 0x00, 0xc0, 0x28, 0x1c, 0x6c, 0xc, 0x68, 0x1c, 0xfc, 0x68, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x20, 0xb9, 0xc5, 0x82, 0xca, 0xb2, 0xe7, 0x3f, 0x8, 0x00, 0x00, 0x00, 0x00,};
const unsigned char playerFallRight[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0xa, 0x1e, 0x7e, 0x46, 0x9b, 0x3, 0x9b, 0x46, 0x4c, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x7c, 0x7e, 0x33, 0x11, 0x2d, 0x21, 0x62, 0x74, 0x38, 0x18, 0x00, 0x00,
// Frame 1:
0x00, 0x00, 0x00, 0x00, 0x1e, 0x7f, 0x46, 0x9b, 0x3, 0x9b, 0x47, 0x4d, 0x30, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x30, 0x7c, 0x7e, 0x33, 0x11, 0x2d, 0x21, 0x62, 0x74, 0x38, 0x18, 0x00, 0x00,};
const unsigned char playerFallLeft[] PROGMEM = {
// Width & Height:
16, 16,
// Frame 0:
0x00, 0x00, 0x00, 0x30, 0x4c, 0x46, 0x9b, 0x3, 0x9b, 0x46, 0x7e, 0x1e, 0xa, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x38, 0x74, 0x62, 0x21, 0x2d, 0x11, 0x33, 0x7e, 0x7c, 0x30, 0x00, 0x00, 0x00,
// Frame 1:
0x00, 0x00, 0x00, 0x30, 0x4d, 0x47, 0x9b, 0x3, 0x9b, 0x46, 0x7f, 0x1e, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x18, 0x38, 0x74, 0x62, 0x21, 0x2d, 0x11, 0x33, 0x7e, 0x7c, 0x30, 0x00, 0x00, 0x00,};
// Floor
const unsigned char floorTile[] = {
// Width & Height:
8, 8,
// Frame 0:
0xff, 0x81, 0x8d, 0x8d, 0x81, 0xa1, 0x83, 0xff,};
const unsigned char nullTile[] = {
// Width & Height:
8, 8,
// Frame 0:
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,};