Created
February 6, 2016 13:17
-
-
Save flaki/8d71b3efe63ae859d9f2 to your computer and use it in GitHub Desktop.
Happy Birthday Manel! A celebratory arduboy pocket game for the occasion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include "Arduboy.h" | |
#include <EEPROM.h> | |
#include <avr/pgmspace.h> | |
Arduboy arduboy; | |
// frame counter | |
unsigned int frame = 0; | |
// general use string buffer | |
char text[16]; | |
PROGMEM const unsigned char cactus_1[] = | |
// w: 12 h: 24 | |
{0, 0, 0, 240, 12, 226, 30, 36, 164, 88, 136, 240, 0, 0, 0, 231, 56, 23, 17, 17, 10, 12, 4, 3, 0, 128, 254, 131, 0, 0, 0, 0, 0, 0, 0, 0}; | |
// cloud_1 w: 17 h: 7 | |
PROGMEM const unsigned char cloud_1[] = | |
{28, 62, 126, 126, 252, 126, 126, 62, 28, 0, 32, 96, 40, 28, 56, 28, 8}; | |
// w: 30 h: 18 | |
PROGMEM const unsigned char dino_tumble[] = | |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 60, 254, 112, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 32, 80, 144, 168, 8, 176, 208, 175, 145, 209, 223, 160, 208, 144, 208, 40, 232, 48, 0, 0, 0, 0, 0, 0, 0, 0, 32, 96, 112, 112, 64, 223, 223, 223, 159, 191, 191, 191, 191, 191, 191, 191, 191, 159, 223, 223, 223, 64, 112, 112, 96, 32, 0, 0}; | |
// top w: 20 h: 18 | |
PROGMEM const unsigned char dino_top[] = | |
{0, 120, 134, 2, 49, 121, 233, 209, 98, 4, 216, 226, 217, 165, 101, 126, 58, 1, 3, 0, 0, 0, 28, 35, 74, 90, 66, 34, 144, 236, 251, 247, 111, 7, 29, 1, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0}; | |
// leg_0 w: 20 h: 5 | |
PROGMEM const unsigned char dino_leg_0[] = | |
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
// leg_1 w: 20 h: 5 | |
PROGMEM const unsigned char dino_leg_1[] = | |
{0, 0, 0, 0, 0, 64, 64, 64, 64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; | |
// leg_2 w: 20 h: 5 | |
PROGMEM const unsigned char dino_leg_2[] = | |
{0, 0, 0, 0, 64, 64, 64, 64, 64, 64, 64, 64, 64, 0, 0, 0, 0, 0, 0, 0}; | |
// Polyphonic music data to be played with Arduboy's audio.cpp::playScore lib | |
const byte PROGMEM score [] = { | |
3,0, | |
0x90, 48, 1,0, 0x80, 0,32, | |
0x90, 48, 0,128, 0x80, 0,32, | |
0x90, 50, 1,128, 0x80, 0,32, | |
0x90, 48, 1,128, 0x80, 0,32, | |
0x90, 53, 1,128, 0x80, 0,32, | |
0x90, 52, 3,0, 0x80, 0,32, | |
0,128, | |
0x90, 48, 1,0, 0x80, 0,32, | |
0x90, 48, 0,128, 0x80, 0,32, | |
0x90, 50, 1,128, 0x80, 0,32, | |
0x90, 48, 1,128, 0x80, 0,32, | |
0x90, 55, 1,128, 0x80, 0,32, | |
0x90, 53, 3,0, 0x80, 0,32, | |
1,128, | |
0xf0 | |
}; | |
void intro() | |
{ | |
if (!arduboy.tunes.playing()) | |
arduboy.tunes.playScore(score); | |
for(int i = 0; i < 36; i = i + 2) | |
{ | |
arduboy.clearDisplay(); | |
arduboy.setCursor(20, i - 10); | |
arduboy.print("HAPPY BIRTHDAY"); | |
arduboy.display(); | |
delay(16); | |
} | |
arduboy.setCursor(14, 42); | |
arduboy.print("Manel Butterfly!"); | |
arduboy.display(); | |
delay(5000); | |
} | |
// distance ran | |
int d, delta; | |
int cloud_1_y; | |
int d_jump, d_jump_t; | |
int d_run; | |
int d_tumble_t; | |
int ox; | |
void setup() | |
{ | |
arduboy.start(); | |
arduboy.setFrameRate(60); | |
intro(); | |
d = 0; | |
delta = 0; | |
cloud_1_y = 2; | |
d_jump = 0; | |
d_jump_t = 0; | |
d_tumble_t = 0; | |
d_run = 0; | |
ox = 130; | |
} | |
void loop() | |
{ | |
if (!d_run && arduboy.pressed(A_BUTTON)) { | |
d_run = 1; | |
} | |
if (d_tumble_t && arduboy.pressed(A_BUTTON)) { | |
setup(); | |
return; | |
} | |
++frame; | |
if (frame>16000) frame = 0; | |
// increase distance whilst running | |
if (d_run && (++delta > 4)) { | |
delta = 0; ++d; | |
} | |
// obstacles | |
if (d_run) { | |
ox -= (frame%2)*(d/100) + 2; | |
if (ox < -15) ox += 140 + random(0,60); | |
} | |
// jump! | |
if (!d_jump_t && arduboy.pressed(A_BUTTON)) { | |
d_jump_t = 1; | |
d_jump=5; | |
arduboy.tunes.tone(440, 40); | |
} else if (d_jump_t) { | |
if (d_jump_t == 3) arduboy.tunes.tone(880, 80); | |
++d_jump_t; | |
if (d_jump_t<6) { | |
d_jump +=6; | |
} else if (d_jump_t<9) { | |
d_jump +=2; | |
} else if (d_jump_t<13) { | |
d_jump +=1; | |
} else if (d_jump_t == 16 || d_jump_t == 18) { | |
d_jump +=1; | |
} else if (d_jump_t == 20 || d_jump_t == 22) { | |
d_jump -=1; | |
} else if (d_jump_t>38) { | |
d_jump = 0; | |
d_jump_t = 0; | |
} else if (d_jump_t>32) { | |
d_jump -=6; | |
} else if (d_jump_t>29) { | |
d_jump -=2; | |
} else if (d_jump_t>25) { | |
d_jump -=1; | |
} | |
} | |
// hit detect | |
if (!d_tumble_t && ox > -10 && ox <16 && d_jump_t < 14) { | |
d_tumble_t = 1; | |
} | |
if (d_tumble_t) { | |
if (d_tumble_t == 1) { | |
arduboy.tunes.tone(246, 80); | |
} else if (d_tumble_t == 6) { | |
arduboy.tunes.tone(174, 200); | |
} | |
++d_tumble_t; | |
if (d_jump > -4) { | |
d_jump -= 1; | |
ox -= 1; | |
} else { | |
d_run = 0; | |
} | |
} | |
arduboy.clearDisplay(); | |
// hud | |
arduboy.setCursor(100, 0); | |
sprintf(text,"%d",d); | |
arduboy.print(text); | |
// parallax clouds | |
arduboy.drawBitmap(128 -(d%128),cloud_1_y,cloud_1,17,7,WHITE); | |
if (d%128 == 0) { | |
cloud_1_y = random(0,10); | |
} | |
// terrain | |
if (d_jump > 4) { | |
arduboy.drawLine(0,60,127,60,WHITE); | |
} else { | |
arduboy.drawLine(0,60,3,60,WHITE); | |
arduboy.drawLine(12,60,127,60,WHITE); | |
} | |
// obstacles | |
arduboy.drawBitmap(ox,40,cactus_1,12,24,WHITE); | |
// dino | |
int dy = 40-d_jump; | |
// tumbles! | |
if (d_tumble_t) { | |
arduboy.drawBitmap(0,dy,dino_tumble,30,18,WHITE); | |
// runs! | |
} else { | |
arduboy.drawBitmap(0,dy,dino_top,20,18,WHITE); | |
// Run, Dino, Run! | |
if (d_run && !d_jump) { | |
if ((frame%8)/4) { | |
arduboy.drawBitmap(0,dy+18,dino_leg_1,20,5,WHITE); | |
} else { | |
arduboy.drawBitmap(0,dy+18,dino_leg_2,20,5,WHITE); | |
} | |
} else { | |
arduboy.drawBitmap(0,dy+18,dino_leg_0,20,5,WHITE); | |
} | |
} | |
arduboy.display(); | |
delay(16); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment