//Suit Shooter
//	Version 2.0, Sep 6, 2016
//	By Jonathan Holmes (crait)
//
//	Website: http://www.crait.net/
//	Twitter: @crait
//
//Launch suits of cards at falling cards in order to create matches and
//score points. Watch out, though! Missing cards will hurt you! The cards
//start out falling slowly, but get faster as you score more points and
//regain health.
//A: Lauch current card
//Up/Down: Change row
//Left/Right: Change Suit
//B + Up: Erase high score data
//
//2.0 - Nov 24, 2016
//	Updated the graphics and layout to look more fun!
//1.2 - Sep 10, 2016
//	Added the ability to pause
//1.1 - Sep 9, 2016
//	Switched to standard get()/put() for storing data into EEPROM
//	Removed title and instruction screen from repeating
//	Elaborated on controls and instructions
//	Added the option to erase game data
//1.0 - Sep 6, 2016
//	Initial release


#include "Arduboy.h"
#include <EEPROM.h>
Arduboy arduboy;
#define BLACK 0
#define WHITE 1
#define OFF HIGH
#define ON LOW
#define SAVELOCATION (EEPROM_STORAGE_SPACE_START + 402)
#define CHAROFFSET 3
#define GAMESPEED 3


boolean buttonfree = true;
boolean shootfree = true;
boolean paused = false;
int movementframe = GAMESPEED;
int gamestate = 50;
int lives = 5;
int level = 0;
int bricksblasted = 0;
int blasttimer = 30;
int score = 0;
int selectedbrick = 1;
int y = 2;
int highscore = 0;
char text[24];
unsigned char timer = 0;
unsigned char fireanimation = 0;

int blastingx[5] = {0, 0, 0, 0, 0};
int blastingtypes[5] = {0, 0, 0, 0, 0};

int fallingx[5][3] = { {128, 128, 128}, {128, 128, 128}, {128, 128, 128}, {128, 128, 128}, {128, 128, 128} };
int fallingtypes[5][3] = { {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {3, 0, 0} };

const static unsigned char abutton[] PROGMEM = {
	0xfc, 0x42, 0xb9, 0x95, 0xb9, 0x42, 0xfc, 0x00, 
	0x00, 0x82, 0x7c, 0x38, 0x10, 0x00, 0x00, 0x00, 
	0x00, 0x01, 0x02, 0x02, 0x02, 0x01, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

const unsigned char me[] PROGMEM = {
	0x45, 0x8a, 0x15, 0xaa, 0x15, 0xaa, 0xd5, 0x6a,
	0x34, 0x28, 0x21, 0x32, 0x1d, 0x0a, 0x0c, 0x06,
	0x03, 0x02, 0x07, 0x02, 0x23, 0x41, 0xa0, 0x41,
	0x80, 0x40, 0x81, 0x40, 0x20, 0x80, 0x40, 0x80,
	0x40, 0x20, 0x08, 0xc7, 0x7d, 0xa2, 0x55, 0xa2,
	0x51, 0xa8, 0x15, 0x88, 0x44, 0xa2, 0x51, 0xaa,
	0x54, 0xa8, 0x51, 0x82, 0x05, 0xff, 0x00, 0x00,
	0x41, 0x80, 0x80, 0x00, 0x80, 0xe0, 0xf0, 0xf0,
	0xf0, 0xf0, 0xe0, 0x60, 0x60, 0x60, 0x68, 0xed,
	0xe4, 0xe3, 0xe0, 0xe0, 0x61, 0x72, 0xe1, 0x02,
	0xe1, 0x32, 0x1a, 0xaf, 0x11, 0x88, 0x54, 0x8a,
	0x45, 0x2a, 0x05, 0x92, 0x45, 0xa2, 0x41, 0xa0,
	0x55, 0xaa, 0x55, 0xaa, 0x55, 0xbf, 0xe0, 0x00,
	0x00, 0x00, 0x00, 0x3c, 0xfe, 0xff, 0xff, 0xff,
	0x1d, 0xec, 0x36, 0x37, 0xf7, 0xf7, 0xee, 0x1f,
	0xff, 0x3f, 0xdf, 0x2e, 0x37, 0xf6, 0xee, 0x1c,
	0x03, 0x7e, 0x55, 0xca, 0xc5, 0xa2, 0x51, 0xa8,
	0x54, 0xaa, 0x55, 0x2a, 0x15, 0x8a, 0x45, 0x22,
	0x51, 0xa2, 0x54, 0xa0, 0x55, 0xfe, 0xc3, 0xbc,
	0x66, 0x40, 0x00, 0x38, 0xdf, 0x7f, 0xbf, 0xff,
	0xff, 0xfe, 0x7d, 0x7d, 0x7d, 0xfd, 0xfe, 0x3f,
	0x7f, 0xff, 0xf0, 0x6d, 0x1d, 0xbd, 0xfe, 0xff,
	0xe0, 0x83, 0x3f, 0xc0, 0x7f, 0xa2, 0x55, 0xa2,
	0x51, 0x28, 0x04, 0xa2, 0x51, 0xa8, 0x54, 0xaa,
	0x11, 0xa2, 0x15, 0xa2, 0xc5, 0x6a, 0x45, 0x63,
	0x41, 0x6b, 0x5e, 0x78, 0x71, 0xa6, 0x4b, 0x8d,
	0x97, 0x9c, 0x38, 0x31, 0x23, 0x06, 0x46, 0x4d,
	0x4d, 0x6d, 0x72, 0x7f, 0x6f, 0x1f, 0x37, 0x0f,
	0x1d, 0xcb, 0xe0, 0x7f, 0x55, 0x6a, 0x45, 0x40,
	0xc0, 0x8a, 0xc5, 0x6a, 0xb5, 0xda, 0xed, 0xca,
	0x3d, 0xce, 0xf3, 0xfd, 0xfe, 0xf8, 0x87, 0x7f,
	0xff, 0xff, 0xff, 0xff, 0xc0, 0xbf, 0xbe, 0xbd,
	0xdb, 0x57, 0xa7, 0xcf, 0xb7, 0x8b, 0xfa, 0xf6,
	0x76, 0xa6, 0xca, 0x76, 0xea, 0x9c, 0x5f, 0xdf,
	0xc0, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x0f,
	0xb6, 0xb9, 0xfb, 0xf8, 0xf3, 0xf7, 0xff, 0xf8
};

const static unsigned char left[] PROGMEM = {
	0x8, 0x1c, 0x3e, 
};

const static unsigned char right[] PROGMEM = {
	0x3e, 0x1c, 0x8, 
};

const static unsigned char shapes[][18] PROGMEM = {
	{
		//Clubs
		0x30, 0x48, 0x4e, 0x31, 0xd1, 0x31, 0x4e, 0x48, 0x30, 0x00, 0x00, 0x1, 0x1, 0x1, 0x1, 0x1, 0x00, 0x00, 
	},
	{
		//Spade
		0x30, 0x48, 0x54, 0xaa, 0x45, 0xa2, 0x44, 0x48, 0x30, 0x00, 0x00, 0x1, 0x1, 0x1, 0x1, 0x1, 0x00, 0x00, 
	},
	{
		//Diamond
		0x10, 0x38, 0x6c, 0xf6, 0xfb, 0xfe, 0x7c, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 
	},
	{
		//Heart
		0x1c, 0x22, 0x7a, 0xfe, 0xfc, 0xfe, 0x7e, 0x3e, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 
	},
};

const static unsigned char shapesblack[][18] PROGMEM = {
	{
		//Clubs
		0x30, 0x48, 0xb6, 0xb1, 0xce, 0x2e, 0xce, 0xb1, 0xb6, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 
	},
	{
		//Spade
		0x30, 0x48, 0xb4, 0xaa, 0x55, 0xba, 0x5d, 0xba, 0xb4, 0x00, 0x00, 0x1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1, 
	},
	{
		//Diamond
		0x10, 0x28, 0x44, 0x92, 0x9, 0x4, 0x1, 0x82, 0x44, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x00, 0x00, 
	},
	{
		//Heart
		0x1c, 0x22, 0x5d, 0x85, 0x1, 0x2, 0x1, 0x81, 0x41, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x00, 0x00, 
	},
};

const static unsigned char fires[][30] PROGMEM = {
	{
		0x8, 0x40, 0x4a, 0x12, 0xb5, 0xea, 0x7e, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x1, 0x1, 0x1, 0x00, 0x1, 0x1, 0x1, 0x1, 0x1, 
	},
	{
		0x10, 0x44, 0x14, 0x5d, 0x7a, 0x57, 0x5b, 0x7f, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x1, 0x1, 0x1, 0x00, 0x1, 0x1, 0x1, 0x1, 
	},
	{
		0x8, 0x8, 0x44, 0x29, 0xbe, 0xfd, 0x77, 0x5e, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x1, 0x00, 0x1, 0x00, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 
	}
};

const static unsigned char smallshapes[][18] PROGMEM = {
	{
		//Clubs
		0x00, 0x10, 0x28, 0xbc, 0xf4, 0xbc, 0x28, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	},
	{
		//Spade
		0x00, 0x10, 0x38, 0x9c, 0xfe, 0x9c, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	},
	{
		//Diamond
		0x00, 0x10, 0x38, 0x74, 0xfa, 0x7c, 0x38, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	},
	{
		//Heart
		0x00, 0x18, 0x3c, 0x7c, 0xf8, 0x7c, 0x3c, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	},
};

const static unsigned char gauge[] PROGMEM = {
	0x1c, 0x22, 0x41, 0x45, 0x41, 0x45, 0x7f, 0x45, 0x45, 0x45, 0x45, 0x45, 0x7f, 0x45, 0x45, 0x45, 0x45, 0x45, 0x7f, 0x45, 0x45, 0x45, 0x45, 0x45, 0x7f, 0x45, 0x41, 0x45, 0x41, 0x22, 0x1c, 0x00, 
};

const static unsigned char title[] PROGMEM = {
	0x00, 0x00, 0x00, 0x00, 0xc0, 0xf0, 0x18, 0x08, 
	0x0c, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 
	0x0c, 0x0c, 0x1c, 0x3c, 0x3c, 0x78, 0x80, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xc0, 
	0x00, 0x00, 0x00, 0x80, 0xc0, 0x00, 0x00, 0x80, 
	0x40, 0xce, 0xce, 0x00, 0x00, 0x20, 0xe0, 0xf8, 
	0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 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, 0xe0, 
	0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xfc, 0xf8, 0xf0, 
	0xe0, 0xe0, 0xc0, 0x80, 0x80, 0x80, 0xc0, 0xe0, 
	0xf0, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x07, 0x1f, 0x3e, 0x7c, 
	0x78, 0x78, 0xf0, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 
	0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0x80, 0x81, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x7f, 
	0x40, 0x20, 0x10, 0x7f, 0x7f, 0x40, 0x20, 0x00, 
	0x00, 0x7f, 0x7f, 0x40, 0x40, 0x00, 0x7f, 0x7f, 
	0x40, 0x40, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 
	0xf6, 0xfe, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xf7, 0x0f, 0x1f, 0xff, 0xff, 0xfe, 0xfc, 0xf8, 
	0xf0, 0xe0, 0xe0, 0xc0, 0x80, 0x00, 0x00, 0x00, 
	0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 
	0x01, 0x01, 0x83, 0xe3, 0x37, 0x17, 0x1f, 0xcf, 
	0xee, 0xec, 0x08, 0x08, 0x08, 0x08, 0x18, 0x18, 
	0x38, 0x78, 0x78, 0xf0, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0xf0, 0xfc, 0x04, 0x04, 0x84, 
	0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 
	0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x40, 
	0xc0, 0xf0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x80, 
	0x81, 0x0f, 0x1f, 0x1f, 0xbf, 0xbf, 0x3f, 0x3f, 
	0x3f, 0x1f, 0x0f, 0xc7, 0xff, 0xe7, 0x0f, 0xff, 
	0xff, 0xbf, 0xbf, 0x3f, 0x3f, 0x1f, 0x1f, 0x87, 
	0x81, 0xe0, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0x7f, 0x3f, 0x1f, 0x0c, 0x00, 
	0x00, 0x01, 0x07, 0x0f, 0x1e, 0x1c, 0x38, 0x38, 
	0x30, 0x70, 0x70, 0x60, 0x60, 0x60, 0x60, 0x60, 
	0x60, 0x60, 0x6f, 0x3f, 0x3c, 0xb8, 0xdc, 0xef, 
	0xe7, 0xe1, 0xe0, 0xe0, 0xc0, 0xc0, 0xc0, 0xc0, 
	0x80, 0x80, 0x80, 0x00, 0x03, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0xff, 0x7f, 0x02, 0x01, 0x01, 
	0xff, 0xff, 0x80, 0x00, 0x3c, 0x7f, 0xc1, 0x80, 
	0x80, 0x41, 0x7f, 0x1e, 0x00, 0x00, 0x3c, 0x7f, 
	0xc1, 0x80, 0x80, 0x41, 0x7f, 0x1e, 0x00, 0x00, 
	0xff, 0xff, 0x80, 0x80, 0x00, 0x3c, 0xff, 0xd1, 
	0x88, 0x84, 0x83, 0x63, 0x01, 0x00, 0x01, 0xff, 
	0x7f, 0x02, 0x01, 0x01, 0x03, 0x01, 0x00, 0x00, 
	0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 
	0x01, 0x03, 0x0f, 0x1f, 0x3f, 0x7f, 0xff, 0xff, 
	0xff, 0xff, 0xff, 0x7f, 0x3f, 0x1f, 0x0f, 0x07, 
	0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x01, 0x01, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 
	0x07, 0x07, 0x0f, 0x0f, 0x3f, 0xfe, 0xfc, 0xf8, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 
	0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 
	0x0f, 0x1e, 0x3c, 0x38, 0x70, 0x70, 0x60, 0xe0, 
	0xe0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 
	0xc0, 0x40, 0x60, 0x70, 0x38, 0x1f, 0x0f, 0x03, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};

void resetgame() {
	paused = false;
	lives = 5;
	level = 0;
	bricksblasted = 0;
	blasttimer = 30;
	score = 0;
	selectedbrick = CHAROFFSET;
	movementframe = GAMESPEED;
	y = 2;
	int j;
	int k;
	for(j = 0; j < 5; j++) {
		blastingx[j] = 0;
		blastingtypes[j] = 0;
		for(k = 0; k < 3; k++) {
			fallingx[j][k] = 128 + (300 + rand() % 350) * (k + 1);
			fallingtypes[j][k] = rand() % 4 + CHAROFFSET;
		}
	}
	fallingx[0][0] = 270;
	fallingx[2][0] = 130;
	fallingx[4][0] = 380;

	fallingx[2][2] = 1000;
	
	fallingx[0][2] = 5000;
	fallingx[1][2] = 4200;
	fallingx[2][2] = 5200;
	fallingx[3][2] = 4500;
	fallingx[4][2] = 5300;
	shootfree = false;
}

void setup() {
	arduboy.begin();
	arduboy.setFrameRate(150);
	arduboy.display();
	srand(7/8);
	EEPROM.get(SAVELOCATION, highscore);
	arduboy.clear();
	
}

void loop() {
	arduboy.clear();
	movementframe -= 1;
	if(movementframe < 0) {
		movementframe = GAMESPEED;
	}
	fireanimation += 1;
	switch (gamestate) {
			case 50:	//Credits Screen
				arduboy.drawBitmap(8, 8, me, 48, 48, WHITE);
				arduboy.setCursor(75, 8);
				arduboy.print(F("Jonathan"));
				arduboy.setCursor(87, 18);
				arduboy.print(F("Holmes"));
				arduboy.setCursor(86, 38);
				arduboy.print(F("@crait"));
				arduboy.setCursor(68, 49);
				arduboy.print(F("crait.net"));
				if(arduboy.pressed(A_BUTTON) && shootfree) {
					gamestate = 0;
					shootfree = false;
					buttonfree = false;
					resetgame();
				}
				break;
  		case 0:	//Title
			arduboy.drawBitmap(0, 0, title, 128, 48, WHITE);
			if(arduboy.pressed(A_BUTTON) && shootfree) {
				gamestate = 1;
				shootfree = false;
				buttonfree = false;
				resetgame();
			}

  			break;
		case 1:
			arduboy.setCursor(0, 0);
			arduboy.print(F("     Instructions\n\nMatch pairs of shapes\nby launching cards to\nget points, but don't\nmiss too many!\n"));

			if(arduboy.pressed(A_BUTTON) && shootfree) {
				gamestate = 15;
				shootfree = false;
				buttonfree = false;
				resetgame();
			}
			arduboy.drawBitmap(110, 51, abutton, 16, 16, WHITE);
			break;
		case 15:	//Controls
			arduboy.drawBitmap(110, 51, abutton, 16, 16, WHITE);
			arduboy.setCursor(0, 0);
			arduboy.print(F("       Controls\n\n    A: Shoot suit\n    B: Pause\n"));
			sprintf(text, "   %c%c: Change suit\n", 27, 26);
			arduboy.print(text);
			sprintf(text, "   %c%c: Change row\n", 24, 25);
			arduboy.print(text);
			sprintf(text, "B + %c: Erase data", 24, 25);
			arduboy.print(text);
			if(arduboy.pressed(B_BUTTON) && arduboy.pressed(UP_BUTTON) && buttonfree) {
				gamestate = 16;
				buttonfree = false;
				highscore = 0;
				EEPROM.put(SAVELOCATION, highscore);
			}
			if(arduboy.pressed(A_BUTTON) && buttonfree) {
				gamestate = 2;
				buttonfree = false;
				resetgame();
			}
			break;
		case 16:	//Erased
			arduboy.drawBitmap(110, 51, abutton, 16, 16, WHITE);
			arduboy.setCursor(0, 0);
			arduboy.print(F("       Complete\n\nAll high score data\nwas erased..."));
			if(arduboy.pressed(A_BUTTON) && buttonfree) {
				gamestate = 0;
				buttonfree = false;
				resetgame();
			}
			break;
  	case 2:	//Game
  	
			if(arduboy.pressed(B_BUTTON) && buttonfree) {
				paused = !paused;
				buttonfree = false;
			}
			if(paused) {
				arduboy.setCursor(0, 0);
				arduboy.print(F("        Paused        "));
				arduboy.display();
			} else {
			if(arduboy.pressed(LEFT_BUTTON) && buttonfree) {
				selectedbrick -= 1;
				buttonfree = false;
			}
			if(arduboy.pressed(RIGHT_BUTTON) && buttonfree) {
				selectedbrick += 1;
				buttonfree = false;
			}
			if(arduboy.pressed(UP_BUTTON) && buttonfree) {
				y -= 1;
				buttonfree = false;
			}
			if(arduboy.pressed(DOWN_BUTTON) && buttonfree) {
				y += 1;
				buttonfree = false;
			}
			if(selectedbrick > 3 + CHAROFFSET) {
				selectedbrick = CHAROFFSET;
			}
			if(selectedbrick < CHAROFFSET) {
				selectedbrick = 3 + CHAROFFSET;
			}
			if(y > 4) {
				y = 4;
			}
			if(y < 0) {
				y = 0;
			}
			
			if(arduboy.pressed(A_BUTTON) && shootfree) {
				if(blastingtypes[y] == 0) {
					blastingtypes[y] = selectedbrick;
				}
				shootfree = false;
			}
			
			int i;
			int z;
			for(i = 0; i < 5; i++) {
				//My Bricks
				if(blastingtypes[i] != 0) {
					blastingx[i] += 1;
					if(blastingx[i] > 120) {
						blastingx[i] = 0;
						blastingtypes[i] = 0;
					} else {
						//arduboy.setCursor(8 + blastingx[i], i * 10 + 12);
						//sprintf(text, "%c", blastingtypes[i]);
						arduboy.drawBitmap(blastingx[i] - 3, i * 10 + 15, fires[((fireanimation + i) / 10) % 3], 15, 16, WHITE);
						arduboy.drawBitmap(7 + blastingx[i], i * 10 + 15, shapesblack[blastingtypes[i] - 3], 9, 16, BLACK);
						arduboy.drawBitmap(8 + blastingx[i], i * 10 + 15, shapes[blastingtypes[i] - 3], 9, 16, WHITE);
						//arduboy.print(text);
						//arduboy.drawLine(8, i * 10 + 15 + 5, 8 + blastingx[i], i * 10 + 15 + 4, WHITE);
					}
				}
				//Falling Bricks	
				for(z = 0; z < 3; z++) {
					if(fallingtypes[i][z] != 0) {
						if(movementframe == 0) {
							fallingx[i][z] -= 1;
						}
						if(fallingx[i][z] < 9) {
							lives -= 1;
							fallingx[i][z] = rand() % 1800 + 300;
							fallingtypes[i][z] = rand() % 4 + CHAROFFSET;
						}
						if(fallingx[i][z] > 9 && fallingtypes[i][z] != 0) {
							if(blastingx[i] + 8 == fallingx[i][z] || blastingx[i] + 8 + 1 == fallingx[i][z]) {
								if(blastingtypes[i] == fallingtypes[i][z]) {
									score += 1;
									if(score % 25 == 0) {
										lives += 1;
									}
									if(score % 12 == 0) {
										level += 1;
									}
									blastingtypes[i] = 0;
									blastingx[i] = 0;
									if(level < 10) {
										fallingx[i][z] = rand() % (1500 - level * 100) + 200;
									} else {
										fallingx[i][z] = rand() % 600 + 100;
									}
									fallingtypes[i][z] = rand() % 4 + CHAROFFSET;
								} else {
									blastingx[i] = 0;
									blastingtypes[i] = 0;
								}
							} else {
//								arduboy.setCursor(fallingx[i][z], i * 10 + 12);
//								sprintf(text, "%c", fallingtypes[i][z]);
//								arduboy.print(text);
								arduboy.drawBitmap(fallingx[i][z], i * 10 + 15, shapes[fallingtypes[i][z] - 3], 9, 16, WHITE);
							}
						}
					}
				}
			}
			
//			arduboy.setCursor(2, y * 10 + 12);
//			sprintf(text, "%c", selectedbrick);
//			arduboy.print(text);
			arduboy.drawBitmap(1, y * 10 + 15, shapes[selectedbrick - 3], 9, 16, WHITE);
			arduboy.drawLine(12, 14, 12, 64, WHITE);
			arduboy.drawLine(12, 14, 128, 14, WHITE);

			arduboy.drawBitmap(37, 3, left, 3, 8, WHITE);
			arduboy.drawBitmap(83, 3, right, 3, 8, WHITE);
			for(char highlight = 0; highlight < 4; highlight++) {
				if(selectedbrick - 3 == highlight) {
					arduboy.drawBitmap(42 + 10 * highlight, 2, shapes[highlight], 9, 16, WHITE);
				} else {
					arduboy.drawBitmap(42 + 10 * highlight, 2, smallshapes[highlight], 9, 16, WHITE);
				}
			}

			for(unsigned char row = 24; row < 60; row += 10) {
				for(unsigned char dot = 12; dot < 128; dot++) {
					if(dot % 2 == 0) {
						arduboy.drawPixel(dot, row, WHITE);
					}
				}
			}
			
			arduboy.setCursor(13, 3);
			sprintf(text, "%d", score);
			arduboy.print(text);

			
			arduboy.setCursor(102, 0);
			arduboy.print("");
			if(lives < 0) {
				gamestate = 3;
				timer = 200;
				if(highscore < score) {
					highscore = score;
					EEPROM.put(SAVELOCATION, highscore);
				}
			} else {
				arduboy.drawBitmap(94, 3, gauge, 32, 8, WHITE);
				for(i = 0; i < lives; i++) {
					arduboy.fillRect(95 + i * 6, 4, 5, 5, WHITE);
				}
			}
			}
			
  			break;
  		case 3:	//Game Over
			arduboy.setCursor(41, 9);
			arduboy.print(F("GAME OVER"));
			if(highscore == score && score != 0) {
				arduboy.setCursor(4, 30);
				sprintf(text, "NEW HIGH SCORE: %d!", highscore);
				arduboy.print(text);
			} else {
				arduboy.setCursor(41, 19);
				sprintf(text, "SCORE: %d", score);
				arduboy.print(text);
				arduboy.setCursor(22, 34);
				sprintf(text, "HIGH SCORE: %d", highscore);
				arduboy.print(text);
			}

			if(timer > 0) {
				timer -= 1;
			} else {
				arduboy.drawBitmap(110, 51, abutton, 16, 16, WHITE);
			}
			if(arduboy.pressed(A_BUTTON) && shootfree && timer == 0) {
				gamestate = 2;
				resetgame();
				buttonfree = false;
			}
			
  			break;
	}
	if(buttonfree == false && arduboy.notPressed(B_BUTTON) && arduboy.notPressed(A_BUTTON) && arduboy.notPressed(LEFT_BUTTON) && arduboy.notPressed(RIGHT_BUTTON) && arduboy.notPressed(UP_BUTTON) && arduboy.notPressed(DOWN_BUTTON)) {
		buttonfree = true;
	}
	
	if(shootfree == false && !arduboy.pressed(A_BUTTON)) {
		shootfree = true;
	}
	
	arduboy.display();
}

