Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Adafruit_GFX.h>
- #include <Adafruit_SharpMem.h>
- // any pins can be used
- #define SHARP_SCK 24
- #define SHARP_MOSI 23
- #define SHARP_SS 22
- #define IN01 A1
- #define IN02 A2
- #define IN03 A3
- #define IN04 A4
- #define IN05 A5
- #define IN06 0
- #define IN07 1
- #define IN08 11
- #define IN09 5
- #define IN10 21
- #define IN11 20
- #define LED 13
- #define VBATPIN 9
- // Set the size of the display here, e.g. 144x168!
- Adafruit_SharpMem display(SHARP_SCK, SHARP_MOSI, SHARP_SS, 144, 168);
- #define BLACK 0
- #define WHITE 1
- typedef struct ButtonStates {
- bool up, down, left, right;
- bool opt, start, menu;
- bool a, b, psi, omega;
- };
- ButtonStates buttonInput = {};
- const int FRAME_LENGTH = 1000 / 60.0;
- const int idleTimeSize = 3;
- int idleTime[idleTimeSize];
- int idleTimePos;
- ulong idleTimeLast = millis();
- float idleTimeUpdates = 1000;
- void setup() {
- Serial.begin(9600);
- Serial.println("Loading");
- display.begin();
- display.setRotation(3);
- display.clearDisplay();
- display.refresh();
- display.fillScreen(BLACK);
- display.refresh();
- display.clearDisplay();
- display.refresh();
- Serial.println("ScreenBlink");
- pinMode(IN01, INPUT);
- pinMode(IN02, INPUT);
- pinMode(IN03, INPUT);
- pinMode(IN04, INPUT);
- pinMode(IN05, INPUT);
- pinMode(IN06, INPUT);
- pinMode(IN07, INPUT);
- pinMode(IN08, INPUT);
- pinMode(IN09, INPUT);
- pinMode(IN10, INPUT);
- pinMode(IN11, INPUT);
- }
- void loop() {
- ulong startF = millis();
- readInput();
- display.startWrite();
- //display.clearDisplay();
- display.fillScreen(WHITE);
- frameLoop(buttonInput);
- overlay(buttonInput);
- display.endWrite();
- display.refresh();
- ulong endF = millis();
- int waitTime = FRAME_LENGTH - (endF - startF);
- if (waitTime > 0) {
- delay(waitTime);
- }
- if (idleTimeLast + idleTimeUpdates < endF) {
- idleTimePos = (idleTimePos + 1) % idleTimeSize;
- idleTime[idleTimePos] = 0;
- idleTimeLast = endF;
- }
- idleTime[idleTimePos] += waitTime;
- }
- void overlay(ButtonStates buttons){
- display.setTextSize(1);
- display.setCursor(0, 0);
- display.setTextSize(1);
- display.setTextColor(BLACK, WHITE);
- display.print( (int)((idleTimeUpdates - idleTime[(idleTimePos - 1) % idleTimeSize]) / idleTimeUpdates ));
- display.setCursor(110, 0);
- display.print("B:");
- display.print(batteryLevel());
- }
- void readInput() {
- buttonInput.up = digitalRead(IN08);
- buttonInput.down = digitalRead(IN11);
- buttonInput.left = digitalRead(IN10);
- buttonInput.right = digitalRead(IN09);
- buttonInput.opt = digitalRead(IN05);
- buttonInput.start = digitalRead(IN07);
- buttonInput.menu = digitalRead(IN06);
- buttonInput.a = digitalRead(IN01);
- buttonInput.b = digitalRead(IN03);
- buttonInput.psi = digitalRead(IN04);
- buttonInput.omega = digitalRead(IN02);
- }
- int gameMode = 0;
- void frameLoop(ButtonStates buttons)
- {
- if (buttons.menu)
- gameMode++;
- switch (gameMode) {
- case 0:
- buttonDisplay(buttons);
- break;
- case 1:
- pong(buttons);
- break;
- default:
- gameMode = 0;
- }
- }
- int ballX;
- int ballY;
- float ballXF;
- float ballYF;
- bool pongStarted = false;
- bool scored = false;
- int p1Y;
- int p2Y;
- int p1Points;
- int p2Points;
- bool bot = true;
- ulong endGameTime = 0;
- const int pongBatSize = 20;
- const int pongBatMove = 3;
- const int pongBallSize = 4;
- void pong(ButtonStates buttons)
- {
- if (buttons.opt) {
- bot = !bot;
- }
- if (buttons.start) {
- pongStarted = false;
- }
- if (!pongStarted) {
- randomSeed(analogRead(0));
- p1Points = 0;
- p2Points = 0;
- scored = true; // cheating?
- pongStarted = true;
- p1Y = display.height() / 2;
- p2Y = display.height() / 2 ;
- endGameTime = 0;
- }
- if (scored)
- {
- ballX = display.width() / 2;
- ballY = display.height() / 2;
- ballXF = 0;
- while (ballXF == 0) {
- ballXF = random(-4, 4);
- }
- ballYF = 0;
- while (ballYF == 0) {
- ballYF = random(-3, 3);
- }
- scored = false;
- }
- if (endGameTime) {
- display.setCursor(display.width() / 2 - 20, display.height() / 2 - 15);
- display.print("PLAYER");
- if (millis() > endGameTime + 1500) {
- display.setCursor(display.width() / 2 - 20, display.height() / 2);
- display.print(p1Points > p2Points ? "ONE" : "TWO");
- }
- if (millis() > endGameTime + 3000) {
- display.setCursor(display.width() / 2 - 20, display.height() / 2 + 15);
- display.print("WINS!");
- }
- } else {
- // Input
- if (buttons.up && p1Y - pongBatSize / 2 > 0) {
- p1Y -= pongBatMove;
- } else if (buttons.down && p1Y + pongBatSize / 2 < display.height()) {
- p1Y += pongBatMove;
- }
- if (bot) {
- if (ballY - pongBallSize > p2Y) {
- p2Y += 2;
- } else if (ballY + pongBallSize < p2Y) {
- p2Y -= 2;
- }
- } else {
- if (buttons.omega && p2Y - pongBatSize / 2 > 0) {
- p2Y -= pongBatMove;
- } else if (buttons.a && p2Y + pongBatSize / 2 < display.height()) {
- p2Y += pongBatMove;
- }
- }
- // Physics
- ballX += ballXF;
- ballY += ballYF;
- }
- // DRAW
- display.fillRect(0, p1Y - pongBatSize / 2, pongBallSize, pongBatSize, BLACK);
- display.fillRect(display.width() - pongBallSize, p2Y - pongBatSize / 2, pongBallSize, pongBatSize, BLACK);
- if (!endGameTime) {
- display.fillCircle(ballX, ballY, pongBallSize, BLACK);
- display.setTextSize(1);
- display.setTextColor(BLACK, WHITE);
- display.setCursor(display.width() / 2 - 20, display.height() / 2 - 10);
- display.print(p1Points);
- display.setCursor(display.width() / 2 + 20, display.height() / 2 - 10);
- display.print(p2Points);
- }
- // WALL
- if (ballY <= pongBallSize) {
- ballYF = -ballYF;
- if (ballYF < pongBallSize) {
- ballY = pongBallSize;
- }
- }
- if (ballY >= display.height() - pongBallSize) {
- ballYF = -ballYF;
- if (ballYF > display.height() - pongBallSize) {
- ballY = display.height() - pongBallSize;
- }
- }
- // Left side
- if (ballX < pongBallSize && ballXF < 0) {
- if (abs(p1Y - ballY) < pongBatSize / 2) {
- paddleBounce();
- } else {
- p2Points++;
- scored = true;
- }
- }
- // Right side
- if (ballX > display.width() - pongBallSize && ballXF > 0) {
- if (abs(p2Y - ballY) < pongBatSize / 2) {
- paddleBounce();
- } else {
- p1Points++;
- scored = true;
- }
- }
- if (max(p1Points, p2Points) >= 7 && !endGameTime) {
- endGameTime = millis();
- }
- }
- const double PADDLE_POSITION_ADDITION = PI / 2;
- const double PADDLE_MAX_ANGLE = PADDLE_POSITION_ADDITION + HALF_PI;
- const double PADDLE_BOUNCE_MAGIC = HALF_PI / PADDLE_MAX_ANGLE;
- void paddleBounce()
- {
- // reflection=((angle + (SQRT(ABS(offset))*offset*45))/135*90 +
- // (((sqrt(ABS(angle/90))*angle) + (SQRT(ABS(offset))*offset*45))/135*90)) / 2
- double incoming = atan2(ballYF, ballXF);
- double offset = (ballY - p1Y) / (double)pongBatSize;
- double offsetAngle = sqrt(abs(offset)) * offset * PADDLE_POSITION_ADDITION;
- double reflection = ( (incoming + offsetAngle) * PADDLE_BOUNCE_MAGIC + //
- (((sqrt(abs(incoming / HALF_PI)) * incoming) + offsetAngle) * PADDLE_BOUNCE_MAGIC)) / 2;
- double dist = sqrt(ballYF * ballYF + ballXF * ballXF);
- ballXF = dist * cos(reflection);
- ballYF = dist * sin(reflection);
- }
- void buttonDisplay(ButtonStates buttons)
- {
- if (buttons.up) {
- display.fillCircle(30, 30, 5, BLACK);
- } else {
- display.drawCircle(30, 30, 5, BLACK);
- }
- if (buttons.down) {
- display.fillCircle(30, 50, 5, BLACK);
- } else {
- display.drawCircle(30, 50, 5, BLACK);
- }
- if (buttons.left) {
- display.fillCircle(20, 40, 5, BLACK);
- } else {
- display.drawCircle(20, 40, 5, BLACK);
- }
- if (buttons.right) {
- display.fillCircle(40, 40, 5, BLACK);
- } else {
- display.drawCircle(40, 40, 5, BLACK);
- }
- if (buttons.opt) {
- display.fillCircle(60, 30, 5, BLACK);
- } else {
- display.drawCircle(60, 30, 5, BLACK);
- }
- if (buttons.menu) {
- display.fillCircle(60, 40, 5, BLACK);
- } else {
- display.drawCircle(60, 40, 5, BLACK);
- }
- if (buttons.start) {
- display.fillCircle(60, 50, 5, BLACK);
- } else {
- display.drawCircle(60, 50, 5, BLACK);
- }
- if (buttons.b) {
- display.fillCircle(100, 40, 5, BLACK);
- } else {
- display.drawCircle(100, 40, 5, BLACK);
- }
- if (buttons.a) {
- display.fillCircle(90, 50, 5, BLACK);
- } else {
- display.drawCircle(90, 50, 5, BLACK);
- }
- if (buttons.psi) {
- display.fillCircle(80, 40, 5, BLACK);
- } else {
- display.drawCircle(80, 40, 5, BLACK);
- }
- if (buttons.omega) {
- display.fillCircle(90, 30, 5, BLACK);
- } else {
- display.drawCircle(90, 30, 5, BLACK);
- }
- }
- float batteryLevel(void) {
- float measuredvbat = analogRead(VBATPIN);
- measuredvbat *= 2; // we divided by 2, so multiply back
- measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
- measuredvbat /= 1024; // convert to voltage
- return measuredvbat;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement