/* * Crappy-Bird * * This is a simple clone of the once popular Flappy Bird running on an ESP32 with a 128x64 px display * This program is free to use and modify. * Have Fun! * * Author: Johannes Marqaurt */ #include //publisher: Adafruit, version: 1.2.9 #include //publisher: Adafruit, version: 1.5.4 //Global variables (accessable in all functions, therefore should be avoided) Adafruit_SSD1306 display(128,64); //create display object (width and heigth) void setup() { // put your setup code here, to run once: display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x64) display.clearDisplay(); display.setTextColor(WHITE); //loading screen char title[] = "CrappyBird"; display.setCursor(0,24); display.print("by J.M."); display.setTextSize(2); display.setCursor(0,5); for(int i = 0; i < 11; i++) { display.print(title[i]); display.display(); delay(50); } display.setTextSize(1); delay(2000); const int buttonPin = 14; //the number of the pushbutton pin pinMode(buttonPin, INPUT_PULLUP); int score = 0; int gapSize = random(10, display.height() - 1); //obstacle gap size, randomized for increased game difficulty int gapPos = random(1, display.height() - gapSize - 1); //upper ycoord of the obstacle gap size, randomized for increased game difficulty int x_obstacle = display.width(); //position of the obstacle (initial: 128) int x_bird = 15; //x-position of bird (from left), should not change for game int y_bird = display.height()/2; //altitude of the bird (initial: 32 = 64/2) int bird_w = 10, bird_h = 5; //size of the bird (width, height) while(true) //could as well be put in void loop(){}... see explanation below { //drawing the picture display.clearDisplay(); display.setCursor(0,0); //Score position display.println(score); //Score display.drawRect(x_bird, y_bird, bird_w, bird_h, WHITE); //Bird display.fillRect(x_obstacle, 0, 5, gapPos, WHITE); //Upper obstacle display.fillRect(x_obstacle, gapPos + gapSize, 5, display.height() - (gapPos + gapSize), WHITE); //Lower obstacle display.display(); //do not forget this line. //calculating obstacle hit if((x_obstacle > x_bird) && (x_obstacle < (x_bird + bird_w))) //Bird and obstacles have the same x-position { if((y_bird(gapPos+gapSize)) //Bird is not in the gap { display.setCursor(30,12); display.print("Game Over"); display.display(); delay(1000); //Resetting score, obstacle and bird score = 0; x_obstacle = display.width(); gapSize = random(10, display.height() - 1); gapPos = random(1, display.height() - gapSize - 1); y_bird = display.height()/2; } } //calculating obstacle x_obstacle--; if(x_obstacle<0) //if obstacle reached left display frame, reset it to right, increase score { x_obstacle = display.width(); //resetting the obstacles' position to the far right gapSize = random(10, display.height() - 1); gapPos = random(1, display.height() - gapSize - 1); score++; //increasing score, when obstacle passed } //calculating bird bool buttonState = digitalRead(buttonPin); //reading input if (buttonState == true) { y_bird--; } else { y_bird++; } //preventing bird from escaping if(y_bird < 0) //bird on upper display frame { y_bird = 0; } if(y_bird > (display.height() - bird_h)) //bird on lower display frame { y_bird = display.height() - bird_h; } } } void loop() { // put your main code here, to run repeatedly: /* * Personally, i do not really like the concept of separated setup and loop in Arduino. * It makes programmers use a lot of global variables which should be avoided. (JM) */ }