Advertisement
BeamNG_IRC

Untitled

Nov 26th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 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. // lets create some functions
  19. void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
  20.     lcd.setCursor(x, y); // set our lcd cursor position
  21.     lcd.print(text); // write to the lcd screen
  22. }
  23.  
  24. void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
  25.     display.setTextSize(size); // set the font size
  26.     display.setTextColor(WHITE); // set the font color
  27.     display.setCursor(x, y); // set the cursor position
  28.     display.println(text); // add our text to the oled buffer
  29. }
  30.  
  31. void RenderScene() { // this function will render our scene on the oled display
  32.     display.display(); // update our oled (print buffer)
  33. }
  34.  
  35. void DrawScore(int Add) { // this function will draw our score to screen
  36.     Score = Score + Add; // add to our score
  37.     OledWrite(100, 5, 1, String(Score));
  38. }
  39.  
  40. // our platform arrays
  41. int Platform1[] = {0, 63, 128, 63}; // x1, y1, x2, y2
  42. int Pipe1[] = {128, 63, 128, 40}; // our first pipe part
  43. int PipeSpeed = 2; // our pipes speed
  44. void DrawPlatforms() { // this function will add our platforms to the oled buffer
  45.     /*
  46.     Lets go into a little detail in this function:
  47.     We use the drawLine() function to draw a line to the oled.
  48.     the arguments are as follows:
  49.     x1, y1, x2, y2, color
  50.     Lets think of using a pen.
  51.     x1 = the starting x position of the pens tip
  52.     y1 = the starting y position of the pens tip
  53.     x2 = the ending x position of the pens tip
  54.     y2 = the ending y position of the pens tip
  55.     so, between these coordinates, a line is drawn
  56.     color = the color of the pen tip
  57.     */
  58.     display.drawLine(Platform1[0], Platform1[1], Platform1[2], Platform1[3], WHITE); // put our first platform into the buffer
  59.     if (Pipe1[0] > 0) {
  60.         Pipe1[0] = Pipe1[0] - PipeSpeed; // change our pipes x pos
  61.         Pipe1[2] = Pipe1[2] - PipeSpeed; // change our pipes x pos
  62.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  63.     } else {
  64.         Pipe1[0] = 138; // set our pipe back to defalt settings
  65.         Pipe1[2] = 138; // set our pipe back to defalt settings
  66.         display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
  67.         }
  68. }
  69.  
  70. // our characters arrays
  71. String character[] = {"*"}; // this array holds ourcharacters images
  72. int CharacterPos[] = {30, 20}; // our characters x and y positions
  73. void Controls() { // this functions checks for input from the controller
  74.     const int Speed = 3; // our characters speed
  75.     buttonState_up = digitalRead(buttonPin_up); // up button state
  76.     buttonState_left = digitalRead(buttonPin_left); // left button state
  77.     buttonState_right = digitalRead(buttonPin_right); // right button state
  78.     if (buttonState_up == HIGH && CharacterPos[1] > 5) { // up button detection
  79.         // This is our jump script
  80.         CharacterPos[1] = CharacterPos[1] - 7; // change the y position by - 1
  81.     }
  82.     if (buttonState_left == HIGH && CharacterPos[0] > 3) { // left button detection
  83.         //CharacterPos[0] = CharacterPos[0] - Speed; // change the x position by - speed
  84.     }
  85.     if (buttonState_right == HIGH && CharacterPos[0] < 120) { // right button detection
  86.         //CharacterPos[0] = CharacterPos[0] + Speed; // change the x position by + speed
  87.     }
  88. }
  89.  
  90. void Gravity() { // this function will control the gravitational forces of our character
  91.     // this is hard to do
  92.     if (CharacterPos[1] <= Platform1[1] - 10) { // if either condition is met, fall
  93.         CharacterPos[1] = CharacterPos[1] + 3; // apply gravity
  94.     }
  95. }
  96.  
  97. void DrawCharacter() { // this function will draw our character to the screen
  98.     if (CharacterPos[0] == Pipe1[0]) { // if the character jumped over a pipe, add 1 to the score
  99.         DrawScore(1); // add one to the score
  100.     }
  101.     OledWrite(CharacterPos[0], CharacterPos[1], 1, character[0]); // draw the character to the buffers
  102.     }
  103. void setup() { // our function that is loaded first
  104.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
  105.     display.clearDisplay(); // clear oled display from anything left over from last session
  106.     lcd.begin(16, 2); // define our lcd properties
  107.     lcd.clear(); // clear our lcd screen from any residue
  108.     LcdWrite(0, 0, "Flappy Asterik"); // display program name to lcd
  109.     //LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
  110. }
  111.  
  112.  
  113. void Debugging() { // this function display debugging info on the lcd display
  114.     lcd.setCursor(0, 1); // set our lcd cursor position
  115.     lcd.print("X: "); // debugging info
  116.     lcd.print(CharacterPos[0]); // debugging info
  117.     lcd.print(" Y: "); // debugging info
  118.     lcd.print(CharacterPos[1]); // debugging info
  119. }
  120. void loop() { // our looping function
  121.     display.clearDisplay(); // clear our oled display (removes ghosting)
  122.     Controls(); // check for input
  123.     DrawCharacter(); // draw our character to the buffer
  124.     DrawPlatforms(); // put our platforms into the oled buffer
  125.     DrawScore(0); // draw our score to the screen  
  126.     Gravity(); // run our gravity script
  127.     Debugging(); // display our debugging information
  128.     RenderScene(); // render our scene to the oled display
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement