ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
shahidsajid
Date:
Sat Apr 27 15:42:36 2019 +0000
Parent:
15:81a3aaf52647
Child:
19:e1ded5acb64a
Commit message:
Created no. of balls completed; created a target; printed the target and no. of balls onto the screen; Created displays for when game is won and game is lost

Changed in this revision

Ball/Ball.cpp Show annotated file Show diff for this revision Revisions of this file
Ball/Ball.h Show annotated file Show diff for this revision Revisions of this file
Bat/Bat.cpp Show annotated file Show diff for this revision Revisions of this file
Bat/Bat.h Show annotated file Show diff for this revision Revisions of this file
Cricket/Cricket.cpp Show annotated file Show diff for this revision Revisions of this file
Cricket/Cricket.h Show annotated file Show diff for this revision Revisions of this file
Scoreboard/Scoreboard.cpp Show annotated file Show diff for this revision Revisions of this file
Scoreboard/Scoreboard.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/Ball/Ball.cpp	Wed Apr 24 21:17:22 2019 +0000
+++ b/Ball/Ball.cpp	Sat Apr 27 15:42:36 2019 +0000
@@ -17,11 +17,20 @@
     _y1=3;
     _x = 42;
     _y = 15;
+    _ball_count=0;
     set_tone=0;
     bat.init(3,4);
     bowled=0;
+    _ball_count=0;
 
 }
+void Ball::increment_ball_count(){
+    _ball_count++;
+}
+int Ball::get_ball_count(){
+    return _ball_count;
+}
+
 Vector2D Ball::get_pos()
 {
     Vector2D p = {_x,_y};
@@ -47,6 +56,7 @@
     }
 }
 int Ball::ball_start(Gamepad &pad){
+   
    int bowler_start_check=bowler_start(pad);
    if (bowler_start_check==1){
          if (_y!=36){ 
@@ -62,6 +72,10 @@
 
 void Ball::draw(N5110 &lcd)
 {
+    char buffer[5];
+    int c=get_ball_count();
+    int length=sprintf(buffer,"B%i",c);
+    lcd.printString(buffer,0,0);
     lcd.drawCircle(_x1,_y1,2,FILL_TRANSPARENT);
     lcd.drawCircle(_x,_y,2,FILL_BLACK);
     //lcd.drawRect(_x,_y,_size,_size,FILL_BLACK);
@@ -93,9 +107,9 @@
 }
 }
 
-
-     
-
+void Ball::reset_ball_count(){
+    _ball_count=0;
+}
 
 
 void Ball::reset(){
@@ -106,6 +120,7 @@
     _x = 42;
     _y = 15;
     set_tone=0;
+    //_ball_count=0;
 }
     
 
--- a/Ball/Ball.h	Wed Apr 24 21:17:22 2019 +0000
+++ b/Ball/Ball.h	Sat Apr 27 15:42:36 2019 +0000
@@ -25,10 +25,13 @@
     /// accessors and mutators
     int ball_start(Gamepad &pad);
     void get_direction(Gamepad &pad);
+    int get_ball_count();
+    void reset_ball_count();
 
     //void update_ball(int checkHit, Direction dir);
     int update_ball(int expected_x,int expected_y);
     int bowler_start(Gamepad &pad);
+    void increment_ball_count();
     
 private:
     Gamepad pad;
@@ -41,6 +44,7 @@
     int fieldNumbers[10];
     Bat bat;
     int ballHit;
+    int _ball_count;
     Vector2D _velocity;
     Direction ball_direction;
     Direction _d;
--- a/Bat/Bat.cpp	Wed Apr 24 21:17:22 2019 +0000
+++ b/Bat/Bat.cpp	Sat Apr 27 15:42:36 2019 +0000
@@ -73,10 +73,10 @@
     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.drawCircle(72,27,5,FILL_TRANSPARENT);
     menu_lcd.printString("CONTROLS",2,5);
     menu_lcd.printString("Y",70,5);
-     menu_lcd.drawCircle(72,42,5,FILL_TRANSPARENT);
+    menu_lcd.drawCircle(72,42,5,FILL_TRANSPARENT);
     
     menu_lcd.refresh();
     
@@ -88,3 +88,29 @@
     //menu_lcd.printString(buffer,3,1);
 }  
 
+void Bat::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);
+    }
+}
+void Bat::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);
+}
+void Bat::info_screen(N5110 &menu_lcd, int target){
+    menu_lcd.clear();
+    menu_lcd.printString("YOU GET 10 BALLS",0,0); 
+    char buffer[5];
+    int length=sprintf(buffer,"SCORE %i RUNS",target);
+    menu_lcd.printString(buffer,0,1);
+    menu_lcd.printString("GOOD LUCK!!!",0,4);
+    menu_lcd.refresh();
+}
+
--- a/Bat/Bat.h	Wed Apr 24 21:17:22 2019 +0000
+++ b/Bat/Bat.h	Sat Apr 27 15:42:36 2019 +0000
@@ -25,6 +25,9 @@
     void first_menu(N5110 &menu_lcd);
     void second_menu(N5110 &menu_lcd);
     void game_over(string message);
+    void game_over_menu(N5110 &menu_lcd,int option);
+    void victory_menu(N5110 &menu_lcd);
+    void info_screen(N5110 &menu_lcd, int target);
     /// accessors and mutators
     
     
--- a/Cricket/Cricket.cpp	Wed Apr 24 21:17:22 2019 +0000
+++ b/Cricket/Cricket.cpp	Sat Apr 27 15:42:36 2019 +0000
@@ -19,11 +19,13 @@
    //float f;
    new_round=1;
    direction_set=0;
+   new_game=1;
    fieldersCount=0;
    init_field_counter=0;
    init_positions();
    fielder_no=-1;
    check_bowled=0;
+   _ball_limit=3;
    check_hit=0;
    loft_check=0;
    outfield_fielder=-1;
@@ -47,20 +49,45 @@
     
 }
 void Cricket::game(N5110 &lcd,Gamepad &pad){
+     if (new_game==1){
+         scoreboard.generate_target();
+         new_game=0;
+     }
      if (new_round==1){
+        if (scoreboard.compare_target()==true){
+            bat.victory_menu(lcd);
+        }
+        check_ball_count(lcd);
         pad.leds_off();
         reset();
         set_field(lcd);
+        ball.increment_ball_count();
         play_game(lcd,pad);
         new_round=0;
     }
     else
-    {
+    {  
         play_game(lcd,pad);
     }
-
       
 }
+void Cricket::check_ball_count(N5110 &lcd){
+    int count=ball.get_ball_count();
+    if (count==_ball_limit){
+        bat.game_over_menu(lcd,1);
+        new_round=1;
+        ball.reset_ball_count();
+        new_game=1;
+    }
+}
+void Cricket::check_victory(N5110 &lcd){
+    bool _victory_check=scoreboard.compare_target();
+        if (_victory_check==true){
+            bat.victory_menu(lcd);
+            new_game=1;
+            ball.reset_ball_count();
+        }
+}
 void Cricket::init_positions(){
     set_init_positions(42,1,N,1);
     set_init_positions(84,30,E,2);
@@ -129,7 +156,7 @@
     scoreboard.reset();
     new_round=1;
     pad.led(1,1.0);
-    pad.tone(750.0,0.5);
+    //pad.tone(750.0,0.5);
     wait(2);
 }    
 void Cricket::draw(N5110 &lcd){
--- a/Cricket/Cricket.h	Wed Apr 24 21:17:22 2019 +0000
+++ b/Cricket/Cricket.h	Sat Apr 27 15:42:36 2019 +0000
@@ -28,10 +28,12 @@
     void play_game(N5110 &lcd,Gamepad &pad);
     void update_scoreboard(int checkUpdate, int runs,Gamepad &pad);
     void batsman_out(string messsage,Gamepad &pad);
+    void check_victory(N5110 &lcd);
     void game(N5110 &lcd,Gamepad &pad);   
     void init_positions();
     void reset();
     int check_fielder(Direction dir);
+    void check_ball_count(N5110 &lcd);
 private:
     Ball ball;
     Bat bat;
@@ -63,6 +65,8 @@
     int fielder_no;
     int ballHit;
     int check_hit;
+      int _ball_limit;
+      int new_game;
     int check_update;
     int loft_check;
     int d;
--- a/Scoreboard/Scoreboard.cpp	Wed Apr 24 21:17:22 2019 +0000
+++ b/Scoreboard/Scoreboard.cpp	Sat Apr 27 15:42:36 2019 +0000
@@ -14,18 +14,35 @@
 {   
    score=0;
 }
-
 void Scoreboard::update_score(int runs){
     score+=runs;
 }
 int Scoreboard::get_score(){
     return score;
 }
+int Scoreboard::get_target(){
+    return target;
+}
 void Scoreboard::draw(N5110 &lcd){
-    char buffer[5];
-    int length=sprintf(buffer,"%i",score);
-    lcd.printString(buffer,70,0);
+    char buffer1[5];
+    int length1=sprintf(buffer1,"S%i",score);
+    char buffer2[5];
+    int length2=sprintf(buffer2,"T%i",target);
+     lcd.printString(buffer2,0,5);
+    lcd.printString(buffer1,70,0);
 }
 void Scoreboard::reset(){
     score=0;
-}
\ No newline at end of file
+}
+void Scoreboard::generate_target(){
+    srand(time(NULL));
+    target=5+rand()%((10+1)-5);
+}
+bool Scoreboard::compare_target(){
+    if (score>=target){
+        return true;
+    }
+    else{
+        return false;
+    }
+}
--- a/Scoreboard/Scoreboard.h	Wed Apr 24 21:17:22 2019 +0000
+++ b/Scoreboard/Scoreboard.h	Sat Apr 27 15:42:36 2019 +0000
@@ -20,10 +20,14 @@
     void draw(N5110 &lcd);
     void update_score(int runs);
     void reset();
+    void generate_target();
+    bool compare_target();
     //getters
     int get_score();
+    int get_target();
 private:
     int score;
+    int target;
     
 
 };
--- a/main.cpp	Wed Apr 24 21:17:22 2019 +0000
+++ b/main.cpp	Sat Apr 27 15:42:36 2019 +0000
@@ -30,6 +30,7 @@
 int main(){
     int fps=10;
     init();
+    
    // welcome();
     draw();
     //float f;