ELEC2645 (2018/19) / Mbed 2 deprecated el17szs

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
shahidsajid
Date:
Tue Apr 30 20:36:42 2019 +0000
Parent:
21:a0904159e183
Child:
23:a16c97a59a41
Commit message:
Completed version of the game;

Changed in this revision

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
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
UserInteraction/UX.cpp Show annotated file Show diff for this revision Revisions of this file
UserInteraction/UX.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.h	Tue Apr 30 13:17:14 2019 +0000
+++ b/Ball/Ball.h	Tue Apr 30 20:36:42 2019 +0000
@@ -6,9 +6,9 @@
 #include "Gamepad.h"
 #include "Bat.h"
 /** Ball Class
-@author Dr Craig A. Evans, University of Leeds
-@brief Controls the ball in the Pong game 
-@date Febraury 2017
+@author Shahid Zubin Sajid
+@brief Controls the ball in the Hero Cricket Game 
+@date May 2019
 */ 
 class Ball
 {
@@ -18,33 +18,29 @@
     ~Ball();
     void init(int size,int speed);
     void draw(N5110 &lcd);
-    Vector2D get_pos();
-    void set_pos(Vector2D p);
+    
+    //reset functions
     void reset();
+    void reset_ball_count();
     
     /// accessors and mutators
-    int ball_start(Gamepad &pad);
     void get_direction(Gamepad &pad);
     int get_ball_count();
-    void reset_ball_count();
-
+    
+    
+    //gameplay methods
     //void update_ball(int checkHit, Direction dir);
+    int ball_start(Gamepad &pad);
     int update_ball(int expected_x,int expected_y);
     int bowler_start(Gamepad &pad);
     void increment_ball_count();
     
 private:
-    struct Fielder{
-        Direction dir;
-        int x;
-        int y;
-        int position;
-    };
+    
     int fieldNumbers[10];
     Bat bat;
     int ballHit;
     int _ball_count;
-    Vector2D _velocity;
     Direction ball_direction;
     Direction _d;
     int bowled;
--- a/Bat/Bat.cpp	Tue Apr 30 13:17:14 2019 +0000
+++ b/Bat/Bat.cpp	Tue Apr 30 20:36:42 2019 +0000
@@ -29,10 +29,9 @@
 }
 int Bat::get_hitBall(UX &ux){
     _hitBall=ux.get_a_pressed();
-    printf("_hitBall %i \n",_hitBall);
     return _hitBall;
 }
 int Bat::get_loft_ball(UX &ux){
-     _loft_ball=ux.get_a_pressed();
+     _loft_ball=ux.get_l_pressed();
     return _loft_ball;
 }
\ No newline at end of file
--- a/Cricket/Cricket.cpp	Tue Apr 30 13:17:14 2019 +0000
+++ b/Cricket/Cricket.cpp	Tue Apr 30 20:36:42 2019 +0000
@@ -17,13 +17,14 @@
    bat.init(4,5);
    ball.init(2,3);
    scoreboard.init();
-   new_round=1;
+   _new_round=1;
    direction_set=0;
-   new_game=1;
+   _new_game=1;
    fieldersCount=0;
    init_field_counter=0;
    init_positions();
    set_hit=0;
+   set_loft=0;
    _position_no=-1;
    check_bowled=0;
    _ball_limit=3;
@@ -35,12 +36,14 @@
 void Cricket::round_reset(){
    ball.reset();
    bat.reset();
-   new_round=0;
+   ux.reset();
+   _new_round=0;
    direction_set=0;
    fieldersCount=0;
    loft_check=0;
    check_hit=0;
    set_hit=0;
+   set_loft=0;
    ballHit=0;
    init_field_counter=0;
    init_positions();
@@ -51,22 +54,37 @@
 }
 void Cricket::game_reset(){
     round_reset();
-    new_game=1;
+    _new_game=1;
     scoreboard.reset();
     ball.reset_ball_count();
 }
+void Cricket::intro(N5110 &lcd){
+    ux.first_menu(lcd);
+}
 void Cricket::game(N5110 &lcd,Gamepad &pad){
      
-     if (new_game==1){
-         scoreboard.generate_target();
-         ux.first_menu(lcd,pad);
-         //bat.info_screen(lcd,scoreboard.get_target());
-         new_game=0;       
+     if (_new_game==1){
+         new_game(lcd,pad);      
      }
      
-     if (new_round==1){
-        
-        if (scoreboard.compare_target()==true){
+     if (_new_round==1){
+         new_round(lcd,pad);
+     }
+     else{
+        play_game(lcd,pad);
+    }
+}
+
+    
+void Cricket::new_game(N5110 &lcd, Gamepad &pad){
+         _new_game=0; 
+         scoreboard.generate_target();
+         ux.second_menu(lcd);
+         ux.info_screen(lcd,scoreboard.get_target());
+         play_game(lcd,pad); 
+}
+void Cricket::new_round(N5110 &lcd, Gamepad &pad){
+    if (scoreboard.compare_target()==true){
             ux.victory_menu(lcd);
             game_reset();
         }
@@ -82,18 +100,16 @@
           set_field(lcd);
           ball.increment_ball_count();
           play_game(lcd,pad);
-          new_round=0;
-        }
-    }
-    else
-        play_game(lcd,pad);
+          _new_round=0;
+      }
 }
+    
 bool Cricket::check_ball_count(N5110 &lcd){
     int count=ball.get_ball_count();
     if (count==_ball_limit){
-        new_round=1;
+        _new_round=1;
         ball.reset_ball_count();
-        new_game=1;
+        _new_game=1;
         return true;
     }
     else{
@@ -104,7 +120,7 @@
     bool _victory_check=scoreboard.compare_target();
         if (_victory_check==true){
             ux.victory_menu(lcd);
-            new_game=1;
+            _new_game=1;
             ball.reset_ball_count();
         }
 }
@@ -131,12 +147,15 @@
         set_hit=1;
     }
     loft_check=bat.get_loft_ball(ux);
+    if (loft_check==1){
+        set_loft=1;
+    }
     Direction dir=pad.get_direction();
     if (check_bowled!=1){
         check_bowled=ball.ball_start(pad);
     }
     else{
-        update_game(set_hit, loft_check, dir,pad,lcd);
+        update_game(set_hit, set_loft, dir,pad,lcd);
     }
 }
 void Cricket::update_game(int checkHit,int loft_check, Direction dir,Gamepad &pad,N5110 &lcd){
@@ -171,13 +190,13 @@
             scoreboard.update_score(0);
             pad.led(3,1.0);
             wait(0.5);
-            new_round=1;
+            _new_round=1;
         }
         else{
             scoreboard.update_score(runs);
             pad.led(3,1.0);
             wait(0.5);
-            new_round=1;
+            _new_round=1;
         }  
     }
 }
@@ -188,7 +207,7 @@
     //pad.tone(750.0,0.5);
     ball.reset_ball_count();
     ux.game_over_menu(lcd,option);
-    new_round=1;
+    _new_game=1;
     wait(2);
 }    
 void Cricket::draw(N5110 &lcd){
--- a/Cricket/Cricket.h	Tue Apr 30 13:17:14 2019 +0000
+++ b/Cricket/Cricket.h	Tue Apr 30 20:36:42 2019 +0000
@@ -31,7 +31,9 @@
     void batsman_out(int option,Gamepad &pad, N5110 &lcd);
     void check_victory(N5110 &lcd);
     void game(N5110 &lcd,Gamepad &pad); 
-      
+    void new_round(N5110 &lcd, Gamepad &pad);
+    void new_game(N5110 &lcd, Gamepad &pad);
+    void intro(N5110 &lcd);  
     void init_positions();
     void round_reset();
     void game_reset();
@@ -64,15 +66,16 @@
     int init_field_counter;
     int outfield_fielder;
     int fieldersCount;
-    int new_round;
+    int _new_round;
     int fieldNumbers[10];
     int _position_no;
     int ballHit;
     int check_hit;
       int _ball_limit;
-      int new_game;
+      int _new_game;
     int check_update;
     int set_hit;
+    int set_loft;
     int loft_check;
     int d;
     int _size;
--- a/UserInteraction/UX.cpp	Tue Apr 30 13:17:14 2019 +0000
+++ b/UserInteraction/UX.cpp	Tue Apr 30 20:36:42 2019 +0000
@@ -15,6 +15,9 @@
     batPad.init();
     _button_pressed=false;
 }
+void UX::reset(){
+    _button_pressed=false;
+}
 int UX::get_a_pressed(){
     int _a_pressed=0;
     if (batPad.check_event(Gamepad::A_PRESSED) == true){
@@ -30,7 +33,7 @@
     }
     return _l_pressed;
 }
-void UX::first_menu(N5110 &menu_lcd,Gamepad &pad){
+void UX::first_menu(N5110 &menu_lcd){
     menu_lcd.clear();
     menu_lcd.printString("HERO CRICKET",5,1);
     menu_lcd.refresh();
@@ -48,7 +51,7 @@
             batPad.leds_off();
             wait(0.1);
         }
-        second_menu(menu_lcd);
+        //second_menu(menu_lcd);
         batPad.leds_off();
 }
 void UX::second_menu(N5110 &menu_lcd){
@@ -58,9 +61,11 @@
             _button_pressed=true;
         }
         if (batPad.check_event(Gamepad::B_PRESSED) == true){
+             printf("HERE1\n");
              rules_menu(menu_lcd);
         }
         if (batPad.check_event(Gamepad::X_PRESSED) == true){
+             printf("WHAT \n");
              controls_menu(menu_lcd);
              
         }
@@ -98,7 +103,7 @@
      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("the outfield ",0,2);
      menu_lcd.printString("marked by",0,3);
      menu_lcd.printString("white circles",0,4);
      menu_lcd.refresh();
@@ -231,17 +236,6 @@
     wait(0.5);
     menu_lcd.printString("GOOD LUCK!!!",0,5);
     menu_lcd.refresh();
-    int i=0;
-    /*
-    while (i<=10){
-        batPad.leds_on();
-        wait(0.1);
-        batPad.leds_off();
-        wait(0.1);
-        i++;
-    }
-    */
-    wait(3);
-    
+    wait(1);
 }
 
--- a/UserInteraction/UX.h	Tue Apr 30 13:17:14 2019 +0000
+++ b/UserInteraction/UX.h	Tue Apr 30 20:36:42 2019 +0000
@@ -23,7 +23,7 @@
     int get_l_pressed();
     void reset();
     int get_loft_ball();
-    void first_menu(N5110 &menu_lcd,Gamepad &pad);
+    void first_menu(N5110 &menu_lcd);
     void second_menu(N5110 &menu_lcd);
     void menu_options(N5110 &menu_lcd);
     void rules_menu(N5110 &menu_lcd);
--- a/main.cpp	Tue Apr 30 13:17:14 2019 +0000
+++ b/main.cpp	Tue Apr 30 20:36:42 2019 +0000
@@ -15,11 +15,13 @@
 #include "Bat.h"
 #include "Cricket.h"
 
+
 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 Gamepad mainpad;
 Cricket cricket;
 
 
+
 void init();
 
 void welcome();
@@ -29,10 +31,9 @@
 int main(){
     int fps=14;
     init();
-    
+    cricket.intro(lcd);
      //welcome();
     //draw();
-    
     while(1){
        
         cricket.game(lcd,mainpad);