Joseph Lind / Mbed 2 deprecated ECE2036Lab2StarterCode

Dependencies:   4DGL-uLCD-SE PinDetect mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 #include "uLCD_4DGL.h"
00004 #include "Speaker.h"
00005 
00006 // Pushbuttons
00007 PinDetect pbUp(p15); 
00008 PinDetect pbDown(p16);
00009 // uLCD
00010 uLCD_4DGL uLCD(p28, p27, p29);
00011 //Speaker
00012 Speaker mySpeaker(p21);
00013  
00014 // Global variables needed for the push button interrupts
00015 int cornerX = 118, cornerY = 1;
00016 int oldCornerY = 1;
00017 int paddleMove = 8;
00018 int length = 40;
00019 int width = 3;
00020  
00021 // State machine definitions
00022 enum gameStateType {START, WAIT, GAME_SETUP, GAME, LOSE};
00023 /* State Definitions:
00024  * START -- Creates the start screen
00025  * WAIT -- After the start screen, goes into wait where mbed spins and does nothing
00026  * GAME_SETUP -- Sets up one time things (like boarders, initializes beginning velocity
00027  * GAME -- When the user actually gets to play
00028  * LOSE -- clears the screen, prints you lose, waits, then goes back to start
00029  */
00030  
00031 // Global state machine variable (So that the pushbuttons can modify it)
00032 gameStateType gameState = START;
00033 
00034 // Pushbutton callbacks
00035 // WARNING: Do not call to draw anything to the uLCD in these
00036 // as this will cause the uLCD to crash sometimes. Update positions
00037 // and draw elsewhere (like it's done here).
00038 // Only modify the logic inside the callback functions.
00039 void pbUp_hit_callback (void)
00040 {
00041     switch (gameState)
00042     {
00043     case WAIT:
00044         gameState = GAME_SETUP;
00045         break;
00046     case GAME:  
00047         if(cornerY > paddleMove) {
00048             cornerY -= paddleMove;
00049         }
00050         break;
00051     }
00052 }
00053  
00054 void pbDown_hit_callback (void)
00055 {
00056     switch (gameState)
00057     {
00058     case WAIT:
00059         gameState = GAME_SETUP;
00060         break;
00061     case GAME:
00062         if(cornerY < 127 - paddleMove - length){
00063             cornerY += paddleMove;
00064         }
00065         break;
00066     }
00067 }
00068  
00069 int main() 
00070 {   
00071     // This is setting up the pushbuttons
00072     // Don't modify this code.
00073     pbUp.mode(PullUp);
00074     pbDown.mode(PullUp);
00075     wait(0.1);
00076     pbUp.attach_deasserted(&pbUp_hit_callback);
00077     pbDown.attach_deasserted(&pbDown_hit_callback);
00078     pbUp.setSampleFrequency();
00079     pbDown.setSampleFrequency();
00080     // Don't modify this code.
00081     
00082     uLCD.display_control(PORTRAIT);
00083     uLCD.cls();
00084     uLCD.baudrate(BAUD_3000000);
00085     uLCD.background_color(BLACK);
00086 
00087     // Initialize all your variables outside the while/switch statement
00088     // to avoid compiler warning/errors
00089     int vxSign = 1, vySign = 1;
00090     float fx=50.0,fy=21.0,vx=1.6,vy=1.2;
00091     int x=50, y=21, radius=5;
00092     int score = 0;
00093     int i = 0;
00094     int random;
00095  
00096     while (1) 
00097     {   
00098         switch (gameState)
00099         {
00100         case START:
00101             uLCD.cls();
00102             uLCD.locate(0,0);
00103             uLCD.printf("Pong!!!\n\n");
00104             uLCD.printf("Press Key to Start");
00105             gameState = WAIT;
00106             break;
00107         case GAME_SETUP:
00108             uLCD.cls();
00109             uLCD.line(0, 0, 127, 0, 0xCFB53B);
00110             uLCD.line(127, 0, 127, 127, 0xCFB53B);
00111             uLCD.line(127, 127, 0, 127, 0xCFB53B);
00112             uLCD.line(0, 127, 0, 0, 0xCFB53B);
00113             vx = 1.6;
00114             vy = 1.2;
00115             srand(i);
00116             random = (rand() % (118 - 2*radius)) + radius; 
00117             fx = random;
00118             random = (rand() % (127 - 2*radius)) + radius;
00119             fy = random;
00120             x=(int)fx; y=(int)fy;
00121             random = rand() % 1;
00122             vxSign=-1; vySign=((float)random - 0.5)*2;
00123             uLCD.filled_rectangle(cornerX, cornerY, cornerX+width, cornerY+length, BLUE);
00124             gameState = GAME;
00125             break;
00126         case GAME:
00127             if ((fx+vxSign*vx<=radius+1)) 
00128             {
00129                 vxSign = -vxSign;
00130             }
00131             if ((fy+vySign*vy<=radius+1) || (fy+vySign*vy>=126-radius)) 
00132             {
00133                 vySign = -vySign;
00134             }
00135             if (((fx+vxSign*vx >= cornerX) && (fx+vxSign*vx <= cornerX+3)) && 
00136                 ((fy+vySign*vy>=cornerY) && (fy+vySign*vy<=cornerY+length))) 
00137             {
00138                 vySign = -vySign;
00139             }
00140             if ((fx+vxSign*vx>=126-radius)) 
00141             {
00142                 vx = 0;
00143                 vy = 0;
00144                 gameState = LOSE;
00145             }
00146             if ((fx+vxSign*vx>=cornerX-radius) && (fy+vySign*vy<=cornerY+length) && (fy+vySign*vy>=cornerY)) 
00147             {
00148                 vxSign = -vxSign;
00149                 score++;
00150                 uLCD.locate(1,1);
00151                 uLCD.printf("%d", score);
00152             }
00153             uLCD.circle(x, y, radius, BLACK);
00154             fx=fx+(vxSign*vx);
00155             fy=fy+(vySign*vy);
00156             x=(int)fx;
00157             y=(int)fy;
00158             uLCD.circle(x, y, radius, WHITE);
00159             // We can assume that these for loops are quick enough that the paddle will move only one interval.
00160             // These movements of the paddle have been optimized. Feel free to draw it out to see how it's been done.
00161             if(oldCornerY > cornerY) {
00162                 uLCD.filled_rectangle(cornerX, oldCornerY-paddleMove+1, cornerX+width, oldCornerY, BLUE);
00163                 uLCD.filled_rectangle(cornerX, oldCornerY+length-paddleMove+1, cornerX+width, oldCornerY+length, BLACK);
00164                 oldCornerY = cornerY;
00165             }
00166             else if(oldCornerY < cornerY) {
00167                 uLCD.filled_rectangle(cornerX, oldCornerY, cornerX+width, oldCornerY+paddleMove, BLACK);
00168                 uLCD.filled_rectangle(cornerX, oldCornerY+length, cornerX+width, oldCornerY+length+paddleMove, BLUE);
00169                 oldCornerY = cornerY;
00170             }
00171             break;
00172         case LOSE:
00173             uLCD.cls();
00174             uLCD.printf("YOU LOSE D:");
00175             score = 0;
00176             wait(5.0);
00177             gameState = START;
00178             break;
00179         case WAIT:
00180             // Used to seed the rand() function so we don't get the same starting position every time.
00181             i++; 
00182             break;
00183         }
00184     } 
00185 }