Advertisement
BeamNG_IRC

Untitled

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