Skip to content

Instantly share code, notes, and snippets.

@flaki
Created November 1, 2015 01:33
Show Gist options
  • Save flaki/0280c9c1f3384d7644c4 to your computer and use it in GitHub Desktop.
Save flaki/0280c9c1f3384d7644c4 to your computer and use it in GitHub Desktop.
Spooky version of the Arduboy rund.ino game (https://github.com/flaki/arduino-rund-ino)
#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];
// w: 12 h: 24
PROGMEM const unsigned char cactus_1[] = { 0xe0, 0xfc, 0x1e, 0xef, 0x0f, 0xff, 0x07, 0xff, 0x03, 0xba, 0xc4, 0xf8, 0xff, 0xff, 0xe0, 0xfe, 0xf1, 0xff, 0xf0, 0xff, 0xf8, 0xff, 0xff, 0xff, 0x5f, 0x5f, 0x1f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x0f, 0x0f, 0x0f };
//{ 0x00, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xfe, 0x00, 0xc0, 0xc0, 0x80, 0xfe, 0xff, 0xfe, 0x00, 0xff, 0xff, 0xff, 0xff, 0xc0, 0xff, 0xff, 0x7f, 0x01, 0x03, 0x03, 0x83, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x00, 0x00 };
// w: 17 h: 7
PROGMEM const unsigned char cloud_1[] = { 0x1c, 0x0e, 0x0c, 0x07, 0x0e, 0x0c, 0x1b, 0x3e, 0x3e, 0x1b, 0x0c, 0x06, 0x0f, 0x1c, 0x0e, 0x1c, 0x38 };
//{ 0x1c, 0x22, 0x22, 0x22, 0x24, 0x10, 0x12, 0x2a, 0x21, 0x41, 0x41, 0x41, 0x42, 0x4a, 0x24, 0x24, 0x18 };
// w: 30 h: 18
PROGMEM const unsigned char dino_tumble[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 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, 0x80, 0x00, 0x00, 0xe0, 0xb0, 0xf0, 0xf0, 0xf0, 0xb0, 0xe0, 0x00, 0x00, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x02, 0x02, 0x00 };
//{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0xf8, 0xf0, 0xe0, 0xe0, 0xf0, 0xf0, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0xf0, 0xf0, 0xe0, 0xe0, 0xc0, 0xc0, 0x80, 0xc0, 0xf0, 0xa8, 0xd8, 0xa8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf0, 0x00, 0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01 };
// w: 20 h: 20
PROGMEM const unsigned char dino_top[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xfe, 0xff, 0x37, 0x37, 0x3f, 0x3f, 0x37, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x0f, 0x9f, 0xfe, 0xfc, 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfe, 0xc6, 0xdd, 0xfe, 0x1e, 0x00, 0x00, 0x08, 0x0e, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x0f, 0x03, 0x01, 0x00, 0x00 };
//PROGMEM const unsigned char dino_top[] = { 0x00, 0x39, 0xdb, 0xef, 0x6e, 0xae, 0xfc, 0xf8, 0x00, 0x54, 0xff, 0xfe, 0xfb, 0xfe, 0xff, 0x3c, 0x3e, 0xbc, 0x3e, 0xbc, 0x7c, 0xf2, 0xf8, 0xe1, 0xe8, 0xf1, 0xf4, 0xf9, 0xfb, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x09, 0x19, 0x00, 0x01, 0x0b, 0x16, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// leg_0 w: 20 h: 5
PROGMEM const unsigned char dino_leg_0[] =
{ 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, 0x1f, 0x17, 0x03, 0x01, 0x03, 0x1f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// leg_1 w: 20 h: 5
PROGMEM const unsigned char dino_leg_1[] =
{ 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, 0x0f, 0x0b, 0x01, 0x01, 0x03, 0x1f, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// leg_2 w: 20 h: 5
PROGMEM const unsigned char dino_leg_2[] =
{ 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, 0x1f, 0x17, 0x03, 0x01, 0x03, 0x0f, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
void intro()
{
for(int i = -8; i < 28; i = i + 2)
{
arduboy.clearDisplay();
arduboy.setCursor(42, i);
arduboy.print("HALLOWEEN");
arduboy.display();
delay(16);
}
arduboy.tunes.tone(987, 120);
delay(120);
arduboy.tunes.tone(1318, 400);
arduboy.setCursor(52, 8);
arduboy.print("spooky");
arduboy.display();
delay(2000);
arduboy.tunes.tone(1318, 1);
}
// 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;
Serial.begin(9600);
}
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;
Serial.print("Game Over! Your score: ");
Serial.println(d);
} 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