Advertisement
BeamNG_IRC

Untitled

Nov 26th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. // some required libraries
  2. #include <LiquidCrystal.h> // lcd display
  3. #include <SPI.h> // communications
  4. #include <Wire.h> // i have no idea, but needed
  5. #include <Adafruit_GFX.h> // gfx library for oled
  6. #include <Adafruit_SSD1306.h> // oled display driver
  7. #define OLED_RESET 4 // oled reset code
  8. Adafruit_SSD1306 display(OLED_RESET); // oled reset
  9. LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd setup
  10. // variables used for buttons
  11. const int buttonPin_left = 7; // left button pin
  12. const int buttonPin_up = 8; // up button pin
  13. const int buttonPin_right = 9; // right button pin
  14. int buttonState_up = 0; // up button state
  15. int buttonState_left = 0; // left button state
  16. int buttonState_right = 0; // right button state
  17. int Score = 0; // our scores variable
  18. int ScoreLock = 0; // locks our score counter
  19. // lets create some functions
  20. void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
  21.     lcd.setCursor(x, y); // set our lcd cursor position
  22.     lcd.print(text); // write to the lcd screen
  23. }
  24.  
  25. void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
  26.     display.setTextSize(size); // set the font size
  27.     display.setTextColor(WHITE); // set the font color
  28.     display.setCursor(x, y); // set the cursor position
  29.     display.println(text); // add our text to the oled buffer
  30. }
  31.  
  32. void RenderScene() { // this function will render our scene on the oled display
  33.     display.display(); // update our oled (print buffer)
  34. }
  35.  
  36. void DrawScore(int Add) { // this function will draw our score to screen
  37.     Score = Score + Add; // add to our score
  38.     OledWrite(100, 5, 1, String(Score)); // put our score into the oled buffer
  39. }
  40.  
  41. // our platform arrays
  42. int Platform1[] = {0, 63, 128, 63}; // x1, y1, x2, y2
  43. int Pipe1[] = {128, 63, 128, 40}; // our first pipe
  44. int Pipe2[] = {128, 0, 128, 20}; // our second pipe
  45. int PipeSpeed = 2; // our pipes speed
  46. void DrawPlatforms() { // this function will add our platforms to the oled buffer
  47.     /*
  48.     Lets go into a little detail in this function:
  49.     We use the drawLine() function to draw a line to the oled.
  50.     the arguments are as follows:
  51.     x1, y1, x2, y2, color
  52.     Lets think of using a pen.
  53.     x1 = the starting x position of the pens tip
  54.     y1 = the starting y position of the pens tip
  55.     x2 = the ending x position of the pens tip
  56.     y2 = the ending y position of the pens tip
  57.     so, between these coordinates, a line is drawn
  58.     color = the color of the pen tip
  59.     */
  60.     display.drawLine(Platform1[0], Platform1[1], Platform1[2], Platform1[3], WHITE); // put our first platform into the buffer
  61.     if (Pipe1[0] > 0) {
  62.         Pipe1[0] = Pipe1[0] - PipeSpeed; // change our pipes x pos
  63.         Pipe1[2] = Pipe1[2] - PipeSpeed; // change our pipes x pos
  64.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  65.     } else {
  66.         ScoreLock = 0; // unlock our score counter
  67.         Pipe1[0] = 138; // set our pipe back to defalt settings
  68.         Pipe1[2] = 138; // set our pipe back to defalt settings
  69.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  70.         }
  71. }
  72.  
  73. // our characters arrays
  74. String character[] = {"*"}; // this array holds our characters images
  75. int CharacterPos[] = {30, 5}; // our characters x and y positions
  76. void Controls() { // this functions checks for input from the controller
  77.     const int Speed = 3; // our characters speed
  78.     buttonState_up = digitalRead(buttonPin_up); // up button state
  79.     buttonState_left = digitalRead(buttonPin_left); // left button state
  80.     buttonState_right = digitalRead(buttonPin_right); // right button state
  81.     if (buttonState_up == HIGH && CharacterPos[1] > 5) { // up button detection
  82.         // This is our jump script
  83.         CharacterPos[1] = CharacterPos[1] - 7; // change the y position by - 1
  84.     }
  85.     if (buttonState_left == HIGH && CharacterPos[0] > 3) { // left button detection
  86.         PipeSpeed = PipeSpeed - 1; // Slow our pipes down
  87.         //CharacterPos[0] = CharacterPos[0] - Speed; // change the x position by - speed
  88.     }
  89.     if (buttonState_right == HIGH && CharacterPos[0] < 120) { // right button detection
  90.         PipeSpeed = PipeSpeed + 1; // speed our pipes up
  91.         //CharacterPos[0] = CharacterPos[0] + Speed; // change the x position by + speed
  92.     }
  93. }
  94.  
  95. void Gravity() { // this function will control the gravitational forces of our character
  96.     // this is hard to do
  97.     if (CharacterPos[1] <= Platform1[1] - 10) { // if either condition is met, fall
  98.         CharacterPos[1] = CharacterPos[1] + 3; // apply gravity
  99.     }
  100. }
  101.  
  102. void DrawCharacter() { // this function will draw our character to the screen
  103.     if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] < Pipe1[3] && ScoreLock == 0) { // if the character jumped over a pipe, add 1 to the score
  104.         DrawScore(1); // add one to the score
  105.         ScoreLock = 1; // lock our score counter
  106.     }
  107.     OledWrite(CharacterPos[0], CharacterPos[1], 1, character[0]); // draw the character to the buffers
  108. }
  109.  
  110. void EndGame() { // this function will end our game
  111.      asm volatile ("jmp 0"); // restart the arduino
  112. }
  113.  
  114. void CheckCollision() { // this function will check for collisions between the character and pipes
  115.     if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] > Pipe1[3]) { // if a collision is detected, end the game
  116.         //OledWrite(45, 30, 2, "FAIL"); // display end game text
  117.         EndGame(); // end the game
  118.     }
  119. }
  120.    
  121. void Debugging() { // this function displays debugging info on the lcd display
  122.     lcd.setCursor(0, 1); // set our lcd cursor position
  123.     lcd.print("X: "); // debugging info
  124.     lcd.print(CharacterPos[0]); // debugging info
  125.     lcd.print(" Y: "); // debugging info
  126.     lcd.print(CharacterPos[1]); // debugging info
  127. }
  128.    
  129. void setup() { // our function that is loaded first
  130.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
  131.     display.clearDisplay(); // clear oled display from anything left over from last session
  132.     lcd.begin(16, 2); // define our lcd properties
  133.     lcd.clear(); // clear our lcd screen from any residue
  134.     LcdWrite(0, 0, "Flappy Asterik"); // display program name to lcd
  135.     //LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
  136. }
  137.  
  138. void loop() { // our looping function
  139.     display.clearDisplay(); // clear our oled display (removes ghosting)
  140.     Controls(); // check for input
  141.     DrawCharacter(); // draw our character to the buffer
  142.     DrawPlatforms(); // put our platforms into the oled buffer
  143.     DrawScore(0); // draw our score to the screen  
  144.     Gravity(); // run our gravity script
  145.     CheckCollision(); // check for collisions with pipes
  146.     Debugging(); // display our debugging information
  147.     RenderScene(); // render our scene to the oled display
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement