Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PGMWrap.h>
- #include <Arduboy2.h>
- // http://community.arduboy.com/
- // https://emutyworks.github.io/BitmapEditor/demo/index.html
- // http://community.arduboy.com/t/reference-page-for-development/76/2
- // http://www.andrewlowndes.co.uk/blog/graphics/arduboy-image-converter
- // http://www.crait.net/tochars/
- // https://github.com/yyyc514/img2ard
- // https://github.com/andremichelle/platforms
- // http://fuopy.github.io/arduboy-image-converter/ !!!
- // arduboy.pressed(RIGHT_BUTTON)
- // http://playground.arduino.cc/Main/PROGMEM
- const unsigned char PlayFieldTiles[] PROGMEM = {0xf0, 0x0c, 0x02, 0xe2, 0x11, 0x09, 0x09, 0x09,
- 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
- 0x09, 0x09, 0x09, 0x11, 0xe1, 0x01, 0x01, 0x01,
- 0x01, 0x01, 0x01, 0xe1, 0x11, 0x09, 0x09, 0x09,
- 0x09, 0x09, 0x09, 0x11, 0xe2, 0x02, 0x0c, 0xf0,
- 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff,
- 0x00, 0x00, 0x00, 0x00, 0xc0, 0x20, 0x10, 0x10,
- 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
- 0x10, 0x10, 0x20, 0xc0, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x08, 0x08,
- 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
- 0x08, 0x08, 0x04, 0x03, 0x00, 0x00, 0x00, 0x00,
- 0x08, 0x08, 0x08, 0x10, 0xe0, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0xe0, 0x10, 0x08, 0x08, 0x08,
- 0x0f, 0x30, 0x40, 0x47, 0x88, 0x90, 0x90, 0x90,
- 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
- 0x00, 0x00, 0x00, 0x07, 0x08, 0x10, 0x10, 0x10,
- 0x10, 0x10, 0x10, 0x08, 0x07, 0x00, 0x00, 0x00,
- 0x90, 0x90, 0x90, 0x88, 0x47, 0x40, 0x30, 0x0f,
- 0x00, 0x00, 0x00, 0x00, 0xf0, 0x10, 0x10, 0x90,
- 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0xf0,
- 0x40, 0x20, 0x40, 0x20, 0x40, 0x20, 0x40, 0x20,
- 0xf0, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90,
- 0x90, 0x10, 0x10, 0xf0, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x0f, 0x08, 0x08, 0x09,
- 0x09, 0x08, 0x08, 0x0f, 0x00, 0x00, 0x00, 0x00,
- 0xff, 0x00, 0x00, 0x07, 0x08, 0x10, 0x10, 0x10,
- 0x10, 0x10, 0x10, 0x08, 0x07, 0x00, 0x00, 0xff,
- 0xff, 0x00, 0x00, 0xe0, 0x10, 0x08, 0x08, 0x08,
- 0x08, 0x08, 0x08, 0x10, 0xe0, 0x00, 0x00, 0xff,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- unsigned char PlayFieldMap[] = {0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x04, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x05, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x07, 0x08, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x09, 0x05, 0x86, 0x07, 0x06, 0x06, 0x08, 0x46, 0x07, 0x06, 0x06, 0x06, 0x08, 0x46, 0x07, 0x08, 0x46, 0x07, 0x06, 0x06, 0x06, 0x08, 0x46, 0x07, 0x06, 0x06, 0x08, 0x86, 0x09, 0x05, 0x46, 0x0D, 0x0E, 0x0E, 0x0F, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x0D, 0x0F, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x0D, 0x0E, 0x0E, 0x0F, 0x46, 0x09, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x05, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x09, 0x05, 0x46, 0x0D, 0x0E, 0x0E, 0x0F, 0x46, 0x07, 0x08, 0x46, 0x0D, 0x0E, 0x0E, 0x10, 0x11, 0x0E, 0x0E, 0x0F, 0x46, 0x07, 0x08, 0x46, 0x0D, 0x0E, 0x0E, 0x0F, 0x46, 0x09, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x12, 0x13, 0x13, 0x13, 0x13, 0x0C, 0x46, 0x07, 0x14, 0x0B, 0x0B, 0x0C, 0x06, 0x07, 0x08, 0x06, 0x0A, 0x0B, 0x0B, 0x15, 0x08, 0x46, 0x0A, 0x13, 0x13, 0x13, 0x13, 0x16, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x11, 0x0E, 0x0E, 0x0F, 0x06, 0x0D, 0x0F, 0x06, 0x0D, 0x0E, 0x0E, 0x10, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x08, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x08, 0x06, 0x17, 0x13, 0x18, 0x19, 0x19, 0x1A, 0x13, 0x1B, 0x06, 0x07, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x01, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x46, 0x0D, 0x0F, 0x06, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x06, 0x0D, 0x0F, 0x46, 0x0D, 0x01, 0x01, 0x01, 0x01, 0x01, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x46, 0x06, 0x06, 0x06, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x06, 0x06, 0x06, 0x46, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x13, 0x13, 0x13, 0x13, 0x13, 0x0C, 0x46, 0x0A, 0x0C, 0x06, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x06, 0x0A, 0x0C, 0x46, 0x0A, 0x13, 0x13, 0x13, 0x13, 0x13, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x08, 0x06, 0x1C, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x1D, 0x06, 0x07, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x08, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x07, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x05, 0x46, 0x07, 0x08, 0x06, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0C, 0x06, 0x07, 0x08, 0x46, 0x09, 0x06, 0x06, 0x06, 0x06, 0x06, 0x00, 0x01, 0x01, 0x01, 0x01, 0x0F, 0x46, 0x0D, 0x0F, 0x06, 0x0D, 0x0E, 0x0E, 0x10, 0x11, 0x0E, 0x0E, 0x0F, 0x06, 0x0D, 0x0F, 0x46, 0x0D, 0x01, 0x01, 0x01, 0x01, 0x04, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x05, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x07, 0x08, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0C, 0x46, 0x09, 0x05, 0x46, 0x0D, 0x0E, 0x10, 0x08, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x0D, 0x0F, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x07, 0x11, 0x0E, 0x0F, 0x46, 0x09, 0x05, 0x86, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x06, 0x06, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x86, 0x09, 0x1E, 0x0B, 0x0C, 0x46, 0x07, 0x08, 0x46, 0x0A, 0x0C, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x0A, 0x0C, 0x46, 0x07, 0x08, 0x46, 0x0A, 0x0B, 0x1F, 0x20, 0x0E, 0x0F, 0x46, 0x0D, 0x0F, 0x46, 0x07, 0x08, 0x46, 0x0D, 0x0E, 0x0E, 0x10, 0x11, 0x0E, 0x0E, 0x0F, 0x46, 0x07, 0x08, 0x46, 0x0D, 0x0F, 0x46, 0x0D, 0x0E, 0x21, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x07, 0x08, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x05, 0x46, 0x0A, 0x0B, 0x0B, 0x0B, 0x0B, 0x15, 0x14, 0x0B, 0x0B, 0x0C, 0x46, 0x07, 0x08, 0x46, 0x0A, 0x0B, 0x0B, 0x15, 0x14, 0x0B, 0x0B, 0x0B, 0x0B, 0x0C, 0x46, 0x09, 0x05, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x0D, 0x0F, 0x46, 0x0D, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x46, 0x09, 0x05, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x46, 0x09, 0x12, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x16};
- const unsigned char TILE_FREE = 6;
- const unsigned char TILE_SIZE = 8;
- const unsigned int MAP_COLUMNS = 28;
- const unsigned char IDLE = 0;
- const unsigned char UP = 1;
- const unsigned char LEFT = 2;
- const unsigned char RIGHT = 3;
- const unsigned char DOWN = 4;
- Arduboy2 arduboy;
- int heroX = 14 * TILE_SIZE - TILE_SIZE / 2;
- int heroY = 17 * TILE_SIZE;
- unsigned int pellets = 244;
- unsigned int heroSpeed = 2;
- unsigned char heroDirection = RIGHT;
- unsigned char desiredDirection = IDLE;
- boolean debug = true;
- int scrollX = 0;
- int scrollY = 0;
- void setup() {
- arduboy.boot();
- arduboy.flashlight();
- arduboy.setFrameRate(30);
- arduboy.audio.begin();
- }
- void loop() {
- if (!(arduboy.nextFrame())) {
- return;
- }
- // Game Logic
- processButtons();
- moveHero();
- positionPlayField();
- // Display Logic
- arduboy.clear();
- drawPlayField();
- drawHero();
- if ( debug = !debug ) {
- arduboy.fillRect( 1, 1, 1, 1, 1 );
- }
- arduboy.display();
- }
- void processButtons() {
- if ( arduboy.pressed(RIGHT_BUTTON) ) {
- requestDirection( RIGHT, LEFT );
- }
- if ( arduboy.pressed(LEFT_BUTTON) ) {
- requestDirection( LEFT, RIGHT );
- }
- if ( arduboy.pressed(DOWN_BUTTON) ) {
- requestDirection( DOWN, UP );
- }
- if ( arduboy.pressed(UP_BUTTON) ) {
- requestDirection( UP, DOWN );
- }
- }
- void requestDirection( const unsigned char direction, const unsigned char counterpart ) {
- if ( heroDirection == counterpart ) {
- heroDirection == direction;
- desiredDirection = direction;
- } else {
- desiredDirection = direction;
- }
- }
- void moveHero() {
- int remaining = heroSpeed;
- while (0 < remaining ) {
- int modX = heroX % TILE_SIZE;
- int modY = heroY % TILE_SIZE;
- if (0 == modX && 0 == modY ) {
- const int tileX = heroX / TILE_SIZE;
- const int tileY = heroY / TILE_SIZE;
- const unsigned char food = evalFood(tileX, tileY);
- if ( 0 < food ) {
- beep();
- if ( munchFood(tileX, tileY) ) {
- heroDirection = IDLE;
- desiredDirection = IDLE;
- }
- }
- checkHeroDirection(tileX, tileY);
- switch ( heroDirection ) {
- case IDLE: {
- return;
- }
- case UP: {
- if ( TILE_FREE == evalMap( tileX, tileY - 1 ) ) {
- heroY -= remaining;
- } else {
- heroDirection = IDLE;
- desiredDirection = IDLE;
- }
- return;
- }
- case LEFT: {
- if ( -1 == tileX ) {
- heroX = MAP_COLUMNS * TILE_SIZE;
- onTeleport();
- continue;
- }
- if ( TILE_FREE == evalMap( tileX - 1, tileY ) ) {
- heroX -= remaining;
- } else {
- heroDirection = IDLE;
- desiredDirection = IDLE;
- }
- return;
- }
- case RIGHT: {
- if ( MAP_COLUMNS == tileX ) {
- heroX = -TILE_SIZE;
- onTeleport();
- continue;
- }
- if ( TILE_FREE == evalMap( tileX + 1, tileY ) ) {
- heroX += remaining;
- } else {
- heroDirection = IDLE;
- desiredDirection = IDLE;
- }
- return;
- }
- case DOWN: {
- if ( TILE_FREE == evalMap( tileX, tileY + 1 ) ) {
- heroY += remaining;
- } else {
- heroDirection = IDLE;
- desiredDirection = IDLE;
- }
- return;
- }
- }
- }
- switch ( heroDirection ) {
- case UP: {
- if ( remaining < modY ) {
- heroY -= remaining;
- return;
- } else {
- heroY -= modY;
- remaining -= modY;
- }
- break;
- }
- case LEFT: {
- if ( remaining < modX ) {
- heroX -= remaining;
- return;
- } else {
- heroX -= modX;
- remaining -= modX;
- }
- break;
- }
- case RIGHT: {
- modX = TILE_SIZE - modX;
- if ( remaining < modX ) {
- heroX += remaining;
- return;
- } else {
- heroX += modX;
- remaining -= modX;
- }
- break;
- }
- case DOWN: {
- modY = TILE_SIZE - modY;
- if ( remaining < modY ) {
- heroY += remaining;
- return;
- } else {
- heroY += modY;
- remaining -= modY;
- }
- break;
- }
- }
- }
- }
- void checkHeroDirection( const int tileX, const int tileY ) {
- switch ( desiredDirection ) {
- case UP: {
- if ( TILE_FREE == evalMap( tileX, tileY - 1 ) ) {
- heroDirection = UP;
- desiredDirection = IDLE;
- }
- return;
- }
- case LEFT: {
- if ( TILE_FREE == evalMap( tileX - 1, tileY ) ) {
- heroDirection = LEFT;
- desiredDirection = IDLE;
- }
- return;
- }
- case RIGHT: {
- if ( TILE_FREE == evalMap( tileX + 1, tileY ) ) {
- heroDirection = RIGHT;
- desiredDirection = IDLE;
- }
- return;
- }
- case DOWN: {
- if ( TILE_FREE == evalMap( tileX, tileY + 1 ) ) {
- heroDirection = DOWN;
- desiredDirection = IDLE;
- }
- return;
- }
- }
- }
- void positionPlayField() {
- scrollX = heroX - 64;
- scrollY = heroY - 32;
- if ( scrollX < 0 ) {
- scrollX = 0;
- } else if ( scrollX > 96 ) {
- scrollX = 96;
- }
- if ( scrollY < 0 ) {
- scrollY = 0;
- } else if ( scrollY > 184 ) {
- scrollY = 184;
- }
- }
- void drawPlayField() {
- const int tileX = scrollX / TILE_SIZE;
- const int tileY = scrollY / TILE_SIZE;
- const int modX = scrollX % TILE_SIZE;
- const int modY = scrollY % TILE_SIZE;
- for (int y = 0 ; y <= 8 ; y++ ) {
- for (int x = 0 ; x <= 16 ; x++ ) {
- const unsigned char mapX = x + tileX;
- const unsigned char mapY = y + tileY;
- const unsigned char tile = evalMap(mapX, mapY);
- const unsigned int bitmapX = x * TILE_SIZE - modX;
- const unsigned int bitmapY = y * TILE_SIZE - modY;
- arduboy.drawBitmap(bitmapX, bitmapY, PlayFieldTiles + (tile << 3), TILE_SIZE, TILE_SIZE, 1);
- const unsigned char food = evalFood(mapX, mapY);
- if ( 0 < food ) {
- arduboy.fillCircle( bitmapX + 4, bitmapY + 4, 1, 1 );
- }
- }
- }
- }
- void drawHero() {
- arduboy.drawRect(heroX - scrollX, heroY - scrollY, TILE_SIZE + 1, TILE_SIZE + 1, 1);
- }
- unsigned char evalMap( const int x, const int y ) {
- // Allow to leave field
- if ( 0 > x || x >= MAP_COLUMNS ) {
- return TILE_FREE;
- }
- const unsigned int tile = y * MAP_COLUMNS + x;
- return PlayFieldMap[tile] & 0x3F;
- }
- unsigned char evalFood( const int x, const int y ) {
- const unsigned int tile = y * MAP_COLUMNS + x;
- return PlayFieldMap[tile] >> 5;
- }
- bool munchFood( const int x, const int y ) {
- const unsigned int tile = y * MAP_COLUMNS + x;
- const unsigned char data = PlayFieldMap[tile] & 0x3F;
- PlayFieldMap[tile] = data;
- return 0 == --pellets;
- }
- void onTeleport() {
- tone(PIN_SPEAKER_2, 110, 200);
- }
- void beep() {
- tone(PIN_SPEAKER_1, 220, 20);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement