Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // some required libraries
- #include <LiquidCrystal.h> // lcd display
- #include <SPI.h> // communications
- #include <Wire.h> // i have no idea, but needed
- #include <Adafruit_GFX.h> // gfx library for oled
- #include <Adafruit_SSD1306.h> // oled display driver
- #define OLED_RESET 4 // oled reset code
- Adafruit_SSD1306 display(OLED_RESET); // oled reset
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // lcd setup
- // variables used for buttons
- const int buttonPin_left = 7; // left button pin
- const int buttonPin_up = 8; // up button pin
- const int buttonPin_right = 9; // right button pin
- int buttonState_up = 0; // up button state
- int buttonState_left = 0; // left button state
- int buttonState_right = 0; // right button state
- int Score = 0; // our scores variable
- int ScoreLock = 0; // locks our score counter
- // lets create some functions
- void LcdWrite(int x, int y, String text) { // this function will write to the lcd screen, this is not handled by RenderScene()
- lcd.setCursor(x, y); // set our lcd cursor position
- lcd.print(text); // write to the lcd screen
- }
- void OledWrite(int x, int y, int size, String text) { // this function will write to our oled
- display.setTextSize(size); // set the font size
- display.setTextColor(WHITE); // set the font color
- display.setCursor(x, y); // set the cursor position
- display.println(text); // add our text to the oled buffer
- }
- void RenderScene() { // this function will render our scene on the oled display
- display.display(); // update our oled (print buffer)
- }
- void DrawScore(int Add) { // this function will draw our score to screen
- Score = Score + Add; // add to our score
- OledWrite(100, 5, 1, String(Score)); // put our score into the oled buffer
- }
- // our platform arrays
- int Platform1[] = {0, 63, 128, 63}; // x1, y1, x2, y2
- int Pipe1[] = {128, 63, 128, 40}; // our first pipe
- int Pipe2[] = {128, 0, 128, 20}; // our second pipe
- int PipeSpeed = 2; // our pipes speed
- void DrawPlatforms() { // this function will add our platforms to the oled buffer
- /*
- Lets go into a little detail in this function:
- We use the drawLine() function to draw a line to the oled.
- the arguments are as follows:
- x1, y1, x2, y2, color
- Lets think of using a pen.
- x1 = the starting x position of the pens tip
- y1 = the starting y position of the pens tip
- x2 = the ending x position of the pens tip
- y2 = the ending y position of the pens tip
- so, between these coordinates, a line is drawn
- color = the color of the pen tip
- */
- display.drawLine(Platform1[0], Platform1[1], Platform1[2], Platform1[3], WHITE); // put our first platform into the buffer
- if (Pipe1[0] > 0) {
- Pipe1[0] = Pipe1[0] - PipeSpeed; // change our pipes x pos
- Pipe1[2] = Pipe1[2] - PipeSpeed; // change our pipes x pos
- display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
- } else {
- ScoreLock = 0; // unlock our score counter
- Pipe1[0] = 138; // set our pipe back to defalt settings
- Pipe1[2] = 138; // set our pipe back to defalt settings
- display.drawLine(Pipe1[0], Pipe1[1], Pipe1[2], Pipe1[3], WHITE); // draw our first pipe section
- }
- }
- // our characters arrays
- String character[] = {"*"}; // this array holds our characters images
- int CharacterPos[] = {10, 5}; // our characters x and y positions
- void Controls() { // this functions checks for input from the controller
- const int Speed = 3; // our characters speed
- buttonState_up = digitalRead(buttonPin_up); // up button state
- buttonState_left = digitalRead(buttonPin_left); // left button state
- buttonState_right = digitalRead(buttonPin_right); // right button state
- if (buttonState_up == HIGH && CharacterPos[1] > 5) { // up button detection
- // This is our jump script
- CharacterPos[1] = CharacterPos[1] - 7; // change the y position by - 1
- }
- if (buttonState_left == HIGH && CharacterPos[0] > 3) { // left button detection
- PipeSpeed = PipeSpeed - 1; // Slow our pipes down
- //CharacterPos[0] = CharacterPos[0] - Speed; // change the x position by - speed
- }
- if (buttonState_right == HIGH && CharacterPos[0] < 120) { // right button detection
- PipeSpeed = PipeSpeed + 1; // speed our pipes up
- //CharacterPos[0] = CharacterPos[0] + Speed; // change the x position by + speed
- }
- }
- void Gravity() { // this function will control the gravitational forces of our character
- // this is hard to do
- if (CharacterPos[1] <= Platform1[1] - 10) { // if either condition is met, fall
- CharacterPos[1] = CharacterPos[1] + 3; // apply gravity
- }
- }
- void DrawCharacter() { // this function will draw our character to the screen
- if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] < Pipe1[3] && ScoreLock == 0) { // if the character jumped over a pipe, add 1 to the score
- DrawScore(1); // add one to the score
- analogWrite(6, 100); // buzz the buzzer
- delay(100); // delay a few ms
- analogWrite(6, 0); // turn the buzzer off
- ScoreLock = 1; // lock our score counter
- }
- OledWrite(CharacterPos[0], CharacterPos[1], 1, character[0]); // draw the character to the buffers
- }
- void EndGame() { // this function will end our game
- analogWrite(6, 50); // buzz the buzzer
- delay(200); // delay a few ms
- analogWrite(6, 0); // turn the buzzer off
- delay(200); // delay a few ms
- analogWrite(6, 50); // buzz the buzzer
- delay(200); // delay a few ms
- analogWrite(6, 0); // turn the buzzer off
- asm volatile ("jmp 0"); // restart the arduino
- }
- void CheckCollision() { // this function will check for collisions between the character and pipes
- if (CharacterPos[0] >= Pipe1[0] && CharacterPos[1] > Pipe1[3]) { // if a collision is detected, end the game
- //OledWrite(45, 30, 2, "FAIL"); // display end game text
- EndGame(); // end the game
- }
- }
- void Debugging() { // this function displays debugging info on the lcd display
- lcd.setCursor(0, 1); // set our lcd cursor position
- lcd.print("X: "); // debugging info
- lcd.print(CharacterPos[0]); // debugging info
- lcd.print(" Y: "); // debugging info
- lcd.print(CharacterPos[1]); // debugging info
- }
- void setup() { // our function that is loaded first
- pinMode(6, OUTPUT); // our buzzer on pin 6
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // lets begin our oled display/connection
- display.clearDisplay(); // clear oled display from anything left over from last session
- lcd.begin(16, 2); // define our lcd properties
- lcd.clear(); // clear our lcd screen from any residue
- LcdWrite(0, 0, "Flappy Asterik"); // display program name to lcd
- //LcdWrite(0, 1, "By Dan Jones"); // display creator to lcd
- }
- void loop() { // our looping function
- display.clearDisplay(); // clear our oled display (removes ghosting)
- Controls(); // check for input
- DrawCharacter(); // draw our character to the buffer
- DrawPlatforms(); // put our platforms into the oled buffer
- DrawScore(0); // draw our score to the screen
- Gravity(); // run our gravity script
- CheckCollision(); // check for collisions with pipes
- Debugging(); // display our debugging information
- RenderScene(); // render our scene to the oled display
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement