One player pong with seven segment display for score keeping

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed wave_player

Fork of ECE2036Lab2StarterCode by Joseph Lind

Revision:
3:c93d1b51785c
Parent:
2:6163865f5ce3
--- a/main.cpp	Fri Jun 20 15:22:28 2014 +0000
+++ b/main.cpp	Thu Mar 17 20:38:26 2016 +0000
@@ -1,22 +1,36 @@
 #include "mbed.h"
 #include "PinDetect.h"
 #include "uLCD_4DGL.h"
-#include "Speaker.h"
+#include "wave_player.h"
+#include "SDFileSystem.h"
+#include "ball.h"
+#include "paddle.h"
+
+Serial pc(USBTX, USBRX); // tx, rx
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+AnalogOut DACout(p18);
+wave_player waver(&DACout);
+
+//Seven Segment Display
+DigitalOut a(p11);
+DigitalOut b(p12);
+DigitalOut c(p13);
+DigitalOut d(p14);
+DigitalOut e(p15);
+DigitalOut f(p16);
+DigitalOut g(p17);
+
+Ticker sevSegDisplay;
 
 // Pushbuttons
-PinDetect pbUp(p15); 
-PinDetect pbDown(p16);
+PinDetect pbUp(p23);//p15
+PinDetect pbDown(p24);//p16
 // uLCD
 uLCD_4DGL uLCD(p28, p27, p29);
-//Speaker
-Speaker mySpeaker(p21);
- 
-// Global variables needed for the push button interrupts
-int cornerX = 118, cornerY = 1;
-int oldCornerY = 1;
-int paddleMove = 8;
-int length = 40;
-int width = 3;
+//Ball
+Ball myBall;
+//Paddle
+Paddle myPaddle;
  
 // State machine definitions
 enum gameStateType {START, WAIT, GAME_SETUP, GAME, LOSE};
@@ -31,11 +45,51 @@
 // Global state machine variable (So that the pushbuttons can modify it)
 gameStateType gameState = START;
 
+void dScore()
+{   
+    switch(myPaddle.getScore()){
+        case 0://Zero digit
+            a=0;b=0;c=0;d=0;e=0;f =0;g=1;
+            break;        
+        case 1://One digit
+            b=0;c =0;
+            a=1;d=1;e=1;f=1;g = 1;
+            break;
+        case 2://Two digit
+            a=0;b=0;g=0;e=0;d =0;
+            f=1;c = 1;
+            break;
+        case 3://Three digit
+            e=1;f =1;
+            a=0;b=0;c=0;d=0;g = 0;
+            break;
+        case 4://Four digit
+            f=0;g=0;b=0;c =0;
+            a=1;d=1;e = 1;
+            break;
+        case 5://Five digit
+            b=1;e =1;
+            a=0;c=0;d=0;f=0;g = 0;
+            break;
+        case 6://Six digit
+            b =1;
+            a=0;c=0;d=0;e=0;f=0;g = 0;
+            break;
+        case 7://Seven digit
+            a=0;b=0;c =0;
+            d=1;e=1;f=1;g = 1;
+            break;
+        case 8://Eight digit
+            a=0;b=0;c=0;d=0;e=0;f=0;g = 0;
+            break;
+        case 9://Nine digit
+            e=0;d =0;
+            a=1;b=1;c=1;f=1;g = 1;
+            break;
+            }
+}
+
 // Pushbutton callbacks
-// WARNING: Do not call to draw anything to the uLCD in these
-// as this will cause the uLCD to crash sometimes. Update positions
-// and draw elsewhere (like it's done here).
-// Only modify the logic inside the callback functions.
 void pbUp_hit_callback (void)
 {
     switch (gameState)
@@ -44,9 +98,7 @@
         gameState = GAME_SETUP;
         break;
     case GAME:  
-        if(cornerY > paddleMove) {
-            cornerY -= paddleMove;
-        }
+        myPaddle.movePaddleUp();
         break;
     }
 }
@@ -59,9 +111,7 @@
         gameState = GAME_SETUP;
         break;
     case GAME:
-        if(cornerY < 127 - paddleMove - length){
-            cornerY += paddleMove;
-        }
+        myPaddle.movePaddleDown();
         break;
     }
 }
@@ -69,7 +119,6 @@
 int main() 
 {   
     // This is setting up the pushbuttons
-    // Don't modify this code.
     pbUp.mode(PullUp);
     pbDown.mode(PullUp);
     wait(0.1);
@@ -77,32 +126,45 @@
     pbDown.attach_deasserted(&pbDown_hit_callback);
     pbUp.setSampleFrequency();
     pbDown.setSampleFrequency();
-    // Don't modify this code.
-    
-    uLCD.display_control(PORTRAIT);
+ 
+    uLCD.display_control(PORTRAIT_R);
     uLCD.cls();
     uLCD.baudrate(BAUD_3000000);
     uLCD.background_color(BLACK);
-
-    // Initialize all your variables outside the while/switch statement
-    // to avoid compiler warning/errors
-    int vxSign = 1, vySign = 1;
-    float fx=50.0,fy=21.0,vx=1.6,vy=1.2;
-    int x=50, y=21, radius=5;
-    int score = 0;
+ 
     int i = 0;
-    int random;
- 
+    sevSegDisplay.attach(&dScore, 0.001);
+    
     while (1) 
     {   
         switch (gameState)
         {
         case START:
+            gameState = WAIT;
+            //Bouncing Ball Demo
+            float fx=50.0,fy=21.0,vx=1.3,vy=0.8;
+            int x=50,y=21,radius=4;
+            uLCD.background_color(BLACK);
             uLCD.cls();
-            uLCD.locate(0,0);
-            uLCD.printf("Pong!!!\n\n");
-            uLCD.printf("Press Key to Start");
-            gameState = WAIT;
+            //draw walls
+            uLCD.line(0, 0, 127, 0, WHITE);
+            uLCD.line(127, 0, 127, 127, WHITE);
+            uLCD.line(127, 127, 0, 127, WHITE);
+            uLCD.line(0, 127, 0, 0, WHITE);
+            while(gameState == WAIT) {//for (int i=0; i<1500; i++) {
+                //draw ball
+                uLCD.filled_circle(x, y, radius, RED);
+                //bounce off edge walls and slow down a bit?
+                if ((x<=radius+1) || (x>=126-radius)) vx = -vx;
+                if ((y<=radius+1) || (y>=126-radius)) vy = -vy;
+                //erase old ball location
+                uLCD.filled_circle(x, y, radius, BLACK);
+                //move ball
+                fx=fx+vx;
+                fy=fy+vy;
+                x=(int)fx;
+                y=(int)fy;
+            }
             break;
         case GAME_SETUP:
             uLCD.cls();
@@ -110,69 +172,23 @@
             uLCD.line(127, 0, 127, 127, 0xCFB53B);
             uLCD.line(127, 127, 0, 127, 0xCFB53B);
             uLCD.line(0, 127, 0, 0, 0xCFB53B);
-            vx = 1.6;
-            vy = 1.2;
-            srand(i);
-            random = (rand() % (118 - 2*radius)) + radius; 
-            fx = random;
-            random = (rand() % (127 - 2*radius)) + radius;
-            fy = random;
-            x=(int)fx; y=(int)fy;
-            random = rand() % 1;
-            vxSign=-1; vySign=((float)random - 0.5)*2;
-            uLCD.filled_rectangle(cornerX, cornerY, cornerX+width, cornerY+length, BLUE);
+            myBall.startPong( i, &uLCD );
+            myPaddle.initDraw( &uLCD );
             gameState = GAME;
             break;
         case GAME:
-            if ((fx+vxSign*vx<=radius+1)) 
-            {
-                vxSign = -vxSign;
-            }
-            if ((fy+vySign*vy<=radius+1) || (fy+vySign*vy>=126-radius)) 
-            {
-                vySign = -vySign;
-            }
-            if (((fx+vxSign*vx >= cornerX) && (fx+vxSign*vx <= cornerX+3)) && 
-                ((fy+vySign*vy>=cornerY) && (fy+vySign*vy<=cornerY+length))) 
-            {
-                vySign = -vySign;
-            }
-            if ((fx+vxSign*vx>=126-radius)) 
-            {
-                vx = 0;
-                vy = 0;
+            myBall.testConditions(&myPaddle, &uLCD);
+            if (myBall.getLose())
                 gameState = LOSE;
-            }
-            if ((fx+vxSign*vx>=cornerX-radius) && (fy+vySign*vy<=cornerY+length) && (fy+vySign*vy>=cornerY)) 
-            {
-                vxSign = -vxSign;
-                score++;
-                uLCD.locate(1,1);
-                uLCD.printf("%d", score);
-            }
-            uLCD.circle(x, y, radius, BLACK);
-            fx=fx+(vxSign*vx);
-            fy=fy+(vySign*vy);
-            x=(int)fx;
-            y=(int)fy;
-            uLCD.circle(x, y, radius, WHITE);
-            // We can assume that these for loops are quick enough that the paddle will move only one interval.
-            // These movements of the paddle have been optimized. Feel free to draw it out to see how it's been done.
-            if(oldCornerY > cornerY) {
-                uLCD.filled_rectangle(cornerX, oldCornerY-paddleMove+1, cornerX+width, oldCornerY, BLUE);
-                uLCD.filled_rectangle(cornerX, oldCornerY+length-paddleMove+1, cornerX+width, oldCornerY+length, BLACK);
-                oldCornerY = cornerY;
-            }
-            else if(oldCornerY < cornerY) {
-                uLCD.filled_rectangle(cornerX, oldCornerY, cornerX+width, oldCornerY+paddleMove, BLACK);
-                uLCD.filled_rectangle(cornerX, oldCornerY+length, cornerX+width, oldCornerY+length+paddleMove, BLUE);
-                oldCornerY = cornerY;
-            }
+            myBall.update( &uLCD );
+            myPaddle.redraw( &uLCD );
             break;
         case LOSE:
             uLCD.cls();
-            uLCD.printf("YOU LOSE D:");
-            score = 0;
+            //uLCD.printf("YOU LOSE D:");
+            myPaddle.resetScore();
+            myBall.setLose(0);
+            myBall.resetBall();
             wait(5.0);
             gameState = START;
             break;
@@ -181,5 +197,5 @@
             i++; 
             break;
         }
-    } 
+    }
 }
\ No newline at end of file