//Libraries in use
#include <Adafruit_GFX.h> //used for Core Graphics Library when using the display
#include <Adafruit_SSD1306.h> //used for Lower Level Graphic Library when using the specific display SSD 1306
//figures in byte arrays (that originally were saved as a PNG (-image)file and got converted to byte array form)
// 'Torro_Logo', 128x64px (X-axis x Y-axis in pixel)
const unsigned char bitmap_Torro_Logo [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x03, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x03, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x01, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xbc, 0x00, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x7c, 0xff, 0xff, 0xff, 0xff, 0x3e, 0x00, 0x7f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x1f, 0x7f, 0xff, 0xff, 0xfe, 0xf8, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x0f, 0x7f, 0xff, 0xff, 0xfe, 0xf0, 0x00, 0x1f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x01, 0xe7, 0xbf, 0xff, 0xff, 0xfd, 0xe7, 0x80, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xf3, 0xbf, 0xff, 0xff, 0xfd, 0xcf, 0xe0, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xfc, 0xbf, 0xff, 0xff, 0xfd, 0x3f, 0xf0, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x1f, 0xff, 0x3f, 0xff, 0xff, 0xfc, 0xff, 0xf8, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x3f, 0xff, 0xf8, 0x3f, 0xfe, 0x0f, 0xff, 0xfc, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x1f, 0xfe, 0x00, 0x1f, 0xf8, 0x00, 0x7f, 0xf8, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x0f, 0xf0, 0x00, 0x07, 0xe0, 0x00, 0x3f, 0xf0, 0x07, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x07, 0xe0, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x20, 0x00, 0x00, 0x02, 0x00, 0x00, 0x1f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0x80, 0x00, 0xfe, 0x00, 0x00, 0x3f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x01, 0xe0, 0x03, 0xc0, 0x00, 0x00, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x07, 0xf8, 0x0f, 0xf0, 0x00, 0x01, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x03, 0xfc, 0x1f, 0xe0, 0x00, 0x03, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x67, 0x73, 0x00, 0x00, 0x1f, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x24, 0x12, 0x00, 0x00, 0x7f, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x1c, 0x1c, 0x00, 0x00, 0xff, 0xff, 0xff,
0xf5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x00, 0x0c, 0x18, 0x00, 0x03, 0xff, 0xff, 0xff,
0xf5, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x00, 0x04, 0x10, 0x00, 0x07, 0xff, 0xff, 0xff,
0xf5, 0xc3, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x04, 0x10, 0x00, 0x1f, 0xff, 0xff, 0xff,
0xf5, 0xdb, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xdf, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
0xff, 0xdf, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xc7, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xdf, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x84, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xdf, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0xc6, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xdb, 0xbf, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0xc6, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xc3, 0x9f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x20, 0x01, 0xc7, 0x01, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x20, 0x00, 0xc6, 0x01, 0xff, 0xff, 0xff, 0xff,
0xe0, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff, 0x30, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x10, 0x00, 0x00, 0x07, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xd8, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xeb, 0xcc, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xeb, 0xe0, 0x0c, 0x00, 0x3f, 0xff, 0xff, 0xff, 0xff,
0xf9, 0xf3, 0xff, 0xbf, 0xff, 0xbf, 0xff, 0xeb, 0xf0, 0x0f, 0x00, 0x7c, 0xff, 0xff, 0xff, 0xff,
0xf0, 0xe1, 0xff, 0xbf, 0xff, 0xbf, 0xff, 0xeb, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x87, 0xff, 0xff,
0xf6, 0xed, 0xc7, 0x0c, 0x78, 0x38, 0x74, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0x33, 0xff, 0xff,
0xf6, 0xed, 0xbb, 0xbb, 0xb3, 0x33, 0xb1, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xf9, 0xff, 0xff,
0xf6, 0xed, 0xfb, 0xbf, 0xb7, 0xb7, 0xb7, 0xff, 0xff, 0xff, 0xfe, 0xff, 0x9f, 0xfd, 0xff, 0xff,
0xf7, 0x5d, 0x83, 0xbc, 0x37, 0xb7, 0xb7, 0xff, 0xff, 0xff, 0xff, 0x7f, 0xcf, 0xfd, 0xff, 0xff,
0xf7, 0x1d, 0xbb, 0xbb, 0xb7, 0xb7, 0xb7, 0xff, 0xff, 0xff, 0xff, 0x3f, 0xe7, 0xf9, 0xff, 0xff,
0xf7, 0xbd, 0xbb, 0xbb, 0xb3, 0x37, 0xb7, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xf3, 0xfb, 0xff, 0xff,
0xf7, 0xbd, 0xc3, 0x8c, 0x38, 0xb8, 0x77, 0xff, 0xff, 0xff, 0xff, 0xdf, 0xf9, 0xe3, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xce, 0xfc, 0x0f, 0xff, 0xff,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xfe, 0xee, 0x7f, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xef, 0x37, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xe7, 0xb1, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0xf7, 0xdc, 0xff, 0x0f, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfb, 0xf7, 0xcf, 0x3f, 0x67, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfd, 0xe7, 0xe7, 0x8f, 0xf7, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xcf, 0xf7, 0xe3, 0xe7, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1e, 0x72, 0x78, 0x0f, 0xff };
// 'Torro', 17x10px (X-axis x Y-axis in pixel)
const unsigned char bitmap_Torro [] PROGMEM = {
0x0f, 0xfc, 0x00, 0x3f, 0xfe, 0x00, 0x7f, 0xff, 0x00, 0x00, 0x00, 0x00, 0x06, 0x30, 0x00, 0xc7,
0x71, 0x80, 0xf3, 0x67, 0x80, 0xf8, 0x0f, 0x80, 0xf9, 0x4f, 0x80, 0xfc, 0x0f, 0x80 };
// 'Mann', 10x10px (X-axis x Y-axis in pixel)
const unsigned char bitmap_Mann [] PROGMEM = {
0xe1, 0xc0, 0xc0, 0xc0, 0x61, 0x80, 0x73, 0x80, 0x00, 0x00, 0xf3, 0xc0, 0xf3, 0xc0, 0xc0, 0xc0,
0xde, 0xc0, 0x1e, 0x00 };
// 'Mann2', 10x10px (X-axis x Y-axis in pixel)
const unsigned char bitmap_Mann2 [] PROGMEM = {
0xe3, 0xc0, 0x61, 0x80, 0x63, 0x80, 0xb7, 0x40, 0xc0, 0xc0, 0xe1, 0xc0, 0xe1, 0xc0, 0xed, 0xc0,
0xed, 0xc0, 0xe4, 0xc0 };
// 'Herz', 10x10px (X-axis x Y-axis in pixel)
const unsigned char bitmap_Herz [] PROGMEM = {
0xcc, 0xc0, 0xb3, 0x40, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0x7f, 0x80, 0xbf, 0x40, 0xde, 0xc0,
0xed, 0xc0, 0xf3, 0xc0 };
//score, lives and gameState
unsigned int score = 0; //game start value of score
unsigned int lives = 3; //game start value of lives
int gameState = LOW; //might not be used
//joystick
#define VERT_PIN A2 //vertical movement of the joystick
#define HORZ_PIN A3 //horizontal movement of the joystick
#define SEL_PIN 2 //pushbutton, to start the game (not yet programmed)
//Display
#define SCREEN_I2C_ADDR 0x3c //0x3c, corresponding I2C address for display
#define SCREEN_WIDTH 128 //OLED display width, in pixels
#define SCREEN_HEIGHT 64 //OLED display height, in pixels
#define OLED_RST_PIN -1 //Reset pin (-1 if not available)
//figures variables/values
// 'Torro_Logo':
int xTorro_Logo = 0;
int yTorro_Logo = 0;
// 'The Player - Torro':
float xTorro = 64;
float yTorro =53;
int wTorro = 17;
int hTorro = 10;
// 'Falling Herz':
int xHerz = random(0,111);
float yHerz = 0;
int wHerz = 10;
int hHerz = 10;
// 'Falling Mann':
int xMann = random(0,111);
float yMann = 0;
int wMann = 10;
int hMann = 10;
// 'Falling Mann2':
int xMann2 = random(0,111);
float yMann2 = 0;
int wMann2 = 10;
int hMann2 = 10;
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RST_PIN); //function to active the screen
//Adafruit_SSD1306 display (var for width, var for height, &Wire, Restart pin OR -1)
void setup ()
{//BEGINNING OF VOID SETUP FUNCTION
Serial.begin(9600); //function to start communication with interface monitor
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //function to begin the specific deisplay type
display.clearDisplay(); //function to clear display, to start clean
delay(1000); // optional for simulations not necessary
//Intro (distplay logo and written text)
display.drawBitmap(xTorro_Logo ,yTorro_Logo ,bitmap_Torro_Logo , 128, 64, bitmap_Torro_Logo, WHITE);
display.display(); //function to display written code
delay(2500);
display.clearDisplay();
// Display static text
display.setTextSize(2); //function to declare text size
display.setTextColor(WHITE); //function to choose text color
display.setCursor(0, 10); //function to declare position of text
display.println("Game Time!"); //function to print string followed by with change of lines
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 45);
display.println("Ready?!");
display.display ();
delay(2000);
//declaration of joystick pins/switch button
pinMode(VERT_PIN, INPUT); // pinMode (variable name of analog pin, INPUT or OUTPUT)
analogWrite(VERT_PIN, LOW); //analogWrite (variable name of analog pin, LOW or HIGH)
pinMode(HORZ_PIN, INPUT);
analogWrite(HORZ_PIN, LOW);
pinMode(SEL_PIN, INPUT);
digitalWrite(SEL_PIN, LOW); //digitalWrite (variable name of digital pin, LOW or HIGH)
//randomize number
randomSeed(analogRead(0)); //setup to generate random number, only if A0 is not connected
}//END OF VOID SETUP FUNCTION
void loop()
{//BEGINNING OF VOID LOOP FUNCTION
display.clearDisplay ();
// Check Game Over
if (lives <= 0)
{gameOver();
return;}
// calling functions
score_lives();
joystickControl();
fall();
CollisionMann ();
CollisionMann2 ();
CollisionHerz ();
display.display();
}//END OF VOID LOOP FUNCTION
void score_lives ()
{//BEGINNING OF VOID SCORE_LIVES FUNCTION
//Display Score and Lives
display.setTextSize(0);
display.setTextColor(WHITE);
display.setCursor(0, 0); //declare position where the following should be printed to on the display
display.print("Score: "); //print onto display the string "Score: "
display.print(score); //print onto display the variable "score"
display.setCursor(70, 0);
display.print("Lives: ");
display.print(lives);
}//END OF VOID SCORE_LIVES FUNCTION
void joystickControl()
{// BEGINNING OF VOID JOYSTICKCONTROL FUNCTION
// reading of the joystick (current on pin)
display.drawBitmap(xTorro ,yTorro ,bitmap_Torro , 17, 10, bitmap_Torro, WHITE);
int joystickV = map(analogRead(VERT_PIN), 0,1023,-1,1);
int joystickH = map(analogRead(HORZ_PIN), 0,1023, -1, 1);
bool currSelPressed = digitalRead(SEL_PIN);
// controlling of the Torro
/*switch (currSelPressed) //switch-function gameState HIGH or LOW ------> NOT YET PROGRAMMED AND OPERATIONAL
{case HIGH:
if (gameState != currSelPressed)
{gameState = HIGH;}
break;
case LOW:
if (gameState == currSelPressed)
{gameState = LOW;}
break;}*/
switch (joystickV) //switch-function for up/down movement
{case 1: //hoch
if( 50 < yTorro)
{yTorro-=5;}
break;
case -1: //runter
if (yTorro < 54)
{yTorro+=5;}
break; }
switch (joystickH) //switch-function for right/left movement
{case 1: //links
if (0 < xTorro)
{xTorro-=5;}
break;
case -1: //rechts
if (xTorro < 111)
{xTorro+=5;}
break;}
}//END OF VOID JOYSTICKCONTROL FUNCTION
void fall ()
{// BEGINNING OF VOID FALL FUNCTION
// Figure: Mann fall
display.drawBitmap(xMann,yMann,bitmap_Mann, 10, 10, bitmap_Mann, WHITE);
if (yMann > 54) // fall until catch or game over
{yMann = 0;}
if (yMann == 0) // start a new fall
{xMann = random(0,111);}
yMann += (1);
// Figure: Mann2 fall
display.drawBitmap(xMann2,yMann2,bitmap_Mann2, 10, 10, bitmap_Mann2, WHITE);
if (yMann2 > 250) // fall until catch or game over
{yMann2 = 0;}
if (yMann2 == 0) // start a new fall
{xMann2 = random(20,91);}
yMann2 += (1.5);
// Figure: Herz fall
display.drawBitmap(xHerz,yHerz,bitmap_Herz, 10, 10, bitmap_Herz, WHITE);
if (yHerz > 500) // fall until catch or game over
{yHerz = 0;}
if (yHerz == 0) // start a new fall
{xHerz = random(0,111);}
yHerz+= (2);
}// END OF VOID FALL FUNCTION
boolean CollisionMann()
{//BEGINNING OF BOOLEAN COLLISIONMANN FUNCTION
if (xTorro + wTorro >= xMann && // r1 right edge past r2 left
xTorro <= xMann + wMann && // r1 left edge past r2 right
yTorro + hTorro >= yMann && // r1 top edge past r2 bottom
yTorro <= yMann + hMann)
{score++;
yMann = 0;
return true;
}
else
if (yMann == 54)
{lives--;}
return false;
}// END OF BOOLEAN COLLISIONMANN FUNCTION
boolean CollisionMann2()
{//BEGINNING OF BOOLEAN COLLISIONMANN2 FUNCTION
if (xTorro + wTorro >= xMann2 && // r1 right edge past r2 left
xTorro <= xMann2 + wMann2 && // r1 left edge past r2 right
yTorro + hTorro >= yMann2 && // r1 top edge past r2 bottom
yTorro <= yMann2 + hMann2)
{score++;
yMann2 = 0;
return true;
}
else
if (yMann2 == 54)
{lives--;}
return false;
}// END OF BOOLEAN COLLISIONMANN2 FUNCTION
boolean CollisionHerz ()
{// BEGINNING OF BOOLEAN COLLISION HERZ FUNCTION
if (xTorro + wTorro >= xHerz && // r1 right edge past r2 left
xTorro <= xHerz + wHerz && // r1 left edge past r2 right
yTorro + hTorro >= yHerz && // r1 top edge past r2 bottom
yTorro <= yHerz + hHerz)
{lives++;
yHerz = 0;
return true;
}
return false;
}// END OF BOOLEAN COLLISION HERZ FUNCTION
void gameOver()
{// BEGINNING OF VOID GAMEOVER FUNCTION
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 20);
display.println("Game Over!");
display.setTextSize(1);
display.setCursor(20, 40);
display.print("Final Score: ");
display.print(score);
display.display();
delay(5000); // Wait before restarting
lives = 3; // Reset lives
score = 0; // Reset score
yMann = yHerz = 0; // Reset positions
}// END OF VOID GAMEOVER FUNCTION
// END OF GAME CODE