Advertisement
Guest User

Flappy Asterisk

a guest
Jan 5th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.55 KB | None | 0 0
  1. // some required libraries
  2. #include <LiquidCrystal.h> // lcd display
  3. #include <EEPROM.h> // EEPROM libray
  4. #include <SPI.h> // communications
  5. #include <Wire.h> // i have no idea, but needed
  6. #include <Adafruit_GFX.h> // gfx library for oled
  7. #include <Adafruit_SSD1306.h> // oled display driver
  8. #define OLED_RESET 4 // oled reset code
  9. Adafruit_SSD1306 display(OLED_RESET); // oled reset
  10. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd setup
  11. // variables used for buttons
  12. const int ButtonLeftPin = 7; // left button pin
  13. const int ButtonUpPin = 8; // up button pin
  14. const int ButtonRightPin = 9; // right button pin
  15. int ButtonUp = 0; // up button state
  16. int ButtonLeft = 0; // left button state
  17. int ButtonRight = 0; // right button state
  18. int Score = 0; // our scores variable
  19. int HighScoreCount = 0; // our high score variable
  20. int ScoreLock = 0; // locks our score counter
  21. int Pause = 1; // when set to one, the game is practically Paused
  22.  
  23. // lets create some functions
  24. void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
  25.     lcd.setCursor(x, y); // set our lcd cursor position
  26.     lcd.print(text); // write to the lcd screen
  27. }
  28.  
  29. void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
  30.     display.setTextSize(size); // set the font size
  31.     display.setTextColor(WHITE); // set the font color
  32.     display.setCursor(x, y); // set the cursor position
  33.     display.println(text); // add our text to the oled buffer
  34. }
  35.  
  36. void RenderScene() { // this function will render our scene on the oled display
  37.     display.display(); // update our oled (print buffer)
  38. }
  39.  
  40. void DrawScore(int Add) { // this function will draw our score to screen
  41.     Score = Score + Add; // add to our score
  42.     OledWrite(100, 5, 1, String(Score)); // put our score into the oled buffer
  43. }
  44.  
  45. // our platform arrays
  46. int Platform1[] = {0, 63, 128, 63}; // x1, y1, x2, y2
  47. int Pipe1[] = {128, 63, 128, 40}; // our first pipe
  48. int PipeSpeed = 4; // our pipes speed
  49. int Pos[] = {0, 0}; // this will hold our pipes new x position
  50. void DrawPlatforms() { // this function will add our platforms to the oled buffer
  51.     /*
  52.     Lets go into a little detail in this function:
  53.     We use the drawLine() function to draw a line to the oled.
  54.     the arguments are as follows:
  55.     x1, y1, x2, y2, color
  56.     Lets think of using a pen.
  57.     x1 = the starting x position of the pens tip
  58.     y1 = the starting y position of the pens tip
  59.     x2 = the ending x position of the pens tip
  60.     y2 = the ending y position of the pens tip
  61.     so, between these coordinates, a line is drawn
  62.     color = the color of the pen tip
  63.     */
  64.     display.drawLine(Platform1[0], Platform1[1], Platform1[2], Platform1[3], WHITE); // put our first platform into the buffer
  65.     if (Pipe1[0] > 0) {
  66.         Pipe1[0] = Pipe1[0] - PipeSpeed; // change our pipes x pos
  67.         Pipe1[2] = Pipe1[2] - PipeSpeed; // change our pipes x pos
  68.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  69.     } else {
  70.         ScoreLock = 0; // unlock our score counter
  71.         // now lets change which pipe is used
  72.         if (Pipe1[1] == 63) { // if our pipes x value == 63
  73.             Pipe1[1] = 0; // change the x value to another structure
  74.             Pipe1[3] = 20; // change the x value to another structure
  75.         } else { // if the case is not met, change the x value to a different structure
  76.             Pipe1[1] = 63; // change the x value to another structure
  77.             Pipe1[3] = 40; // change the x value to another structure
  78.         }              
  79.         Pipe1[0] = 138; // set our pipe back to defalt settings
  80.         Pipe1[2] = 138; // set our pipe back to defalt settings
  81.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  82.     }
  83. }
  84.  
  85. // our characters arrays
  86. int x = 1; // this will hold which index value we will use to draw our character
  87. String character[] = {"*", "*"}; // this array holds our characters images
  88. int CharacterPos[] = {10, 5}; // our characters x and y positions
  89. void Controls() { // this functions checks for input from the controller
  90.     const int Speed = 3; // our characters speed
  91.     ButtonUp = digitalRead(ButtonUpPin); // up button state
  92.     ButtonLeft = digitalRead(ButtonLeftPin); // left button state
  93.     ButtonRight = digitalRead(ButtonRightPin); // right button state
  94.     if (ButtonUp == HIGH && CharacterPos[1] > 5) { // up button detection
  95.         // This is our jump script
  96.         CharacterPos[1] = CharacterPos[1] - 7; // change the y position by - 1
  97.     }
  98.     if (ButtonLeft == HIGH && CharacterPos[0] > 3) { // left button detection
  99.         PipeSpeed = PipeSpeed - 1; // Slow our pipes down
  100.         //CharacterPos[0] = CharacterPos[0] - Speed; // change the x position by - speed
  101.     }
  102.     if (ButtonRight == HIGH && CharacterPos[0] < 120) { // right button detection
  103.         PipeSpeed = PipeSpeed + 1; // speed our pipes up
  104.         //CharacterPos[0] = CharacterPos[0] + Speed; // change the x position by + speed
  105.     }
  106.     if (ButtonUp == HIGH && Pause == 1) { // if the ump button is pressed and the game is Paused, un-Pause
  107.         Pause = 0; // un-Pause the game
  108.     }
  109. }
  110.  
  111. void Gravity() { // this function will control the gravitational forces of our character
  112.     // this is hard to do
  113.     if (CharacterPos[1] <= Platform1[1] - 10) { // if either condition is met, fall
  114.         CharacterPos[1] = CharacterPos[1] + 3; // apply gravity
  115.     }
  116. }
  117.  
  118. void DrawCharacter() { // this function will draw our character to the screen
  119.     if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] < Pipe1[3] && ScoreLock == 0) { // if the character jumped over a pipe, add 1 to the score
  120.         DrawScore(1); // add one to the score
  121.         //analogWrite(6, 100); // buzz the buzzer
  122.         //delay(100); // delay a few ms
  123.         //analogWrite(6, 0); // turn the buzzer off
  124.         ScoreLock = 1; // lock our score counter
  125.     }
  126.     if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] > Pipe1[3] && ScoreLock == 0) { // if the character jumped over a pipe, add 1 to the score
  127.         DrawScore(1); // add one to the score
  128.         //analogWrite(6, 100); // buzz the buzzer
  129.         //delay(100); // delay a few ms
  130.         //analogWrite(6, 0); // turn the buzzer off
  131.         ScoreLock = 1; // lock our score counter
  132.     }
  133.     if (x == 0) { // change our index reference
  134.         x = 1; // set our index referenece to one
  135.     } else { // if x == 1
  136.         x = 0; // set our index reference
  137.     }
  138.     OledWrite(CharacterPos[0], CharacterPos[1], 1, character[x]); // draw the character to the buffers
  139. }
  140.  
  141. void EndGame() { // this function will end our game
  142.     analogWrite(6, 50); // buzz the buzzer
  143.     delay(200); // delay a few ms
  144.     analogWrite(6, 0); // turn the buzzer off
  145.     delay(200); // delay a few ms
  146.     analogWrite(6, 50); // buzz the buzzer
  147.     delay(200); // delay a few ms
  148.     analogWrite(6, 0); // turn the buzzer off
  149.     asm volatile("jmp 0"); // restart the arduino
  150. }
  151.  
  152. void CheckCollision() { // this function will check for collisions between the character and pipes
  153.     if (Pipe1[1] == 63) { // if the x of Pipe1 is 53, top pipe
  154.         if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] > Pipe1[3]) { // if a collision is detected with the bottom pipe, end the game
  155.             EndGame(); // end the game
  156.         }
  157.     } else { // if the x of Pipe1 is not 63, bottom pipe
  158.         if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] < Pipe1[3]) { // if a collision is detected with the top pipe, end the game
  159.             EndGame(); // end the game
  160.         }      
  161.     }
  162.     if (CharacterPos[1] >= 54) { // if the character hits the ground, end game
  163.         EndGame(); // end the game
  164.     }
  165. }
  166.  
  167. void Debugging() { // this function displays debugging info on the lcd display
  168.     lcd.setCursor(0, 1); // set our lcd cursor position
  169.     lcd.print("X: "); // debugging info
  170.     lcd.print(CharacterPos[0]); // debugging info
  171.     lcd.print(" Y: "); // debugging info
  172.     lcd.print(CharacterPos[1]); // debugging info
  173.     OledWrite(1, 1, 1, "TESTS"); // display test string
  174. }
  175. int RomHighScore = EEPROM.read(1); // read our high score from the eeprom address 1
  176. void HighScore() { // this function will control our high score in the eeprom
  177.     if (Score > RomHighScore) { // if the current score is higher than the highest score
  178.         EEPROM.write(1, Score); // write our new high score to eeprom address 1
  179.     }
  180. }
  181.  
  182. int Key = 0;
  183. void NameInput() { // name input system tests
  184.     if (Key < 0) {
  185.         Key = 35;
  186.     }
  187.     if (Key > 35) {
  188.         Key = -1;
  189.     }
  190.     String EN_Keys[] = {"A", "B", "C", "D",
  191.                                        "E", "F", "G", "H",
  192.                                        "I",  "J",  "K",  "L",
  193.                                        "M", "N", "O", "P",
  194.                                        "Q", "R", "S", "T",
  195.                                        "U", "V", "W", "X",
  196.                                        "Y",  "Z",  "0",  "1",
  197.                                        "2",  "3",  "4",  "5",
  198.                                        "6",  "7",  "8",  "9"}; // english alphabet and numerals
  199.     ButtonUp = digitalRead(ButtonUpPin); // up button state
  200.     ButtonLeft = digitalRead(ButtonLeftPin); // left button state
  201.     ButtonRight = digitalRead(ButtonRightPin); // right button state
  202.     if (ButtonUp == HIGH) { // up button detection
  203.         display.clearDisplay();
  204.         OledWrite(20, 20, 2, EN_Keys[Key]);
  205.     }
  206.     if (ButtonLeft == HIGH) { // left button detection
  207.         Key = Key - 1;
  208.         display.clearDisplay();
  209.         OledWrite(20, 20, 2, EN_Keys[Key]);
  210.     }
  211.     if (ButtonRight == HIGH) { // right button detection
  212.         Key++;
  213.         display.clearDisplay();
  214.         OledWrite(20, 20, 2, EN_Keys[Key]);
  215.     }
  216.     OledWrite(20, 50, 2, "NAME?");
  217.     delay(50); // delay a few ms
  218. }
  219.  
  220. void setup() { // our function that is loaded first
  221.     pinMode(6, OUTPUT); // our buzzer on pin 6
  222.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
  223.     display.clearDisplay(); // clear oled display from anything left over from last session
  224.     lcd.begin(16, 2); // define our lcd properties
  225.     lcd.clear(); // clear our lcd screen from any residue
  226.     LcdWrite(0, 0, "Flappy Asterisk"); // display program name to lcd
  227. }
  228. //Pause = 3; // delete upon dev setup completion
  229. void loop() { // our looping function
  230.     HighScore(); // check and store our high rom into a var
  231.     if (Pause == 0) { // only run if the game isn't 'Paused'
  232.         display.clearDisplay(); // clear our oled display (removes ghosting)
  233.         DrawCharacter(); // draw our character to the buffer
  234.         DrawPlatforms(); // put our platforms into the oled buffer
  235.         DrawScore(0); // draw our score to the screen  
  236.         Gravity(); // run our gravity script
  237.         CheckCollision(); // check for collisions with pipes
  238.     } else if (Pause == 1) { // if the game is Paused
  239.         OledWrite(20, 20, 2, "_PLAY_"); // display the 'begin' text
  240.         OledWrite(20, 40, 1, "HIGH SCORE"); // display our high score
  241.         OledWrite(40, 50, 2, String(RomHighScore)); // write our high score to the screen
  242.     }
  243.     Controls(); // check for input
  244.     Debugging(); // display our debugging information
  245.     //NameInput(); // dev setup
  246.     RenderScene(); // render our scene to the oled display
  247. }
  248.  
  249. /*
  250. TO DO:
  251. Add name to high score - too hard with eeprom
  252. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement