Code for Player 1 of Rock Paper Scissors Game
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
Revision 3:154b62d481b7, committed 2015-10-23
- Comitter:
- pkulkarni34
- Date:
- Fri Oct 23 02:51:31 2015 +0000
- Parent:
- 2:d6ccc8dae85b
- Commit message:
- Rock Paper Scissors - Player 1
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r d6ccc8dae85b -r 154b62d481b7 main.cpp --- a/main.cpp Thu Oct 22 01:16:27 2015 +0000 +++ b/main.cpp Fri Oct 23 02:51:31 2015 +0000 @@ -1,33 +1,29 @@ -//ECE 4180 Mini Project + //ECE 4180 Mini Project //Prachi Kulkarni and Kendra Dodson +// This code is for Player 1 + #include "mbed.h" #include "uLCD_4DGL.h" #include "SDFileSystem.h" #include "wave_player.h" -//#include "beep.h" +//Leds to show status of sending and receiving messages DigitalOut myled(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); +//SD Card Reader SDFileSystem sd(p5, p6, p7, p8, "sd"); -//AnalogOut DACout(p18); -//Beep DACout(p18); AnalogOut DACout(p18); wave_player waver(&DACout); // Wave Player -//DigitalOut myled(LED1); uLCD_4DGL lcd(p28, p27, p29); //uLCD -DigitalIn pb1(p17); //Pushbutton 1 -DigitalIn pb2(p19); //Pushbutton 2 -DigitalIn pb3(p20); //Pushbutton 3 +DigitalIn pb1(p17); //Pushbutton 1 - Rock +DigitalIn pb2(p19); //Pushbutton 2 - Paper +DigitalIn pb3(p20); //Pushbutton 3 - Scissors -//PwmOut led1(p21); //led 1 -//PwmOut led2(p22); //led2 -//PwmOut led3(p23); //led3 - -//shiftbrite +//Shiftbrite DigitalOut latch(p15); DigitalOut enable(p16); SPI spi(p11, p12, p13); @@ -57,6 +53,7 @@ latch=0; } int i; + //Serial Serial player1(p9,p10); @@ -64,7 +61,6 @@ //Serial player1.baud(9600); - spi.format(16,0); spi.frequency(500000); enable=0; @@ -77,7 +73,6 @@ wait(0.001); //Display - lcd.locate(1,2); //Screen 1 shows the name of the game lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -88,7 +83,6 @@ lcd.printf(" Rock\n"); lcd.printf(" Paper\n\n"); lcd.printf(" Scissor\n\n"); - //DACout.beep(1000,0.5); wait(5); //Screen 2 shows pushbutton options @@ -152,6 +146,7 @@ red = red + 5; lcd.printf("%d",i); //Display time left on LCD wait(1); + //Press pushbutton 1 to choose rock if(pb1 != 1) { @@ -205,7 +200,8 @@ lcd.rectangle(0,0,125,125,WHITE); //Draw white border lcd.locate(0,1); lcd.printf("You chose scissors"); - //Play button select + + //Play button select FILE *wave_file; printf("\n\n\nHello, wave world!\n"); wave_file=fopen("/sd/mydir/select.wav","r"); @@ -253,162 +249,15 @@ //-------------------------------------------------------------------------------------------------------------------------------------- - - - // lcd.cls(); //Clear screen - // wait(2); - - /* - //-------------------------------------------------------------------------------------------------------------------------------- - //------------------------------- PLAYER 2 -------------------------------------------------------------------------------------- - //------------------------------------------------------------------------------------------------------------------------------- - - //Check to see if Player 2 is ready to play - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.locate(0,5); - lcd.printf(" Press 1 if P2\n\n is ready"); - - //Start timer for player 2 if player 2 presses pushbutton 1 - while(1){ - if(pb1 != 1) - { //Play button select - FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/select.wav","r"); - waver.play(wave_file); - - printf("ok!!"); - fclose(wave_file); - break; - } - } - lcd.cls(); - wait(1); - - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - //Tell player 1 that she/he has 5 seconds to chose between rock,paper and scissors - lcd.locate(0,4); - lcd.color(BLUE); //Set font color to yellow - lcd.printf(" Player 2 \n\n\n"); - lcd.locate(0,6); - lcd.color(GREEN); //Set font color to green - lcd.printf("You have 5 seconds"); - lcd.printf(" to choose\n"); - lcd.printf(" an option!\n"); - wait(4); - - - - - int j; - int blue = 1; - for(j = 5; j > -1 ; j--){ - - lcd.cls(); - lcd.locate(4,4); - lcd.text_width(8); - lcd.text_height(8); - RGB_LED(0,0,blue*25); - blue = blue + 5; - lcd.printf("%d",j); - wait(1); - - //Rock - if(pb1 != 1) { - lcd.cls(); - choice2 = 1; - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.locate(0,1); - lcd.printf(" You chose rock!"); - //Play button select - FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/select.wav","r"); - waver.play(wave_file); - - printf("ok!!"); - fclose(wave_file); - lcd.circle(60, 60, 30,WHITE); - wait(3); - lcd.cls(); - break; - } - //Paper - if(pb2 != 1) { - lcd.cls(); - choice2 = 2; - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.locate(0,1); - lcd.printf(" You chose paper"); - //Play button select - FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/select.wav","r"); - waver.play(wave_file); - - printf("ok!!"); - fclose(wave_file); - lcd.filled_rectangle(50, 45, 80,90,WHITE); - wait(3); - lcd.cls(); - break; - } - //Scissors - if(pb3 != 1) { - lcd.cls(); - choice2 = 3; - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.locate(0,1); - lcd.printf("You chose scissors"); - //Play button select - FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/select.wav","r"); - waver.play(wave_file); - - printf("ok!!"); - fclose(wave_file); - lcd.circle(50, 60, 10,WHITE); - lcd.circle(50, 80, 10,WHITE); - lcd.line(62, 61, 90, 85, WHITE); - lcd.line(62, 81, 90, 55, WHITE); - wait(3); - lcd.cls(); - break; - } - } - -//---------------------------------------------------------------------------------------------------------------------------------------- -// TIME-OUT -//---------------------------------------------------------------------------------------------------------------------------------------- - //Time out message - if(j == -1){ - lcd.cls(); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.locate(0,5); - lcd.color(RED); - lcd.printf(" Your time is up!"); - lcd.color(GREEN); - //Play time up tune - FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/timeup.wav","r"); - waver.play(wave_file); - - printf("ok!!"); - fclose(wave_file); - wait(3); - goto L1; - } - RGB_LED(0,0,0); - - */ - + //Send Player 1's choice to Player 2 player1.printf("%c",choice1); myled2 = 1; wait(1); + //Receive Player 2's choice while(1){ + lcd.locate(1,4); + lcd.printf(" Waiting for \n the other player"); if(player1.readable()){ choice2 = player1.getc(); myled = 1; @@ -429,23 +278,7 @@ lcd.locate(1,2); lcd.printf(" It's a draw!"); wait(4); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - } - */ + } //Case 2: //Player 1 : Rock @@ -463,24 +296,7 @@ lcd.filled_rectangle(50, 60, 80,90,WHITE); p2++; wait(4); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - } - }*/ + } //Case 3: //Player 1 : Paper @@ -497,22 +313,7 @@ lcd.filled_rectangle(50, 60, 80,90,WHITE); p1++; wait(5); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - }*/ + } //Case 4: @@ -533,22 +334,7 @@ lcd.line(62, 61, 90, 85, WHITE); lcd.line(62, 81, 90, 55, WHITE); wait(5); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - }*/ + } //Case 5: //Player 1 : Scissor @@ -567,22 +353,7 @@ lcd.line(62, 61, 90, 85, WHITE); lcd.line(62, 81, 90, 55, WHITE); wait(5); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - }*/ + } //Case 6: //Player 1 : Rock @@ -598,22 +369,7 @@ p1++; lcd.circle(60, 70, 30,WHITE); wait(5); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - }*/ + } //Case 7: //Player 1 : Scissors @@ -630,22 +386,7 @@ p2++; lcd.circle(60, 70, 30,WHITE); wait(5); - /* - lcd.cls(); - lcd.locate(1,4); - lcd.rectangle(0,0,125,125,WHITE); //Draw white border - lcd.printf(" P1 Score : %d\n",p1); - lcd.printf(" P2 Score : %d\n\n\n\n",p2); - lcd.printf(" 1 to continue\n\n"); - lcd.printf(" 2 to end\n\n"); - - while(1){ - if(pb1 != 1) { - lcd.cls(); - goto L3; - } - else if(pb2 != 1) goto L2; - }*/ + } @@ -682,6 +423,8 @@ wait(1); while(1){ + lcd.locate(1,4); + lcd.printf(" Waiting for \n the other player"); if(player1.readable()){ continue2 = player1.getc(); myled = 1; @@ -692,24 +435,8 @@ //If both say continue then you need to go back to L3 if(continue1 == 'Y' && continue2 == 'Y') goto L3; else goto L2; - - - - } - - } - - - - // Code to play a wave file - /* FILE *wave_file; - printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/HG.wav","r"); - waver.play(wave_file); - wait(5); - printf("ok!!"); - fclose(wave_file); - */ + }} + @@ -717,13 +444,11 @@ //If game ends : L2: lcd.cls(); - - //Create animation of two bouncing balls float fx=50.0,fy=21.0,vx=5.0,vy=0.4,vx2=2,fx2=40.0; int x=50,y=21,radius=4; int x2 = 30, y2 = 10; - //lcd.rectangle(0,0,125,125,GREEN); //Draw green border + //draw walls wait(2); //If Player 1's score is higher than Player 2's score @@ -752,15 +477,12 @@ //draw ball //lcd.cls(); if(p1 > p2) - //lcd.printf("Player 1 wins!"); lcd.text_string("Player 1 Wins!", 2, 4, FONT_7X8, WHITE); else if(p1 == p2) - //lcd.printf("It's a draw!"); lcd.text_string("It's a draw!", 2, 4, FONT_7X8, WHITE); else if(p2 > p1) - //lcd.printf("Player 2 wins!"); lcd.text_string("Player 2 Wins!", 2, 4, FONT_7X8, WHITE); lcd.filled_circle(x, y, radius, RED);