Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "U8glib.h"
- #include "pitches.h"
- int melody[] = {
- NOTE_G4, NOTE_C5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_F5, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_G5
- };
- int noteDurations[] = {
- 4, 2, 4, 2, 4, 4, 4, 4, 4, 2
- };
- U8GLIB_SH1106_128X64 u8g(12, 11, 10, 8, 9); // D0=12, D1=11, CS=10, DC=9, Reset=8
- const unsigned char horse[3][92] PROGMEM =
- {
- {
- 0x00,0x00,0x07,0x80, // .....................####.......
- 0x00,0x10,0x0C,0xC0, // ...........#........##..##......
- 0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
- 0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
- 0x00,0x3C,0x63,0x80, // ..........####...##...###.......
- 0x00,0x3C,0x40,0x00, // ..........####...#..............
- 0x00,0x3C,0xC0,0x00, // ..........####..##..............
- 0x00,0x19,0x80,0x00, // ...........##..##...............
- 0x00,0x1B,0x50,0x00, // ...........##.##.#.#............
- 0x00,0x7E,0x50,0x00, // .........######..#.#............
- 0x00,0x58,0x78,0x00, // .........#.##....####...........
- 0x00,0x18,0x6E,0x00, // ...........##....##.###.........
- 0x00,0x18,0x7E,0x00, // ...........##....######.........
- 0x80,0x3C,0xE0,0x00, // #.........####..###.............
- 0xF8,0xEF,0xE0,0x00, // #####...###.#######.............
- 0x7F,0xF7,0xE0,0x00, // .###########.######.............
- 0x0D,0xEF,0xE0,0x00, // ....##.####.#######.............
- 0x01,0xF7,0xE0,0x00, // .......#####.######.............
- 0x01,0xEF,0xE0,0x00, // .......####.#######.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x02,0x81,0xA0,0x00, // ......#.#......##.#.............
- 0x02,0x81,0x20,0x00 // ......#.#......#..#.............
- },
- {
- 0x00,0x00,0x07,0x80, // .....................####.......
- 0x00,0x10,0x0C,0xC0, // ...........#........##..##......
- 0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
- 0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
- 0x00,0x3C,0x63,0x80, // ..........####...##...###.......
- 0x00,0x3C,0x40,0x00, // ..........####...#..............
- 0x00,0x3C,0xC0,0x00, // ..........####..##..............
- 0x00,0x19,0x80,0x00, // ...........##..##...............
- 0x00,0x1B,0xA0,0x00, // ...........##.###.#.............
- 0x00,0x7E,0xA0,0x00, // .........######.#.#.............
- 0x00,0x58,0xF0,0x00, // .........#.##...####............
- 0x00,0x18,0xDC,0x00, // ...........##...##.###..........
- 0x00,0x18,0xFC,0x00, // ...........##...######..........
- 0x70,0x3C,0xE0,0x00, // .###......####..###.............
- 0xF8,0xEF,0xE0,0x00, // #####...###.#######.............
- 0x9F,0xF7,0xE0,0x00, // #..#########.######.............
- 0x0D,0xEF,0xE0,0x00, // ....##.####.#######.............
- 0x01,0xF7,0xC0,0x00, // .......#####.#####..............
- 0x01,0xEF,0xE0,0x00, // .......####.#######.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x01,0x40,0x80,0x00 // .......#.#......#...............
- },
- {
- 0x00,0x00,0x07,0x80, // .....................####.......
- 0x00,0x10,0x0C,0xC0, // ...........#........##..##......
- 0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
- 0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
- 0x00,0x3C,0x63,0x80, // ..........####...##...###.......
- 0x00,0x3C,0x40,0x00, // ..........####...#..............
- 0x00,0x3C,0xC0,0x00, // ..........####..##..............
- 0x00,0x19,0x80,0x00, // ...........##..##...............
- 0x00,0x1B,0xA0,0x00, // ...........##.###.#.............
- 0x00,0x7E,0xA0,0x00, // .........######.#.#.............
- 0x00,0x58,0xF0,0x00, // .........#.##...####............
- 0x00,0x18,0xDC,0x00, // ...........##...##.###..........
- 0x00,0x18,0xFC,0x00, // ...........##...######..........
- 0x00,0x3C,0xE0,0x00, // ..........####..###.............
- 0x80,0xEF,0xE0,0x00, // #.......###.#######.............
- 0xFF,0xF7,0xE0,0x00, // ############.######.............
- 0x3D,0xEF,0xE0,0x00, // ..####.####.#######.............
- 0x01,0xF7,0xE0,0x00, // .......#####.######.............
- 0x01,0xEF,0xE0,0x00, // .......####.#######.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
- 0x00,0xA0,0x90,0x00, // ........#.#.....#..#............
- 0x00,0xA0,0x50,0x00 // ........#.#......#.#............
- }
- };
- const unsigned char house[3][52] PROGMEM = {
- {
- 0x00,0x07,0x00,0x00, // .............###................
- 0x00,0x0A,0x80,0x00, // ............#.#.#...............
- 0x00,0x17,0x40,0x00, // ...........#.###.#..............
- 0x00,0x2F,0xA0,0x00, // ..........#.#####.#.............
- 0x00,0x5F,0xD0,0x00, // .........#.#######.#............
- 0x00,0xB0,0x68,0x00, // ........#.##.....##.#...........
- 0x01,0x75,0x74,0x00, // .......#.###.#.#.###.#..........
- 0x02,0xF5,0x7A,0x00, // ......#.####.#.#.####.#.........
- 0x05,0xF0,0x7D,0x00, // .....#.#####.....#####.#........
- 0x0B,0xF5,0x7E,0x80, // ....#.######.#.#.######.#.......
- 0x17,0xF5,0x7F,0x40, // ...#.#######.#.#.#######.#......
- 0x2F,0xF0,0x7F,0xA0, // ..#.########.....########.#.....
- 0xDF,0xFF,0xFF,0xD8 // ##.#######################.##...
- },
- {
- 0x00,0x00,0x00,0x00, // ................................
- 0x00,0x08,0x80,0x00, // ............#...#...............
- 0x00,0x1F,0xC0,0x00, // ...........#######..............
- 0x00,0x3F,0xE0,0x00, // ..........#########.............
- 0x00,0x7F,0xF0,0x00, // .........###########............
- 0x00,0xFF,0xF8,0x00, // ........#############...........
- 0x01,0xFF,0xFC,0x00, // .......###############..........
- 0x03,0xFA,0xFE,0x00, // ......#######.#.#######.........
- 0x07,0xF2,0x7F,0x00, // .....#######..#..#######........
- 0x0F,0xFF,0xFF,0x80, // ....#####################.......
- 0x1F,0xE2,0x3F,0xC0, // ...########...#...########......
- 0x3F,0xC2,0x1F,0xE0, // ..########....#....########.....
- 0xFF,0xFF,0xFF,0xF8 // #############################...
- },
- {
- 0x00,0x00,0x00,0x00, // ................................
- 0x00,0x05,0x00,0x00, // .............#.#................
- 0x00,0x1F,0xC0,0x00, // ...........#######..............
- 0x00,0x1F,0xC0,0x00, // ...........#######..............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00, // ............#####...............
- 0x00,0x0F,0x80,0x00 // ............#####...............
- }
- };
- const unsigned char moon[] PROGMEM = {
- 0x07,0xC0, // .....#####......
- 0x1F,0xC0, // ...#######......
- 0x3F,0x80, // ..#######.......
- 0x7F,0x00, // .#######........
- 0x7F,0x00, // .#######........
- 0xFE,0x00, // #######.........
- 0xFC,0x00, // ######..........
- 0xFC,0x00, // ######..........
- 0xFC,0x00, // ######..........
- 0xFE,0x00, // #######.........
- 0x7E,0x00, // .######.........
- 0x7F,0x00, // .#######........
- 0x3F,0x00, // ..######........
- 0x1F,0xC0, // ...#######......
- 0x07,0xC0 // .....#####......
- };
- const unsigned char sky[] 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,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,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,
- 0x00,0x02,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x78,0x00,0x00,0x71,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x38,0x00,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xF0,
- 0x00,0x00,0x01,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x70,0x04,0x00,0x00,0x0F,0xF0,
- 0x00,0x00,0x01,0xF7,0xFC,0x00,0x00,0x02,0x00,0x0F,0xF8,0x00,0x00,0x00,0x07,0xFC,
- 0x00,0x80,0x00,0xF9,0xDC,0x00,0x00,0x00,0x00,0x5F,0xF8,0x00,0x00,0x00,0x07,0xF8,
- 0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x00,0x00,0x00,0x00,0x00
- };
- int currentHorse = 0;
- int currentScreen = 0;
- int playTune = 1;
- int jumpSoundDone = 0;
- int currentMap[10];
- int currentMap2[10];
- int mapPart1Pos = 128;
- int mapPart2Pos = 128 + 300;
- int skyPart1Pos = 0;
- int horseAnimationTimer = 0;
- int jump = 0;
- int fall = 0;
- int playerX = 4;
- int playerY = 39;
- int score = 0;
- int menuMessage = 1;
- int menuCounter = 0;
- int gedichtScroll = 0;
- void drawMenu(void) {
- u8g.setFont(u8g_font_osb18);
- u8g.drawStr(12, 30, "Sinttendo");
- u8g.setFont(u8g_font_unifont);
- if(menuMessage == 1){
- u8g.drawStr(32, 50, "Up Start");
- }
- if(menuMessage == 0){
- u8g.drawStr(18, 50, "Down Gedicht");
- }
- }
- const String gedicht[] =
- {
- "xxxx's lijstje",
- "Stond dit jaar",
- "weer helemaal vol.",
- "Een pen, een kat",
- "en eenhoorn drol?",
- "Een grote lijst",
- "met leuke spullen,",
- "genoeg om een uur",
- "lang vol te lullen.",
- "Toch heeft Sint",
- "menig winkel bezocht,",
- "en uitendelijk maar",
- "iets leuks gekocht.",
- "Misschien iets om",
- "je geld in te",
- "bewaren, zodat je",
- "voor iets leuks",
- "kan sparen.",
- "De sint weet dat",
- "je gamen ook",
- "leuk vindt,",
- "daarom hoopt hij",
- "dat deze surprise",
- "je zint.",
- "Help sint en zijn",
- "paard over de",
- "daken, en probeer",
- "de obstakels niet",
- "te raken.",
- "Vergeet niet eerst",
- "je kado te bekijken,",
- "voordat je een",
- "hoge score probeert",
- "te bereiken",
- "Sinterklaas",
- };
- void drawGedicht(){
- u8g.setFont(u8g_font_courB08);
- int y = 30;
- for(int x = 0; x < 35; x++) {
- if(y + gedichtScroll >= 13 && y + gedichtScroll <= 64)
- {
- u8g.drawStr(8, y + gedichtScroll, gedicht[x].c_str());
- }
- y+=10;
- }
- }
- void drawGame(void) {
- drawSky(skyPart1Pos);
- u8g.setFont(u8g_font_unifont); // select font
- char buf[3];
- snprintf (buf, 3, "%d", score);
- u8g.drawStr( 2, 15, buf);
- u8g.drawBitmapP( playerX, playerY, 4, 23, horse[currentHorse]); // put bitmap
- u8g.drawBitmapP( 100, 10, 2, 15, moon); // put bitmap
- drawMap(mapPart1Pos, currentMap);
- drawMap(mapPart2Pos, currentMap2);
- u8g.drawLine(0,63, 128,63);
- }
- void drawGameOver(void) {
- u8g.setFont(u8g_font_osb18);
- u8g.drawStr(2, 30, "Game Over");
- u8g.setFont(u8g_font_unifont);
- char buf[3];
- snprintf (buf, 3, "%d", score);
- u8g.drawStr( 60, 50, buf);
- }
- void drawMap(int offset, int cmap[]) {
- int newPos = offset;
- for(int x = 0; x < 10; x++) {
- if(newPos > -30 && newPos < 128 && cmap[x] > -1){
- u8g.drawBitmapP( newPos, 50, 4, 13, house[cmap[x]]); // put bitmap
- hitDetect(newPos);
- }
- newPos+=30;
- }
- }
- int hitCount = 0;
- void hitDetect(int pos){
- if(pos == playerX){
- hitCount++;
- }
- if(pos <= playerX+6 && pos >= playerX-6 && playerY >= 30){
- currentScreen = 2;
- }
- if(hitCount == 8){
- tone(7, NOTE_G5, 10);
- score++;
- hitCount=0;
- }
- }
- void randomMap(int *cmap) {
- int last = 0;
- for(int x = 0; x < 10; x++) {
- int r = random(-5, 3);
- if(last >= 0){
- cmap[x] = -1;
- last = -1;
- }else{
- cmap[x] = r;
- last = r;
- }
- }
- }
- void drawSky(int x) {
- u8g.drawBitmapP( x / 10, 10, 16, 15, sky); // put bitmap
- }
- void setup(void) {
- randomMap(currentMap);
- randomMap(currentMap2);
- pinMode(4, INPUT);
- pinMode(3, INPUT);
- }
- void loop(void) {
- u8g.firstPage();
- do {
- if(currentScreen == 0) {
- drawMenu();
- if(menuCounter == 200){
- menuCounter = 0;
- if(menuMessage == 1){
- menuMessage = 0;
- }else{
- menuMessage = 1;
- }
- }else{
- menuCounter++;
- }
- }
- if(currentScreen == 1) {
- drawGame();
- }
- if(currentScreen == 2) {
- drawGameOver();
- }
- if(currentScreen == 3) {
- drawGedicht();
- }
- } while( u8g.nextPage() );
- if(horseAnimationTimer == 5) {
- if(currentHorse == 2)
- {
- currentHorse = 0;
- }
- else{
- currentHorse++;
- }
- horseAnimationTimer = 0;
- }else {
- horseAnimationTimer++;
- }
- if(mapPart1Pos == -300)
- {
- mapPart1Pos = 300;
- currentMap[0] = 0;
- }else{
- mapPart1Pos-=2;
- }
- if(mapPart2Pos == -300)
- {
- mapPart2Pos = 128 + 300;
- }else{
- mapPart2Pos-=2;
- }
- if(skyPart1Pos == -1280)
- {
- skyPart1Pos = 1280;
- }else{
- skyPart1Pos--;
- }
- if(jump == 1){
- if(playerY >= 15 && fall == 0){
- playerY-=(playerY / 10);
- }else{
- jump = 0;
- fall = 1;
- }
- }
- if(fall == 1){
- if(playerY <= 39 && jump == 0){
- playerY+=1;
- }else{
- fall = 0;
- playerY = 39;
- jumpSoundDone = 0;
- }
- }
- if(fall == 2){
- if(playerY <= 39 && jump == 1){
- playerY+=3;
- }else{
- fall = 0;
- playerY = 39;
- jumpSoundDone = 0;
- tone(7, NOTE_G3, 10);
- }
- }
- if(digitalRead(3) == HIGH) {
- if(currentScreen == 0 || currentScreen == 2){
- currentScreen = 1;
- currentHorse = 0;
- mapPart1Pos = 128;
- mapPart2Pos = 128 + 300;
- skyPart1Pos = 0;
- horseAnimationTimer = 0;
- jump = 0;
- fall = 0;
- playerX = 4;
- playerY = 39;
- score = 0;
- randomMap(currentMap);
- randomMap(currentMap2);
- }
- if(currentScreen == 1){
- jump = 1;
- if(jumpSoundDone == 0){
- tone(7, NOTE_C5, 10);
- jumpSoundDone = 1;
- }
- }
- if(currentScreen == 3){
- gedichtScroll+=2;
- }
- }
- if(digitalRead(4) == HIGH) {
- if(currentScreen == 0){
- currentScreen = 3;
- }
- if(currentScreen == 1){
- fall = 2;
- }
- if(currentScreen == 3){
- gedichtScroll-=2;
- }
- }
- if(playTune == 1){
- playSint();
- playTune=0;
- }
- }
- void playSint(){
- // iterate over the notes of the melody:
- for (int thisNote = 0; thisNote < 10; thisNote++) {
- // to calculate the note duration, take one second divided by the note type.
- //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
- int noteDuration = 1000 / noteDurations[thisNote];
- tone(7, melody[thisNote], noteDuration);
- // to distinguish the notes, set a minimum time between them.
- // the note's duration + 30% seems to work well:
- int pauseBetweenNotes = noteDuration * 1.30;
- delay(pauseBetweenNotes);
- // stop the tone playing:
- noTone(7);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement