Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 15.26 KB | None | 0 0
  1. #include "U8glib.h"
  2. #include "pitches.h"
  3.  
  4. int melody[] = {
  5.   NOTE_G4, NOTE_C5, NOTE_E5, NOTE_G5, NOTE_C5, NOTE_F5, NOTE_A5, NOTE_C6, NOTE_A5, NOTE_G5
  6. };
  7. int noteDurations[] = {
  8.   4, 2, 4, 2, 4, 4, 4, 4, 4, 2
  9. };
  10.  
  11. U8GLIB_SH1106_128X64 u8g(12, 11, 10, 8, 9);  // D0=12, D1=11, CS=10, DC=9, Reset=8
  12. const unsigned char horse[3][92] PROGMEM =
  13. {
  14. {
  15.   0x00,0x00,0x07,0x80, // .....................####.......
  16.   0x00,0x10,0x0C,0xC0, // ...........#........##..##......
  17.   0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
  18.   0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
  19.   0x00,0x3C,0x63,0x80, // ..........####...##...###.......
  20.   0x00,0x3C,0x40,0x00, // ..........####...#..............
  21.   0x00,0x3C,0xC0,0x00, // ..........####..##..............
  22.   0x00,0x19,0x80,0x00, // ...........##..##...............
  23.   0x00,0x1B,0x50,0x00, // ...........##.##.#.#............
  24.   0x00,0x7E,0x50,0x00, // .........######..#.#............
  25.   0x00,0x58,0x78,0x00, // .........#.##....####...........
  26.   0x00,0x18,0x6E,0x00, // ...........##....##.###.........
  27.   0x00,0x18,0x7E,0x00, // ...........##....######.........
  28.   0x80,0x3C,0xE0,0x00, // #.........####..###.............
  29.   0xF8,0xEF,0xE0,0x00, // #####...###.#######.............
  30.   0x7F,0xF7,0xE0,0x00, // .###########.######.............
  31.   0x0D,0xEF,0xE0,0x00, // ....##.####.#######.............
  32.   0x01,0xF7,0xE0,0x00, // .......#####.######.............
  33.   0x01,0xEF,0xE0,0x00, // .......####.#######.............
  34.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  35.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  36.   0x02,0x81,0xA0,0x00, // ......#.#......##.#.............
  37.   0x02,0x81,0x20,0x00  // ......#.#......#..#.............
  38. },
  39. {
  40.   0x00,0x00,0x07,0x80, // .....................####.......
  41.   0x00,0x10,0x0C,0xC0, // ...........#........##..##......
  42.   0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
  43.   0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
  44.   0x00,0x3C,0x63,0x80, // ..........####...##...###.......
  45.   0x00,0x3C,0x40,0x00, // ..........####...#..............
  46.   0x00,0x3C,0xC0,0x00, // ..........####..##..............
  47.   0x00,0x19,0x80,0x00, // ...........##..##...............
  48.   0x00,0x1B,0xA0,0x00, // ...........##.###.#.............
  49.   0x00,0x7E,0xA0,0x00, // .........######.#.#.............
  50.   0x00,0x58,0xF0,0x00, // .........#.##...####............
  51.   0x00,0x18,0xDC,0x00, // ...........##...##.###..........
  52.   0x00,0x18,0xFC,0x00, // ...........##...######..........
  53.   0x70,0x3C,0xE0,0x00, // .###......####..###.............
  54.   0xF8,0xEF,0xE0,0x00, // #####...###.#######.............
  55.   0x9F,0xF7,0xE0,0x00, // #..#########.######.............
  56.   0x0D,0xEF,0xE0,0x00, // ....##.####.#######.............
  57.   0x01,0xF7,0xC0,0x00, // .......#####.#####..............
  58.   0x01,0xEF,0xE0,0x00, // .......####.#######.............
  59.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  60.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  61.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  62.   0x01,0x40,0x80,0x00  // .......#.#......#...............
  63. },
  64. {
  65.   0x00,0x00,0x07,0x80, // .....................####.......
  66.   0x00,0x10,0x0C,0xC0, // ...........#........##..##......
  67.   0x00,0x18,0x1B,0x40, // ...........##......##.##.#......
  68.   0x00,0x18,0x32,0xC0, // ...........##.....##..#.##......
  69.   0x00,0x3C,0x63,0x80, // ..........####...##...###.......
  70.   0x00,0x3C,0x40,0x00, // ..........####...#..............
  71.   0x00,0x3C,0xC0,0x00, // ..........####..##..............
  72.   0x00,0x19,0x80,0x00, // ...........##..##...............
  73.   0x00,0x1B,0xA0,0x00, // ...........##.###.#.............
  74.   0x00,0x7E,0xA0,0x00, // .........######.#.#.............
  75.   0x00,0x58,0xF0,0x00, // .........#.##...####............
  76.   0x00,0x18,0xDC,0x00, // ...........##...##.###..........
  77.   0x00,0x18,0xFC,0x00, // ...........##...######..........
  78.   0x00,0x3C,0xE0,0x00, // ..........####..###.............
  79.   0x80,0xEF,0xE0,0x00, // #.......###.#######.............
  80.   0xFF,0xF7,0xE0,0x00, // ############.######.............
  81.   0x3D,0xEF,0xE0,0x00, // ..####.####.#######.............
  82.   0x01,0xF7,0xE0,0x00, // .......#####.######.............
  83.   0x01,0xEF,0xE0,0x00, // .......####.#######.............
  84.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  85.   0x01,0x40,0xA0,0x00, // .......#.#......#.#.............
  86.   0x00,0xA0,0x90,0x00, // ........#.#.....#..#............
  87.   0x00,0xA0,0x50,0x00  // ........#.#......#.#............
  88. }
  89. };
  90.  
  91. const unsigned char house[3][52] PROGMEM = {
  92.   {
  93.  0x00,0x07,0x00,0x00, // .............###................
  94.   0x00,0x0A,0x80,0x00, // ............#.#.#...............
  95.   0x00,0x17,0x40,0x00, // ...........#.###.#..............
  96.   0x00,0x2F,0xA0,0x00, // ..........#.#####.#.............
  97.   0x00,0x5F,0xD0,0x00, // .........#.#######.#............
  98.   0x00,0xB0,0x68,0x00, // ........#.##.....##.#...........
  99.   0x01,0x75,0x74,0x00, // .......#.###.#.#.###.#..........
  100.   0x02,0xF5,0x7A,0x00, // ......#.####.#.#.####.#.........
  101.   0x05,0xF0,0x7D,0x00, // .....#.#####.....#####.#........
  102.   0x0B,0xF5,0x7E,0x80, // ....#.######.#.#.######.#.......
  103.   0x17,0xF5,0x7F,0x40, // ...#.#######.#.#.#######.#......
  104.   0x2F,0xF0,0x7F,0xA0, // ..#.########.....########.#.....
  105.   0xDF,0xFF,0xFF,0xD8  // ##.#######################.##...
  106.   },
  107.   {
  108.   0x00,0x00,0x00,0x00, // ................................
  109.   0x00,0x08,0x80,0x00, // ............#...#...............
  110.   0x00,0x1F,0xC0,0x00, // ...........#######..............
  111.   0x00,0x3F,0xE0,0x00, // ..........#########.............
  112.   0x00,0x7F,0xF0,0x00, // .........###########............
  113.   0x00,0xFF,0xF8,0x00, // ........#############...........
  114.   0x01,0xFF,0xFC,0x00, // .......###############..........
  115.   0x03,0xFA,0xFE,0x00, // ......#######.#.#######.........
  116.   0x07,0xF2,0x7F,0x00, // .....#######..#..#######........
  117.   0x0F,0xFF,0xFF,0x80, // ....#####################.......
  118.   0x1F,0xE2,0x3F,0xC0, // ...########...#...########......
  119.   0x3F,0xC2,0x1F,0xE0, // ..########....#....########.....
  120.   0xFF,0xFF,0xFF,0xF8  // #############################...
  121.   },
  122.   {
  123.   0x00,0x00,0x00,0x00, // ................................
  124.   0x00,0x05,0x00,0x00, // .............#.#................
  125.   0x00,0x1F,0xC0,0x00, // ...........#######..............
  126.   0x00,0x1F,0xC0,0x00, // ...........#######..............
  127.   0x00,0x0F,0x80,0x00, // ............#####...............
  128.   0x00,0x0F,0x80,0x00, // ............#####...............
  129.   0x00,0x0F,0x80,0x00, // ............#####...............
  130.   0x00,0x0F,0x80,0x00, // ............#####...............
  131.   0x00,0x0F,0x80,0x00, // ............#####...............
  132.   0x00,0x0F,0x80,0x00, // ............#####...............
  133.   0x00,0x0F,0x80,0x00, // ............#####...............
  134.   0x00,0x0F,0x80,0x00, // ............#####...............
  135.   0x00,0x0F,0x80,0x00  // ............#####...............
  136.   }
  137. };
  138.  
  139. const unsigned char moon[] PROGMEM = {
  140.  0x07,0xC0, // .....#####......
  141.   0x1F,0xC0, // ...#######......
  142.   0x3F,0x80, // ..#######.......
  143.   0x7F,0x00, // .#######........
  144.   0x7F,0x00, // .#######........
  145.   0xFE,0x00, // #######.........
  146.   0xFC,0x00, // ######..........
  147.   0xFC,0x00, // ######..........
  148.   0xFC,0x00, // ######..........
  149.   0xFE,0x00, // #######.........
  150.   0x7E,0x00, // .######.........
  151.   0x7F,0x00, // .#######........
  152.   0x3F,0x00, // ..######........
  153.   0x1F,0xC0, // ...#######......
  154.   0x07,0xC0  // .....#####......
  155. };
  156.  
  157. const unsigned char sky[] PROGMEM = {
  158.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  159.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  160.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  161.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  162.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  163.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
  164.   0x00,0x02,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  165.   0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  166.   0x78,0x00,0x00,0x71,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  167.   0x38,0x00,0x00,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xF0,
  168.   0x00,0x00,0x01,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x70,0x04,0x00,0x00,0x0F,0xF0,
  169.   0x00,0x00,0x01,0xF7,0xFC,0x00,0x00,0x02,0x00,0x0F,0xF8,0x00,0x00,0x00,0x07,0xFC,
  170.   0x00,0x80,0x00,0xF9,0xDC,0x00,0x00,0x00,0x00,0x5F,0xF8,0x00,0x00,0x00,0x07,0xF8,
  171.   0x00,0x00,0x00,0x00,0x78,0x00,0x00,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,
  172.   0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x00,0x00,0x00,0x00,0x00
  173. };
  174.  
  175. int currentHorse = 0;
  176. int currentScreen = 0;
  177. int playTune = 1;
  178. int jumpSoundDone = 0;
  179.  
  180. int currentMap[10];
  181. int currentMap2[10];
  182.  
  183. int mapPart1Pos = 128;
  184. int mapPart2Pos = 128 + 300;
  185.  
  186. int skyPart1Pos = 0;
  187.  
  188. int horseAnimationTimer = 0;
  189.  
  190. int jump = 0;
  191. int fall = 0;
  192. int playerX = 4;
  193. int playerY = 39;
  194. int score = 0;
  195.  
  196. int menuMessage = 1;
  197. int menuCounter = 0;
  198.  
  199. int gedichtScroll = 0;
  200.  
  201. void drawMenu(void) {
  202.   u8g.setFont(u8g_font_osb18);
  203.   u8g.drawStr(12, 30, "Sinttendo");
  204.  
  205.   u8g.setFont(u8g_font_unifont);
  206.   if(menuMessage == 1){
  207.     u8g.drawStr(32, 50, "Up Start");
  208.   }
  209.   if(menuMessage == 0){
  210.     u8g.drawStr(18, 50, "Down Gedicht");
  211.   }
  212. }
  213.  
  214. const String gedicht[] =
  215. {
  216. "xxxx's lijstje",
  217. "Stond dit jaar",
  218. "weer helemaal vol.",
  219. "Een pen, een kat",
  220. "en eenhoorn drol?",
  221. "Een grote lijst",
  222. "met leuke spullen,",
  223. "genoeg om een uur",
  224. "lang vol te lullen.",
  225. "Toch heeft Sint",
  226. "menig winkel bezocht,",
  227. "en uitendelijk maar",
  228. "iets leuks gekocht.",
  229. "Misschien iets om",
  230. "je geld in te",
  231. "bewaren, zodat je",
  232. "voor iets leuks",
  233. "kan sparen.",
  234. "De sint weet dat",
  235. "je gamen ook",
  236. "leuk vindt,",
  237. "daarom hoopt hij",
  238. "dat deze surprise",
  239. "je zint.",
  240. "Help sint en zijn",
  241. "paard over de",
  242. "daken, en probeer",
  243. "de obstakels niet",
  244. "te raken.",
  245. "Vergeet niet eerst",
  246. "je kado te bekijken,",
  247. "voordat je een",
  248. "hoge score probeert",
  249. "te bereiken",
  250. "Sinterklaas",
  251. };
  252.  
  253. void drawGedicht(){
  254.   u8g.setFont(u8g_font_courB08);
  255.   int y = 30;
  256.   for(int x = 0; x < 35; x++)  {
  257.     if(y + gedichtScroll >= 13 && y + gedichtScroll <= 64)
  258.     {
  259.       u8g.drawStr(8, y + gedichtScroll, gedicht[x].c_str());
  260.     }
  261.     y+=10;
  262.   }
  263. }
  264.  
  265. void drawGame(void) {
  266.   drawSky(skyPart1Pos);
  267.   u8g.setFont(u8g_font_unifont);  // select font
  268.   char buf[3];
  269.   snprintf (buf, 3, "%d", score);
  270.   u8g.drawStr( 2, 15, buf);
  271.  
  272.   u8g.drawBitmapP( playerX, playerY, 4, 23, horse[currentHorse]);  // put bitmap
  273.  
  274.   u8g.drawBitmapP( 100, 10, 2, 15, moon);  // put bitmap
  275.  
  276.   drawMap(mapPart1Pos, currentMap);
  277.   drawMap(mapPart2Pos, currentMap2);
  278.   u8g.drawLine(0,63, 128,63);
  279. }
  280.  
  281. void drawGameOver(void) {
  282.   u8g.setFont(u8g_font_osb18);
  283.   u8g.drawStr(2, 30, "Game Over");
  284.   u8g.setFont(u8g_font_unifont);
  285.   char buf[3];
  286.   snprintf (buf, 3, "%d", score);
  287.   u8g.drawStr( 60, 50, buf);
  288. }
  289.  
  290. void drawMap(int offset, int cmap[]) {
  291.     int newPos = offset;
  292.     for(int x = 0; x < 10; x++)  {
  293.       if(newPos > -30 && newPos < 128 && cmap[x] > -1){
  294.         u8g.drawBitmapP( newPos, 50, 4, 13, house[cmap[x]]);  // put bitmap
  295.         hitDetect(newPos);
  296.       }
  297.       newPos+=30;
  298.     }
  299. }
  300.  
  301. int hitCount = 0;
  302. void hitDetect(int pos){
  303.  if(pos == playerX){
  304.    hitCount++;
  305.  }
  306.  if(pos <= playerX+6 && pos >= playerX-6 && playerY >= 30){
  307.    currentScreen = 2;
  308.  }
  309.  if(hitCount == 8){
  310.    tone(7, NOTE_G5, 10);
  311.    score++;
  312.    hitCount=0;
  313.  }
  314. }
  315.  
  316.  
  317.  
  318. void randomMap(int *cmap) {
  319.   int last = 0;
  320.   for(int x = 0; x < 10; x++)  {
  321.     int r = random(-5, 3);
  322.     if(last >= 0){
  323.        cmap[x] = -1;
  324.        last = -1;
  325.     }else{
  326.        cmap[x] = r;
  327.        last = r;
  328.     }
  329.   }
  330. }
  331.  
  332. void drawSky(int x) {
  333.    u8g.drawBitmapP( x / 10, 10, 16, 15, sky);  // put bitmap
  334. }
  335.  
  336. void setup(void) {
  337.   randomMap(currentMap);
  338.   randomMap(currentMap2);
  339.   pinMode(4, INPUT);
  340.   pinMode(3, INPUT);
  341.  
  342. }
  343.  
  344. void loop(void) {
  345.   u8g.firstPage();  
  346.   do {
  347.     if(currentScreen == 0) {
  348.       drawMenu();
  349.       if(menuCounter == 200){
  350.         menuCounter = 0;
  351.         if(menuMessage == 1){
  352.           menuMessage = 0;
  353.         }else{
  354.           menuMessage = 1;
  355.         }
  356.       }else{
  357.         menuCounter++;
  358.       }
  359.     }
  360.     if(currentScreen == 1) {
  361.       drawGame();
  362.     }
  363.     if(currentScreen == 2) {
  364.       drawGameOver();
  365.     }
  366.     if(currentScreen == 3) {
  367.       drawGedicht();
  368.     }
  369.   } while( u8g.nextPage() );
  370.  
  371.     if(horseAnimationTimer == 5) {
  372.       if(currentHorse == 2)
  373.       {
  374.         currentHorse = 0;
  375.         }
  376.         else{
  377.           currentHorse++;
  378.         }
  379.         horseAnimationTimer = 0;
  380.     }else {
  381.       horseAnimationTimer++;
  382.     }
  383.  
  384.     if(mapPart1Pos == -300)
  385.     {
  386.       mapPart1Pos = 300;
  387.       currentMap[0] = 0;
  388.       }else{
  389.         mapPart1Pos-=2;
  390.       }
  391.  
  392.     if(mapPart2Pos == -300)
  393.     {
  394.       mapPart2Pos = 128 + 300;
  395.       }else{
  396.         mapPart2Pos-=2;
  397.       }
  398.  
  399.    
  400.     if(skyPart1Pos == -1280)
  401.     {
  402.       skyPart1Pos = 1280;
  403.       }else{
  404.         skyPart1Pos--;
  405.       }
  406.  
  407.     if(jump == 1){
  408.       if(playerY >= 15 && fall == 0){
  409.         playerY-=(playerY / 10);
  410.       }else{
  411.         jump = 0;
  412.         fall = 1;
  413.       }
  414.     }
  415.  
  416.     if(fall == 1){
  417.       if(playerY <= 39 && jump == 0){
  418.          playerY+=1;
  419.       }else{
  420.         fall = 0;
  421.         playerY = 39;
  422.         jumpSoundDone = 0;
  423.       }
  424.     }
  425.  
  426.      if(fall == 2){
  427.       if(playerY <= 39 && jump == 1){
  428.          playerY+=3;
  429.       }else{
  430.         fall = 0;
  431.         playerY = 39;
  432.         jumpSoundDone = 0;
  433.         tone(7, NOTE_G3, 10);
  434.       }
  435.     }
  436.  
  437.       if(digitalRead(3) == HIGH) {
  438.         if(currentScreen == 0 || currentScreen == 2){
  439.           currentScreen = 1;
  440.           currentHorse = 0;
  441.           mapPart1Pos = 128;
  442.           mapPart2Pos = 128 + 300;
  443.           skyPart1Pos = 0;
  444.           horseAnimationTimer = 0;
  445.           jump = 0;
  446.           fall = 0;
  447.           playerX = 4;
  448.           playerY = 39;
  449.           score = 0;
  450.           randomMap(currentMap);
  451.           randomMap(currentMap2);
  452.         }
  453.         if(currentScreen == 1){
  454.           jump = 1;
  455.           if(jumpSoundDone == 0){
  456.             tone(7, NOTE_C5, 10);
  457.             jumpSoundDone = 1;
  458.           }
  459.         }
  460.         if(currentScreen == 3){
  461.           gedichtScroll+=2;
  462.         }
  463.       }
  464.  
  465.       if(digitalRead(4) == HIGH) {
  466.         if(currentScreen == 0){
  467.           currentScreen = 3;
  468.         }
  469.         if(currentScreen == 1){
  470.           fall = 2;
  471.         }
  472.         if(currentScreen == 3){
  473.           gedichtScroll-=2;
  474.         }
  475.       }
  476.  
  477.       if(playTune == 1){
  478.         playSint();
  479.         playTune=0;
  480.       }
  481.      
  482.    }
  483.  
  484.    void playSint(){
  485.      // iterate over the notes of the melody:
  486.      for (int thisNote = 0; thisNote < 10; thisNote++) {
  487.  
  488.     // to calculate the note duration, take one second divided by the note type.
  489.     //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
  490.     int noteDuration = 1000 / noteDurations[thisNote];
  491.     tone(7, melody[thisNote], noteDuration);
  492.  
  493.     // to distinguish the notes, set a minimum time between them.
  494.     // the note's duration + 30% seems to work well:
  495.     int pauseBetweenNotes = noteDuration * 1.30;
  496.     delay(pauseBetweenNotes);
  497.     // stop the tone playing:
  498.     noTone(7);
  499.   }
  500.    }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement