Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: UserInteraction/UX.cpp
- Revision:
- 30:43aace0fdbdf
- Parent:
- 24:23fd6b451db7
- Child:
- 31:eefa1d23a843
--- a/UserInteraction/UX.cpp Tue May 07 18:38:54 2019 +0000
+++ b/UserInteraction/UX.cpp Wed May 08 01:20:06 2019 +0000
@@ -1,237 +1,271 @@
#include "UX.h"
-
-
UX::UX()
{
-
+
}
-
UX::~UX()
{
-
+
}
+//Initialises the UX Object
void UX::init()
-{
+{
batPad.init();
+ _button_pressed=false;
+}
+//Resets the button_pressed variable to ensure screen is printed again
+void UX::reset(){
_button_pressed=false;
}
-
+/*Checks if A Button is pressed to indicate if the ball is hit during the gameplay*/
int UX::get_a_pressed(){
- int _a_pressed=0;
- if (batPad.check_event(Gamepad::A_PRESSED) == true){
- _a_pressed=1;
- }
- return _a_pressed;
+ int _a_pressed=0;
+ if (batPad.check_event(Gamepad::A_PRESSED) == true){
+ _a_pressed=1;
+ }
+ return _a_pressed;
}
+/*Checks if L Button is pressed to indicate if the ball hit is lofted during the gameplay*/
int UX::get_l_pressed(){
- int _l_pressed=0;
- if (batPad.check_event(Gamepad::L_PRESSED) == true){
- //badPad.tone(750.0,0.1);
- _l_pressed=1;
- }
- return _l_pressed;
+ int _l_pressed=0;
+ if (batPad.check_event(Gamepad::L_PRESSED) == true){
+ _l_pressed=1;
+ }
+ return _l_pressed;
}
+/*prints the first screen labeling the game name and my student ID*/
void UX::first_menu(N5110 &menu_lcd){
- menu_lcd.clear();
- menu_lcd.printString("HERO CRICKET",5,1);
- menu_lcd.refresh();
- menu_lcd.printString(" A Game By",5,2);
- menu_lcd.printString( " el17szs",5,3);
- wait(1);
- menu_lcd.refresh();
- menu_lcd.printString("PRESS START..",1,5);
- wait(1);
- menu_lcd.refresh();
- while (batPad.check_event(Gamepad::START_PRESSED) == false) {
- printf("2");
- batPad.leds_on();
- wait(0.1);
- batPad.leds_off();
- wait(0.1);
- }
- second_menu(menu_lcd);
- batPad.leds_off();
+ menu_lcd.clear();
+ menu_lcd.printString("HERO CRICKET",5,1);
+ menu_lcd.refresh();
+ menu_lcd.printString(" A Game By",5,2);
+ menu_lcd.printString( " el17szs",5,3);
+ wait(1);
+ menu_lcd.refresh();
+ menu_lcd.printString("PRESS START..",1,5);
+ wait(1);
+ menu_lcd.refresh();
+ while (batPad.check_event(Gamepad::START_PRESSED) == false) {
+ printf("2");
+ batPad.leds_on();
+ wait(0.1);
+ batPad.leds_off();
+ wait(0.1);
+ }
+ second_menu(menu_lcd);
+ batPad.leds_off();
}
+/*
+ The second menu is called at the start of the game and each time a game is over
+ The menu gives the user an option to choose between playing the game
+ reading the controls or the rules of the game
+*/
void UX::second_menu(N5110 &menu_lcd){
- while (_button_pressed==false){
- menu_options(menu_lcd);
- if (batPad.check_event(Gamepad::A_PRESSED) == true){
- _button_pressed=true;
- }
- if (batPad.check_event(Gamepad::B_PRESSED) == true){
- rules_menu(menu_lcd);
- }
- if (batPad.check_event(Gamepad::X_PRESSED) == true){
- controls_menu(menu_lcd);
-
- }
-
+ menu_lcd.setContrast(0.5);
+ while (_button_pressed==false){
+ menu_options(menu_lcd);
+ if (batPad.check_event(Gamepad::A_PRESSED) == true){
+ _button_pressed=true;
}
-
+ if (batPad.check_event(Gamepad::B_PRESSED) == true){
+ rules_menu(menu_lcd);
+ }
+ if (batPad.check_event(Gamepad::X_PRESSED) == true){
+ controls_menu(menu_lcd);
+ }
+ }
}
+/*Prints the options for the second menu onto the screen */
void UX::menu_options(N5110 &menu_lcd){
- batPad.leds_off();
- menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
- menu_lcd.printString("START GAME",2,1);
- menu_lcd.printString("A",70,1);
- menu_lcd.drawCircle(72,11,5,FILL_TRANSPARENT);
- menu_lcd.printString("RULES",2,3);
- menu_lcd.printString("B",70,3);
- menu_lcd.drawCircle(72,27,5,FILL_TRANSPARENT);
- menu_lcd.printString("CONTROLS",2,5);
- menu_lcd.printString("X",70,5);
- menu_lcd.drawCircle(72,42,5,FILL_TRANSPARENT);
- menu_lcd.refresh();
+ batPad.leds_off();
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+ menu_lcd.printString("START GAME",2,1);
+ menu_lcd.printString("A",70,1);
+ menu_lcd.drawCircle(72,11,5,FILL_TRANSPARENT);
+ menu_lcd.printString("RULES",2,3);
+ menu_lcd.printString("B",70,3);
+ menu_lcd.drawCircle(72,27,5,FILL_TRANSPARENT);
+ menu_lcd.printString("CONTROLS",2,5);
+ menu_lcd.printString("X",70,5);
+ menu_lcd.drawCircle(72,42,5,FILL_TRANSPARENT);
+ menu_lcd.refresh();
+}
+/*Prints the rules of the game to the LCD for user experience
+ There is wait of 5 seconds betwen each screen for the user to read the rules
+*/
+void UX::rules_menu(N5110 &menu_lcd){
+ rules_menu_game(menu_lcd);
+ wait(5);
+ rules_menu_screen_scoring_runs(menu_lcd);
+ wait(5);
+ rules_menu_screen_out(menu_lcd);
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("The target is",0,0);
+ menu_lcd.printString("printed bottom ",0,1);
+ menu_lcd.printString("left of LCD",0,2);
+ menu_lcd.printString("Balls and Runs",0,3);
+ menu_lcd.printString("on top left",0,4);
+ menu_lcd.printString("and right",0,5);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("10 balls are",0,1);
+ menu_lcd.printString("given to score",0,2);
+ menu_lcd.printString("a set target",0,3);
+ menu_lcd.printString("GOOD LUCK!!!",0,4);
+ menu_lcd.refresh();
+ wait(5);
}
-void UX::rules_menu(N5110 &menu_lcd){
+/*Prints the rules for the game on the screen, called in rules_menu()*/
+void UX::rules_menu_game(N5110 &menu_lcd){
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.printString("Hit the ball",0,0);
+ menu_lcd.printString("to score runs",0,1);
+ menu_lcd.printString("Ball is bolwed",0,3);
+ menu_lcd.printString("with a tone",0,4);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.printString("There are 5",0,0);
+ menu_lcd.printString("fielders in",0,1);
+ menu_lcd.printString("the ",0,1);
+ menu_lcd.printString("marked by",0,3);
+ menu_lcd.printString("white circles",0,4);
+ menu_lcd.refresh();
+}
+/*Prints the rules for scoring runs during the game*/
+void UX::rules_menu_screen_scoring_runs(N5110 &menu_lcd){
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.printString("SCORING RUNS",0,0);
+ menu_lcd.printString("Lofted Hit:",0,2);
+ menu_lcd.printString("Hit In between ",0,3);
+ menu_lcd.printString("the fielders ",0,4);
+ menu_lcd.printString("is 6 runs",0,5);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.printString("SCORING RUNS",0,0);
+ menu_lcd.printString("Normal Hit:",0,2);
+ menu_lcd.printString("Hit In between ",0,3);
+ menu_lcd.printString("the fielders ",0,4);
+ menu_lcd.printString("is 4 runs",0,5);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.setContrast(0.5);
+ menu_lcd.printString("SCORING RUNS",0,0);
+ menu_lcd.printString("Normal Hit:",0,2);
+ menu_lcd.printString("Hit to fielder ",0,3);
+ menu_lcd.printString("inside circle ",0,4);
+ menu_lcd.printString("is 1 run",0,5);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("SCORING RUNS",0,0);
+ menu_lcd.printString("Normal Hit:",0,2);
+ menu_lcd.printString("Hit to fielder ",0,3);
+ menu_lcd.printString("out of circle ",0,4);
+ menu_lcd.printString("is 2 runs",0,5);
+ menu_lcd.refresh();
+}
+/*Prints the rules for how a player can get out and lose the game */
+void UX::rules_menu_screen_out(N5110 &menu_lcd){
menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.printString("Hit the ball",0,0);
- menu_lcd.printString("to score runs",0,1);
- menu_lcd.printString("Ball is bolwed",0,3);
- menu_lcd.printString("with a tone",0,4);
- menu_lcd.refresh();
- wait(5);
-
- menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.printString("There are 5",0,0);
- menu_lcd.printString("fielders in",0,1);
- menu_lcd.printString("the ",0,1);
- menu_lcd.printString("marked by",0,3);
- menu_lcd.printString("white circles",0,4);
- menu_lcd.refresh();
- wait(5);
-
- menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.printString("SCORING RUNS",0,0);
- menu_lcd.printString("Lofted Hit:",0,2);
- menu_lcd.printString("Hit In between ",0,3);
- menu_lcd.printString("the fielders ",0,4);
- menu_lcd.printString("is 6 runs",0,5);
- menu_lcd.refresh();
- wait(5);
-
-
-
- menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.printString("SCORING RUNS",0,0);
- menu_lcd.printString("Normal Hit:",0,2);
- menu_lcd.printString("Hit In between ",0,3);
- menu_lcd.printString("the fielders ",0,4);
- menu_lcd.printString("is 4 runs",0,5);
- menu_lcd.refresh();
- wait(5);
- menu_lcd.clear();
- menu_lcd.setContrast(0.5);
- menu_lcd.printString("SCORING RUNS",0,0);
- menu_lcd.printString("Normal Hit:",0,2);
- menu_lcd.printString("Hit to fielder ",0,3);
- menu_lcd.printString("inside circle ",0,4);
- menu_lcd.printString("is 1 run",0,5);
- menu_lcd.refresh();
- wait(5);
- menu_lcd.clear();
- menu_lcd.printString("SCORING RUNS",0,0);
- menu_lcd.printString("Normal Hit:",0,2);
- menu_lcd.printString("Hit to fielder ",0,3);
- menu_lcd.printString("out of circle ",0,4);
- menu_lcd.printString("is 2 runs",0,5);
- menu_lcd.refresh();
- wait(5);
-
- menu_lcd.clear();
- menu_lcd.printString("GETTING OUT",0,0);
- menu_lcd.printString("CAUGHT:",0,2);
- menu_lcd.printString("A Lofted Hit",0,3);
- menu_lcd.printString("to a fiedler",0,4);
- menu_lcd.printString("is OUT",0,5);
- menu_lcd.refresh();
- wait(5);
- menu_lcd.clear();
- menu_lcd.printString("GETTING OUT",0,0);
- menu_lcd.printString("BOWLED:",0,2);
- menu_lcd.printString("If you do ",0,3);
- menu_lcd.printString("not hit ball",0,4);
- menu_lcd.printString("It is OUT",0,5);
- menu_lcd.refresh();
- wait(5);
- menu_lcd.clear();
- menu_lcd.printString("10 balls are",0,1);
- menu_lcd.printString("given to score",0,2);
- menu_lcd.printString("a set target",0,3);
- menu_lcd.printString("GOOD LUCK!!!",0,4);
- menu_lcd.refresh();
- wait(5);
-}
+ menu_lcd.printString("GETTING OUT",0,0);
+ menu_lcd.printString("CAUGHT:",0,2);
+ menu_lcd.printString("A Lofted Hit",0,3);
+ menu_lcd.printString("to a fiedler",0,4);
+ menu_lcd.printString("is OUT",0,5);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("GETTING OUT",0,0);
+ menu_lcd.printString("BOWLED:",0,2);
+ menu_lcd.printString("If you do ",0,3);
+ menu_lcd.printString("not hit ball",0,4);
+ menu_lcd.printString("It is OUT",0,5);
+ menu_lcd.refresh();
+}
+/*Prints the controls for the game to the LCD for user experience
+ There is wait of 5 seconds betwen each screen for the user to read the controls
+*/
void UX::controls_menu(N5110 &menu_lcd){
+ menu_lcd.clear();
+ menu_lcd.printString("Hold down A to ",0,2);
+ menu_lcd.printString("hit the ball",0,3);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("Hold down A ",0,1);
+ menu_lcd.printString("and L for ",0,2);
+ menu_lcd.printString("a lofted hit",0,3);
+ menu_lcd.refresh();
+ wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("Move joystick to",0,1);
+ menu_lcd.printString("the direction",0,2);
+ menu_lcd.printString("you want to",0,3);
+ menu_lcd.printString("hit the ball",0,4);
+ menu_lcd.refresh();
+ wait(5);
+}
+/*Prints messages to the screen to inform user that game is over
+ There are 3 ways for the game to end with the argument integer indicating the afformentoned.
+ The 3 ways for the game to end are
+ 1. No more balls remaning
+ 2. Player did not hit the ball: BOWLED
+ 3. Player hit a lofted shot to a fielder : CAUGHT
+*/
+void UX::game_over_menu(N5110 &menu_lcd,int option){
+ if (option==1){
menu_lcd.clear();
- menu_lcd.printString("Hold down A to ",0,2);
- menu_lcd.printString("hit the ball",0,3);
+ menu_lcd.printString("GAME LOST",2,1);
+ menu_lcd.printString("NO MORE BALLS LEFT",2,3);
menu_lcd.refresh();
wait(5);
+ }
+ if (option==2){
menu_lcd.clear();
- menu_lcd.printString("Hold down A ",0,1);
- menu_lcd.printString("and L for ",0,2);
- menu_lcd.printString("a lofted hit",0,3);
+ menu_lcd.printString("GAME LOST",2,1);
+ menu_lcd.printString("BOWLED",2,3);
menu_lcd.refresh();
wait(5);
+ }
+ if (option==3){
menu_lcd.clear();
- menu_lcd.printString("Move joystick to",0,1);
- menu_lcd.printString("the direction",0,2);
- menu_lcd.printString("you want to",0,3);
- menu_lcd.printString("hit the ball",0,4);
+ menu_lcd.printString("GAME LOST",2,1);
+ menu_lcd.printString("CAUGHT",2,3);
menu_lcd.refresh();
wait(5);
- }
-void UX::game_over_menu(N5110 &menu_lcd,int option){
- if (option==1){
- menu_lcd.clear();
- menu_lcd.printString("GAME LOST",2,1);
- menu_lcd.printString("NO MORE BALLS LEFT",2,3);
- menu_lcd.refresh();
- wait(5);
- }
- if (option==2){
- menu_lcd.clear();
- menu_lcd.printString("GAME LOST",2,1);
- menu_lcd.printString("BOWLED",2,3);
- menu_lcd.refresh();
- wait(5);
- }
- if (option==3){
- menu_lcd.clear();
- menu_lcd.printString("GAME LOST",2,1);
- menu_lcd.printString("CAUGHT",2,3);
- menu_lcd.refresh();
- wait(5);
- }
-
+ }
}
+/*Prints messages to the LCD to inform user that the game is won*/
void UX::victory_menu(N5110 &menu_lcd){
- menu_lcd.clear();
- menu_lcd.printString("CONGRATS!!",2,1);
- menu_lcd.printString("YOU HAVE WON",2,3);
- menu_lcd.refresh();
- wait(5);
+ menu_lcd.clear();
+ menu_lcd.printString("CONGRATS!!",2,1);
+ menu_lcd.printString("YOU HAVE WON",2,3);
+ menu_lcd.refresh();
+ wait(5);
}
void UX::info_screen(N5110 &menu_lcd, int target){
- menu_lcd.clear();
- menu_lcd.printString("YOU ARE GIVEN",2,1);
- menu_lcd.printString("10 BALLS ",3,2);
- menu_lcd.refresh();
- wait(0.5);
- char buffer[5];
- int length=sprintf(buffer,"SCORE %i RUNS",target);
- menu_lcd.printString(buffer,2,4);
- menu_lcd.refresh();
- wait(0.5);
- menu_lcd.printString("GOOD LUCK!!!",0,5);
- menu_lcd.refresh();
- wait(3);
+ menu_lcd.clear();
+ menu_lcd.printString("YOU ARE GIVEN",2,1);
+ menu_lcd.printString("10 BALLS ",3,2);
+ menu_lcd.refresh();
+ wait(0.5);
+ char buffer[5];
+ int length=sprintf(buffer,"SCORE %i RUNS",target);
+ menu_lcd.printString(buffer,2,4);
+ menu_lcd.refresh();
+ wait(0.5);
+ menu_lcd.printString("GOOD LUCK!!!",0,5);
+ menu_lcd.refresh();
+ wait(3);
}
-