My Code, Memory Problem

Hey everyone. Working on a little isometric adventure game. I’m at 82% dynamic memory. I want to add sound, more levels, enemies etc. But I don’t know how I can add more without totally running out of memory.

I will include my code. If anyone could glance at it and offer any suggestions. Thanks! BTW go easy on me I’m a BASIC programmer from way back this is my first attempt at C++.

//Matthew Bryan (TropicAnthropic)
//Began October 21st, 2017
//Path of the Sage 0.4

#include <Arduboy2.h>
#include <avr/pgmspace.h>
Arduboy2 arduboy;

unsigned char xyRoom[7][7]; // Block data for type of block and location
unsigned char xGet;
unsigned char yGet;
unsigned char valGet;
unsigned char xDraw;
unsigned char yDraw;

unsigned char chi = 0;
signed char balance = 0;

unsigned char count;
unsigned char Direction;

bool roomChange;
bool Level1;
bool Level2;

unsigned char RoomPop[7][7];
unsigned char roomX = 1;
unsigned char roomY = 2;

//Player Cords
signed char playerX; // Cords on the detection screen
signed char playerY;
unsigned char xyPlayer[7][7]; // Occupied Box on the grid
signed char xIso; // Projected position on screen
signed char yIso;
unsigned char Elevation; // Projected height on the screen
unsigned char calcX; // calculated player xy
unsigned char calcY;

// Objects
unsigned char object[] = {1,1,1,1,1,1,};

// **************************************************************************

const unsigned char playerMask[] PROGMEM  = {
    0x00, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff, 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00, 0x1, 0x3, 0x3, 0x87, 0xc7, 0x6f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0x7, 0x87, 0xf3, 0x1b, 0x1, 0x00, 0x00, 0x00, 0x1, 0x3, 0x2, 0x1f, 0x1f, 0x17, 0x7, 0x1f, 0x1f, 0x11, 0x1, 0xf, 0x19, 0x00, 0x00, 
};

const unsigned char player[] PROGMEM  = {
    0x00, 0x00, 0x80, 0x40, 0xa0, 0xd0, 0xe8, 0xbc, 0x5e, 0xaa, 0x14, 0xa8, 0x10, 0xa0, 0x40, 0x80, 0x00, 0x00, 0x00, 0x1, 0x00, 0x3, 0x3, 0x7, 0x5, 0x2, 0xd5, 0x42, 0xd4, 0x2, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3, 0x00, 0x3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char parchmentMask[] PROGMEM  = {
    0x20, 0x70, 0x78, 0xfc, 0xfe, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0x70, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x3, 0x3, 0x7, 0x3, 0x1, 0x00, 0x00, 0x00, 
};

const unsigned char parchment[] PROGMEM  = {
    0x00, 0x00, 0x00, 0x20, 0x10, 0x40, 0x4, 0x50, 0x8, 0xa0, 0x00, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Block0Mask[] PROGMEM  = {
    0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0xfe, 0xfe, 0xff, 0xff, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char Block0[] PROGMEM  = {
    0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0xee, 0xee, 0xf7, 0xf7, 0xbe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Block1Mask[] PROGMEM  = {
    0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0x1, 0x1, 0x3, 0x3, 0x7, 0x7, 0xf, 0xf, 0x1f, 0x1f, 0xf, 0xf, 0x7, 0x7, 0x3, 0x3, 0x1, 0x1,
};

const unsigned char Block1[] PROGMEM  = {
    0x00, 0x80, 0x50, 0x90, 0x38, 0xb8, 0x7c, 0x7c, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x2, 0x5, 0x2, 0x4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Block2Mask[] PROGMEM  = {
    0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};

const unsigned char Block2[] PROGMEM  = {
    0x00, 0x80, 0x50, 0x90, 0x38, 0xb8, 0x7c, 0x7c, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0xa, 0x15, 0xa, 0x15, 0x2a, 0x55, 0x2a, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Block3Mask[] PROGMEM  = {
    0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1, 0x1, 0x3, 0x3, 0x7, 0x7, 0xf, 0xf, 0x1f, 0x1f, 0xf, 0xf, 0x7, 0x7, 0x3, 0x3, 0x1, 0x1, 
};

const unsigned char Block3[] PROGMEM  = {
    0x00, 0x80, 0x50, 0x90, 0x38, 0xb8, 0x7c, 0x7c, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x2, 0x5, 0x2, 0x5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};


const unsigned char Block4Mask[] PROGMEM  = {
    0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Block4[] PROGMEM  = {
    0x00, 0x80, 0x50, 0x90, 0x38, 0xb8, 0x7c, 0x7c, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa, 0x15, 0xa, 0x15, 0x2a, 0x55, 0x2a, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

const unsigned char Background01[] PROGMEM = {
    0xbf, 0x6e, 0x2, 0xc, 0xc4, 0x98, 0x88, 0x30, 0x10, 0x60, 0x20, 0xc0, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x7d, 0x60, 0x20, 0x6b, 0x6f, 0x41, 0x63, 0xe1, 0xe6, 0x2, 0xc, 0x4, 0x19, 0x8, 0x33, 0x11, 0x66, 0x22, 0xcc, 0x44, 0x98, 0x88, 0x30, 0x10, 0x60, 0x20, 0xc0, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xae, 0x56, 0x6, 0x00, 0x77, 0x7b, 0x60, 0x6, 0xf6, 0xf6, 0x2, 0x64, 0x6e, 0x6e, 0x60, 0x40, 0x60, 0x60, 0x20, 0x60, 0x40, 0x21, 0x40, 0x23, 0x41, 0x26, 0x62, 0x4c, 0x64, 0x79, 0x28, 0x73, 0x71, 0x66, 0x62, 0x4c, 0x44, 0x18, 0x8, 0x30, 0x10, 0x60, 0x20, 0xc0, 0x40, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x6, 0x7, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x7, 0x7, 0x00, 0x00, 0x7, 0x7, 0x6, 0x6, 0x2, 0x6, 0x6, 0x4, 0x6, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x6, 0x2, 0x6, 0x6, 0x4, 0x6, 0x6, 0x6, 0x7, 0x6, 0x7, 0x7, 0x6, 0x6, 0x4, 0x4, 
};

const unsigned char Background02[] PROGMEM = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x40, 0xc0, 0x20, 0x60, 0x10, 0x30, 0x88, 0x98, 0xc4, 0xc, 0x2, 0x6e, 0xbf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x40, 0xc0, 0x20, 0x60, 0x10, 0x30, 0x88, 0x98, 0x44, 0xcc, 0x22, 0x66, 0x11, 0x33, 0x8, 0x19, 0x4, 0xc, 0x2, 0xe6, 0xe1, 0x63, 0x41, 0x6f, 0x6b, 0x20, 0x60, 0x7d, 0x76, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0x40, 0xc0, 0x20, 0x60, 0x10, 0x30, 0x8, 0x18, 0x44, 0x4c, 0x62, 0x66, 0x71, 0x73, 0x28, 0x79, 0x64, 0x4c, 0x62, 0x26, 0x41, 0x23, 0x40, 0x21, 0x40, 0x60, 0x20, 0x60, 0x60, 0x40, 0x60, 0x6e, 0x6e, 0x64, 0x2, 0xf6, 0xf6, 0x6, 0x60, 0x7b, 0x77, 0x00, 0x6, 0x56, 0xae, 0x4, 0x4, 0x6, 0x6, 0x7, 0x7, 0x6, 0x7, 0x6, 0x6, 0x6, 0x4, 0x6, 0x6, 0x2, 0x6, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x2, 0x6, 0x4, 0x6, 0x6, 0x2, 0x6, 0x6, 0x7, 0x7, 0x00, 0x00, 0x7, 0x7, 0x2, 0x4, 0x2, 0x4, 0x2, 0x4, 0x7, 0x6, 
};

const unsigned char Background03[] PROGMEM = {
    0x80, 0x60, 0x18, 0x46, 0x54, 0x54, 0x54, 0x54, 0xc4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3, 0x4, 0x6, 
};

const unsigned char Background04[] PROGMEM = {
    0x60, 0x98, 0x4, 0x2, 0x82, 0x59, 0x59, 0xc2, 0xe6, 0xfc, 0xf8, 0x60, 0x00, 0x1, 0x2, 0x4, 0x5, 0xe, 0xe, 0x7, 0x7, 0x3, 0x1, 0x00, 
};

const unsigned char Background05[] PROGMEM = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xc0, 0xc0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xff, 0xff, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0xe0, 0xc0, 0xc0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xc0, 0xc0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0xe0, 0xc0, 0xc0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xc0, 0xc0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xfc, 0xfc, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfc, 0xfc, 0xf8, 0xf8, 0xf0, 0xf0, 0xe0, 0xe0, 0xc0, 0xc0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x38, 0x38, 0x7c, 0x7c, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0x7c, 0x7c, 0x38, 0x38, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x3, 0x3, 0x7, 0x7, 0xf, 0xf, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x1f, 0xf, 0xf, 0x7, 0x7, 0x3, 0x3, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x3, 0x3, 0x7, 0x7, 0xf, 0xf, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x1f, 0xf, 0xf, 0x7, 0x7, 0x3, 0x3, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x3, 0x3, 0x7, 0x7, 0xf, 0xf, 0x1f, 0x1f, 0x3f, 0x3f, 0x7f, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x7f, 0x3f, 0x3f, 0x1f, 0x1f, 0xf, 0xf, 0x7, 0x7, 0x3, 0x3, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
};

unsigned char roomData[] = {
    0,0,0,1,0,0,0,
    0,0,1,2,1,0,0,
    0,1,2,3,2,1,0,
    1,2,3,4,3,2,1,
    0,1,2,3,2,1,0,
    0,0,1,2,1,0,0,
    0,0,0,1,0,0,0,
    };

unsigned char levelData[] = {
    11,21,31,
    12,22,32,
    13,23,33,
    14,24,34,
    };


void setup() {
  // put your setup code here, to run once:

// Starting Room
roomChange= false;
Level1 = true;
roomX = 1;
roomY = 2;

arduboy.begin();
arduboy.clear();

playerX = 14;
playerY = 14;
Elevation = 0;

//Populate First Level and Room

valGet=0;
for (unsigned char yGet = 0 ; yGet < 4 ; yGet = yGet + 1) {
for (unsigned char xGet = 0 ; xGet < 3 ; xGet = xGet + 1) {
RoomPop[xGet][yGet] = levelData[valGet]; valGet = valGet + 1; }
}

valGet=0;
for (unsigned char yGet = 0 ; yGet < 7 ; yGet = yGet + 1) {
for (unsigned char xGet = 0 ; xGet < 7 ; xGet = xGet + 1) {
if (RoomPop[roomX][roomY] == 23) { xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
}
}

}

void loop() {
  // Main Loop

arduboy.clear();

// Render Room

arduboy.drawBitmap( 0, 37, Background01, 53, 27, WHITE);
arduboy.drawBitmap( 75 , 37, Background02, 53, 27, WHITE);
arduboy.drawBitmap( 0, 0, Background03, 12, 11, WHITE);
arduboy.drawBitmap( 116, 0, Background04, 12, 12, WHITE);

xIso = (playerX - playerY)+55;
yIso = (playerX + playerY) / 2;

calcX = (playerX+9)/7;
calcY = (playerY+9)/7;
if(calcY == 7){calcY=6;}
if(calcX == 7){calcX=6;}

xyPlayer[calcX][calcY]=xyRoom[calcX][calcY];

if(xyPlayer[calcX][calcY] == 0){Elevation = 0;}
if(xyPlayer[calcX][calcY] == 1){Elevation = 4;}
if(xyPlayer[calcX][calcY] == 2){Elevation = 8;}
if(xyPlayer[calcX][calcY] == 3){Elevation = 12;}
if(xyPlayer[calcX][calcY] == 4){Elevation = 16;}

// Position Objects in Room

arduboy.drawBitmap( 8, 7, Background05, 112, 57, WHITE);

xDraw=55;
yDraw=7;
valGet=0;
count=0;
for (unsigned char yGet = 0 ; yGet < 7 ; yGet = yGet + 1) {
for (unsigned char xGet = 0 ; xGet < 7 ; xGet = xGet + 1) {

if (xyRoom[xGet][yGet] == 1){
arduboy.drawBitmap( xDraw, yDraw-4, Block1Mask, 18, 13, BLACK);
arduboy.drawBitmap( xDraw, yDraw-4, Block1, 18, 13, WHITE);
}
if (xyRoom[xGet][yGet] == 2){
arduboy.drawBitmap( xDraw, yDraw-8, Block2Mask, 18, 17, BLACK);
arduboy.drawBitmap( xDraw, yDraw-8, Block2, 18, 17, WHITE);
}
if (xyRoom[xGet][yGet] == 3){
arduboy.drawBitmap( xDraw, yDraw-11, Block3Mask, 18, 21, BLACK);
arduboy.drawBitmap( xDraw, yDraw-11, Block3, 18, 21, WHITE);
}
if (xyRoom[xGet][yGet] == 4){
arduboy.drawBitmap( xDraw, yDraw-15, Block4Mask, 18, 21, BLACK);
arduboy.drawBitmap( xDraw, yDraw-15, Block4, 18, 21, WHITE);
}
xDraw = xDraw + 8;
yDraw = yDraw + 4;
count = count + 1;

if(Level1 == true){
//Place Objects in LEVEL1
if (RoomPop[roomX][roomY] == 14 && object[0] == 1){
arduboy.drawBitmap( 11,15, parchmentMask, 15, 11, BLACK);
arduboy.drawBitmap( 11,15, parchment, 15, 11, WHITE);
}
if (RoomPop[roomX][roomY] == 13 && object[1] == 1){
arduboy.drawBitmap( 58,-3, parchmentMask, 15, 11, BLACK);
arduboy.drawBitmap( 58,-3, parchment, 15, 11, WHITE);
}
if (RoomPop[roomX][roomY] == 11 && object[2] == 1){
arduboy.drawBitmap( 58,-3, parchmentMask, 15, 11, BLACK);
arduboy.drawBitmap( 58,-3, parchment, 15, 11, WHITE);
}
if (RoomPop[roomX][roomY] == 33 && object[3] == 1){
arduboy.drawBitmap( 55,25, parchmentMask, 15, 11, BLACK);
arduboy.drawBitmap( 55,25, parchment, 15, 11, WHITE);
}
if (RoomPop[roomX][roomY] == 34 && object[4] == 1){
arduboy.drawBitmap( 31,28, parchmentMask, 15, 11, BLACK);
arduboy.drawBitmap( 31,28, parchment, 15, 11, WHITE);
}
}

if(count<7){if(calcY==0){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(count<14){if(calcY==1){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(count<21){if(calcY==2){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(count<28){if(calcY==3){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(count<35){if(calcY==4){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(count<42){if(calcY==5){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}}
if(calcY==6){arduboy.drawBitmap(xIso, yIso-Elevation, playerMask, 18, 21, BLACK);
arduboy.drawBitmap(xIso, yIso-Elevation, player, 18, 21, WHITE);}


//arduboy.display();
//delay(5);
}
if (count == 7) { xDraw = 47; yDraw = 11; };
if (count == 14) { xDraw = 39; yDraw = 15; };
if (count == 21) { xDraw = 31; yDraw = 19; };
if (count == 28) { xDraw = 23; yDraw = 23; };
if (count == 35) { xDraw = 15; yDraw = 27; };
if (count == 42) { xDraw = 7; yDraw = 31; };
}


if( arduboy.pressed(UP_BUTTON) == true && playerY > -10) {
    if(xyRoom[calcX][calcY-1]<(xyPlayer[calcX][calcY])+2){
    playerY = playerY - 1;} else {if(calcY == 0){playerY = playerY - 1;}}
}

if( arduboy.pressed(DOWN_BUTTON) == true && playerY < 41) {
    if(xyRoom[calcX][calcY+1]<(xyPlayer[calcX][calcY])+2){
    playerY = playerY + 1;} else {if(calcY == 6){playerY = playerY + 1;}}
}

if( arduboy.pressed(LEFT_BUTTON) == true && playerX > -10) {
    if(xyRoom[calcX-1][calcY]<(xyPlayer[calcX][calcY])+2){
    playerX = playerX - 1;} else {if(calcX == 0){playerX = playerX - 1;}}
}

if( arduboy.pressed(RIGHT_BUTTON) == true && playerX < 41) {
    if(xyRoom[calcX+1][calcY]<(xyPlayer[calcX][calcY])+2){
    playerX = playerX + 1;} else {if(calcX == 6){playerX = playerX + 1;}}
}

arduboy.setCursor(2,13);
arduboy.print(chi);
arduboy.setCursor(119, 13);
arduboy.print(balance);

arduboy.display();
//delay(10);

// Room Edge Detection

roomChange = false;
//Directions: 1=North, 2=East, 3=West, 4=South
Direction = 0;
if (Level1 == true){
if(playerX < -9 && roomX>0) {roomChange = true;Direction = 3;}
if(playerY < -9 && roomY>0) {roomChange = true;Direction = 1;}
if(playerX > 40 && roomX<2) {roomChange = true;Direction = 2;}
if(playerY > 40 && roomY<3) {roomChange = true;Direction = 4;}
}

if (Level2 == true){
if(playerX < -9  && roomX>0) {roomChange = true;Direction = 3;}
if(playerY < -9 && roomY>0) {roomChange = true;Direction = 1;}
if(playerX > 40 && roomX<4) {roomChange = true;Direction = 2;}
if(playerY > 40 && roomY<4) {roomChange = true;Direction = 4;}
}

// ********************* LEVEL1 ***********************

if(Level1 == true){

if (chi == 35 && RoomPop[roomX][roomY] == 31 && calcY == 0 && calcX == 3){
Level1 = false;
Level2 = true;
roomChange = true;
unsigned char levelData[] = {
    000,010,020,030,040,
    001,011,021,031,041,
    002,012,022,032,042,
    003,013,023,033,043,
    004,014,024,034,044,};   
playerY = 30;
roomX=1;
roomY=4;
valGet=0;
for (unsigned char yGet = 0 ; yGet < 5 ; yGet = yGet + 1) {
for (unsigned char xGet = 0 ; xGet < 5 ; xGet = xGet + 1) {
RoomPop[xGet][yGet] = levelData[valGet]; valGet = valGet + 1; }
}
valGet=0;
for (unsigned char yGet = 0 ; yGet < 7 ; yGet = yGet + 1) {
for (unsigned char xGet = 0 ; xGet < 7 ; xGet = xGet + 1) {
xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1;
}
}
}


//Detect Object Collision

if (RoomPop[roomX][roomY] == 14 && calcX == 0 && calcY == 6 && object[0] == 1){
  chi = chi + 7; balance = balance + 1; object[0] = 0;
}

if (RoomPop[roomX][roomY] == 13 && calcX == 1 && calcY == 1 && object[1] == 1){
  chi = chi + 7; balance = balance + 1; object[1] = 0;
}

if (RoomPop[roomX][roomY] == 11 && calcX == 1 && calcY == 1 && object[2] == 1){
  chi = chi + 7; balance = balance + 1; object[2] = 0;
}

if (RoomPop[roomX][roomY] == 33 && calcX == 3 && calcY == 3 && object[3] == 1){
  chi = chi + 7; balance = balance + 1; object[3] = 0;
}

if (RoomPop[roomX][roomY] == 34 && calcX == 2 && calcY == 5 && object[4] == 1){
  chi = chi + 7; balance = balance + 1; object[4] = 0;
}

count = 0;  
if(roomChange == true){

if(Direction == 1){roomY=roomY-1;count = 1;}
if(Direction == 2){roomX=roomX+1;count = 1;}
if(Direction == 3){roomX=roomX-1;count = 1;}
if(Direction == 4){roomY=roomY+1;count = 1;}

      if(Direction == 1){
        if(count==1){playerY=39;}
        }
      if(Direction == 2){
        if(count==1){playerX=-8;}
        }
      if(Direction == 3){
        if(count==1){playerX=39;}
        }
      if(Direction == 4){
        if(count==1){playerY=-8;}
        }  
  valGet=0;
  for (unsigned char yGet = 0 ; yGet < 7 ; yGet = yGet + 1) {
    for (unsigned char xGet = 0 ; xGet < 7 ; xGet = xGet + 1) {
      if (RoomPop[roomX][roomY] == 23) {unsigned char roomData[49] =
                                            {0,0,0,1,0,0,0,
                                             0,0,1,2,1,0,0,
                                             0,1,2,3,2,1,0,
                                             1,2,3,4,3,2,1,
                                             0,1,2,3,2,1,0,
                                             0,0,1,2,1,0,0,
                                             0,0,0,1,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 33) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,4,2,3,2,4,0,
                                             0,2,0,0,0,2,0,
                                             0,0,0,0,0,3,0,
                                             0,2,0,0,0,2,0,
                                             0,4,2,3,2,4,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 13) {unsigned char roomData[49] = 
                                            {0,0,0,0,0,1,0,
                                             0,4,0,0,0,0,0,
                                             0,3,2,1,0,0,0,
                                             0,3,3,0,0,0,0,
                                             0,0,0,0,1,1,0,
                                             0,0,0,0,0,0,0,
                                             0,1,2,1,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 14) {unsigned char roomData[49] =
                                             {0,0,0,0,0,0,0,
                                             0,0,0,0,1,0,0,
                                             0,0,0,1,2,1,0,
                                             1,0,0,0,1,0,0,
                                             2,0,0,0,0,0,0,
                                             3,0,0,0,0,0,0,
                                             4,3,2,1,0,0,0,};
      
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 24) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,1,0,0,0,1,0,
                                             0,0,0,0,0,2,0,
                                             0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,0,
                                             0,4,2,0,0,1,0,
                                             0,0,0,0,1,2,0,};
      
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 34) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,0,1,0,0,0,0,
                                             0,0,0,0,0,1,0,
                                             0,0,0,0,0,0,0,
                                             0,0,2,2,1,0,0,
                                             0,0,2,2,0,0,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 12) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,0,
                                             0,1,0,0,0,0,0,
                                             0,0,0,0,0,1,0,
                                             1,0,0,0,0,0,0,
                                             0,0,0,1,0,0,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 32) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,3,
                                             0,4,0,1,0,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,3,0,0,0,0,
                                             0,0,0,0,0,2,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 21) {unsigned char roomData[49] =
                                            {0,1,2,1,0,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,0,0,0,1,0,
                                             0,0,0,0,0,0,0,
                                             1,2,1,0,2,0,0,
                                             0,0,0,0,1,0,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 11) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,4,3,2,1,0,0,
                                             0,3,3,2,1,0,0,
                                             0,2,2,2,1,0,0,
                                             0,1,1,1,1,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 31) {unsigned char roomData[49] =
                                            {2,2,4,0,4,2,2,
                                             0,0,0,0,0,0,0,
                                             0,0,1,0,1,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,1,0,1,0,0,
                                             0,0,0,0,0,0,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
      if (RoomPop[roomX][roomY] == 22) {unsigned char roomData[49] =
                                            {0,0,0,0,0,0,0,
                                             0,2,0,0,0,2,0,
                                             0,3,1,1,1,3,0,
                                             0,4,2,2,2,4,0,
                                             0,3,1,1,1,3,0,
                                             0,2,0,0,0,2,0,
                                             0,0,0,0,0,0,0,};
        xyRoom[xGet][yGet] = roomData[valGet]; valGet = valGet + 1; }
     }
   }  
}
}

}
1 Like

Allot of this can be stored in progmem

roomData and levelData could be in progmem if you aren’t using them.

You could make valGet local rather than global since it seems you’re only using it as a sort of counter. Same for calcX and calcY and possibly some of the others.
(Global variables are more expensive than local variables.)

Otherwise @Dreamer2345 was pretty much spot-on, those 49-byte roomData arrays in your if statements aren’t helping.


I can think of quite a few ways to save you progmem too if you’re interested in progmem savings.

1 Like

Can you say how I read an array from PROGMEM?

On my room and level data I overwrite the old with the new. Wouldn’t that mean that it’s only using the memory space once. Or does it see every instance and use more memory evey time?

I’m not sure how to use local variables. I’ll have to look into it.

Page 13 in Volume 7 of the mag will help you here.

This may help > http://www.learncpp.com/cpp-tutorial/4-1a-local-variables-and-local-scope/

2 Likes

Short version:

// Array definition
const uint8_t someArray[] PROGMEM = { 1, 2, 3, 4, 5 };

// Somewhere in code
uint8_t data = pgm_read_byte(&someArray[index]);

If you’re doing this for e.g. a map then you can cheat by doing:

memcpy_P(&ramArray[0], &progmemArray[0], bytesToCopy);

If you’re storing in progmem it’s stored once.
If you’re doing this:

That array is being stored once in progmem and once again in RAM.

The simple version is:

uint8_t globalVar;

void loop()
{
  uint8_t localVar;
}

Globals stay around forever in a particular area of RAM.
Global variables have to be read from and written to RAM when modifying them.

Locals live until the end of the scope they’re declared in.
Locals don’t necessarily end up in RAM, if they have a short enough lifespan they’ll just live in a CPU register.

A scope can be thought of as a set of curly braces either defining a function or inside a function (e.g. the braces of a while, for, if etc).
(If you don’t know the difference between RAM and registers, just think of registers as small & fast memory and RAM as big & slow memory for now. (Unless you want to know the full details.))


@filmote’s links are both very handy, be sure to read them.
I particular the V7P13 article is very much like the sort of thing you’re trying to do and it discusses multidimensional arrays so it’s worth a read.

1 Like

Thanks for all the great info!

So when I declare a variable at the beginning it makes it global. Cool I just won’t declare everything at the beginning that should save some.

You have been very helpful thanks. I’ll need some time to digest everything.

I think I’ll put all my level data into permanent PROGMEM and just read the data when I draw a new room. That should help a lot. I guess the way I’m doing it is that I have created a few global 2 dimensional arrays. They need to go.

1 Like

Keep in mind that local variables still may take up RAM space. Although that space can be shared by multiple variables in different scopes, if a scope contains many local variables you could still run out of RAM, manifesting as problems that might be difficult to predict or explain.

However, it’s still better to use local variables instead of globals where possible.

1 Like

I’ll keep that in mind, thanks. As I said I grew up programming BASIC where all variables are global variables.

1 Like