Yang Hongxiao 201199678

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
YHX
Date:
Thu May 14 17:00:34 2020 +0000
Parent:
0:4b02786450c0
Commit message:
Yang Hongxiao; el17hy; 201199678

Changed in this revision

Bullet.cpp Show annotated file Show diff for this revision Revisions of this file
Bullet.h Show annotated file Show diff for this revision Revisions of this file
Control.cpp Show annotated file Show diff for this revision Revisions of this file
Control.h Show annotated file Show diff for this revision Revisions of this file
Gamepad/Gamepad.h Show annotated file Show diff for this revision Revisions of this file
Obstacle.cpp Show annotated file Show diff for this revision Revisions of this file
Obstacle.h Show annotated file Show diff for this revision Revisions of this file
Platform.cpp Show annotated file Show diff for this revision Revisions of this file
Platform.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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 4b02786450c0 -r a6ead8050c23 Bullet.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bullet.cpp	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,72 @@
+#include "Bullet.h"
+
+Bullet::Bullet()
+{
+
+}
+
+Bullet::~Bullet()
+{
+
+}
+
+void Bullet::init(int r,int vv)
+{
+    _r = r;
+    srand(time(NULL));
+    _x = rand()%(84-_r);   //the inintial ball fall from the top
+    _y = 0;
+
+    srand(time(NULL));
+    int direction = rand()%4;     //the direction of the ball has four possibility
+
+    // 4 possibilities. Get random modulo and set velocities accordingly
+    if (direction == 0) {
+        _velocity.x = vv;
+        _velocity.y = vv;
+    } else if (direction == 1) {
+        _velocity.x = vv;
+        _velocity.y = -vv;
+    } else if (direction == 2) {
+        _velocity.x = vv;
+        _velocity.y = vv;
+    } else {
+        _velocity.x = -vv;
+        _velocity.y = -vv;
+    }
+}
+
+void Bullet::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_r,_r,FILL_BLACK);      //draw the ball
+}
+
+void Bullet::update()
+{
+    _x += _velocity.x;
+    _y += _velocity.y;
+}
+
+void Bullet::set_velocity(Vector2D v)
+{
+    _velocity.x = v.x;
+    _velocity.y = v.y;
+}
+
+Vector2D Bullet::get_velocity()
+{
+    Vector2D v = {_velocity.x,_velocity.y};
+    return v;
+}
+
+Vector2D Bullet::get_pos()
+{
+    Vector2D p = {_x,_y};
+    return p;
+}
+
+void Bullet::set_pos(Vector2D p)
+{
+    _x = p.x;
+    _y = p.y;
+}
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 Bullet.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Bullet.h	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,58 @@
+#ifndef BULLET_H
+#define BULLET_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Platform.h"
+/** The bullet Class
+* @brief This program define the bullets
+* @author Yang Hongxiao
+* @date 15/5/2020
+*/
+
+class Bullet
+{
+
+public:
+/** Constructor */
+Bullet();
+/** Destructor */
+~Bullet();
+/** Set the inital value
+ * @param the value of the bullet size (int r),the value of the bullet speed (int vv),
+ */
+    void init(int r,int vv);
+/** draw the bullet
+ * @param the screen(N5110 &lcd)
+ */
+    void draw(N5110 &lcd);
+/** Set the update value
+ * @param none
+ */
+    void update();
+/** Set the velocity value
+ * @param the velocity(Vector2D v)
+ */    
+    void set_velocity(Vector2D v);
+/** Get the velocity
+ * @return the current velocity
+ */    
+    Vector2D get_velocity();
+ /** Get the position
+ * @return the current position
+ */    
+    Vector2D get_pos();
+/** Set the position value
+ * @param the position(Vector2D p)
+ */      
+    void set_pos(Vector2D p);
+    
+private:
+
+    Vector2D _velocity;
+    int _r;
+    int _x;
+    int _y;
+};
+#endif
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 Control.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Control.cpp	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,147 @@
+#include "Control.h"
+
+Control::Control()
+{
+
+}
+
+Control::~Control()
+{
+
+}
+
+void Control::init(int platform_w,int platform_h,int r,int vv)
+{
+   
+    _platform_w = platform_w;
+    _platform_h = platform_h;
+    _r = r;
+    _vv = vv;
+    _length =  45;
+    _p.init(_length,_platform_h,_platform_w);
+    _o.init();
+    _bullet.init(_r,_vv);
+}
+
+void Control::read_input(Gamepad &pad)
+{
+    _d = pad.get_direction();
+    _mag = pad.get_mag();
+}
+
+void Control::draw(N5110 &lcd)
+{
+
+    lcd.drawRect(0,0,WIDTH,HEIGHT,FILL_TRANSPARENT);
+    print_scores(lcd);
+    _p.draw(lcd);
+    _bullet.draw(lcd);
+    _o.draw(lcd);
+}
+
+void Control::update(Gamepad &pad)
+{
+    check_goal(pad);
+    _p.update(_d,_mag);
+    _bullet.update();
+    check_wall_collision(pad);
+    check_platform_collisions(pad);
+    check__obstacle_collisios(pad);
+}
+
+void Control::check_wall_collision(Gamepad &pad)
+{
+    Vector2D bullet_pos = _bullet.get_pos();
+    Vector2D bullet_velocity = _bullet.get_velocity();
+
+    if (bullet_pos.y <= 1) {  
+        bullet_pos.y = 1;  
+        bullet_velocity.y = -bullet_velocity.y;
+        pad.tone(550.0,0.1);
+    }
+  
+    else if (bullet_pos.x + _r >= (WIDTH-1) ) { 
+        bullet_pos.x = (WIDTH-1) - _r;  
+       bullet_velocity.x = -bullet_velocity.x;
+
+        pad.tone(550.0,0.1);
+    }
+
+    else if (bullet_pos.x <= 1) { 
+        bullet_pos.x = 1; 
+        bullet_velocity.x = -bullet_velocity.x;
+        pad.tone(550.0,0.1);
+    }
+    _bullet.set_velocity(bullet_velocity);
+    _bullet.set_pos(bullet_pos);
+}
+
+void Control::check_platform_collisions(Gamepad &pad)
+{
+    Vector2D bullet_pos = _bullet.get_pos();
+    Vector2D bullet_velocity = _bullet.get_velocity();
+
+    Vector2D p_pos = _p.get_pos();
+
+    if (
+        (bullet_pos.y >= p_pos.y) &&                  
+        (bullet_pos.y <= p_pos.y + _platform_h) && 
+        (bullet_pos.x >= _length) && 
+        (bullet_pos.x <= _length + _platform_w)
+    ) {
+        bullet_pos.y = _length+ _platform_h;
+        bullet_velocity.y = -bullet_velocity.y;
+        pad.tone(1000.0,0.1);
+              
+    }
+
+    _bullet.set_velocity(bullet_velocity);
+    _bullet.set_pos(bullet_pos);
+}
+void Control::check__obstacle_collisios(Gamepad &pad)
+{
+  
+    Vector2D bullet_pos = _bullet.get_pos();
+    Vector2D bullet_velocity = _bullet.get_velocity();
+    Vector2D p_pos = _p.get_pos();
+    Vector2D o_pos= _o.get_obstacle_pos();
+
+    if ((bullet_pos.y >= o_pos.y) && 
+        (bullet_pos.y <= o_pos.y + 2) &&
+        (bullet_pos.x >= o_pos.x) && 
+        (bullet_pos.x <= o_pos.x + 20) ){
+       
+        bullet_velocity.y = -bullet_velocity.y;
+   
+        pad.tone(1000.0,0.1);
+        _p.add_score();
+        _o.new_obstacle();
+    }
+
+    _bullet.set_velocity(bullet_velocity);
+    _bullet.set_pos(bullet_pos);
+}
+
+void Control::check_goal(Gamepad &pad)
+{
+   
+    Vector2D bullet_pos = _bullet.get_pos();
+    
+    if (bullet_pos.y > WIDTH) {
+      
+        _bullet.init(_r,_vv);
+        pad.tone(1500.0,0.5);
+        pad.leds_on();
+        wait(0.5);
+        pad.leds_off();
+    }
+}
+
+void Control::print_scores(N5110 &lcd)
+{
+    int p_score = _p.get_score();
+    char buffer1[14];
+    sprintf(buffer1,"%2d",p_score);
+    lcd.printString(buffer1,WIDTH/2 + 22,1); 
+    
+}
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 Control.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Control.h	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,67 @@
+#ifndef CONTROL_H
+#define CONTROL_H
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+#include "Bullet.h"
+#include "Platform.h"
+#include "Obstacle.h"
+/** The control Class
+* @brief This program define the control system
+* @author Yang Hongxiao
+* @date 15/5/2020
+*/
+
+class Control
+{
+
+public:
+/** Constructor */
+    Control();
+/** Destructor */
+    ~Control();
+/** Set the inital value
+ * @param the value of the platform width (nt platform_w),the value of the platform height (int platform),
+ * the value of bullet size(int r),the value of bullet spped(int vv),
+ */
+    void init(int platform_w,int platform_h,int r,int vv);
+ /** Get the direction and magnitude
+ * @return the direction and magnitud
+ */   
+    void read_input(Gamepad &pad);
+/** Set the update value
+ * @param none
+ */
+    void update(Gamepad &pad);
+/** draw the bullet,platform,obstacle,the screen edge
+ * @param the screen(N5110 &lcd)
+ */
+    void draw(N5110 &lcd);
+    
+    
+private:
+
+    void check_wall_collision(Gamepad &pad);
+    void check_platform_collisions(Gamepad &pad);
+    void check_goal(Gamepad &pad);
+    void print_scores(N5110 &lcd);
+    void check__obstacle_collisios(Gamepad &pad);
+    
+    Platform _p;
+    Obstacle  _o;
+    Bullet _bullet;
+
+    int _platform_w;
+    int _platform_h;
+    int _r;
+    int _vv;
+    int _length;
+
+ 
+    Direction _d;
+    float _mag;
+
+};
+
+#endif
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 Gamepad/Gamepad.h
--- a/Gamepad/Gamepad.h	Sun Apr 26 13:17:59 2020 +0000
+++ b/Gamepad/Gamepad.h	Thu May 14 17:00:34 2020 +0000
@@ -43,14 +43,15 @@
 };
 
 /** Gamepad Class
- * @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
- * @author Dr Craig A. Evans
- * @author Dr Alex Valavanis
- */
+@brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds
+@author Dr Craig A. Evans
+@author Dr Alex Valanvanis
+@date Febraury 2017
+*/
 class Gamepad
 {
-public:
-/** Gamepad events 
+    public:
+    /** Gamepad events 
  * @brief List of events that can be registered on the gamepad
  */
 enum GamepadEvent {
@@ -65,7 +66,6 @@
     JOY_PRESSED,   ///< Joystick button has been pressed
     N_EVENTS       ///< A dummy flag that marks the end of the list
 };
-
 private:
     mbed::PwmOut *_led1;
     mbed::PwmOut *_led2;
@@ -99,6 +99,7 @@
     float _y0;
 
 public:
+
     /** Constructor */
     Gamepad();
 
@@ -143,23 +144,10 @@
      */
     bool check_event(GamepadEvent const id);
 
-    /**
-     * @brief   Get the raw binary event state
-     * @return  The event state as a binary code
-     * @details The check_event() function is likely to be more useful than
-     *          this, for testing whether a given event has occurred. It can be
-     *          useful for debugging via the terminal, however, for example:
-     *          @code
-     *          std::cout << gamepad.get_raw_event_state() << std::endl;
-     *          @endcode
-     */
-    inline std::bitset<N_EVENTS> get_raw_event_state() const {
-        return _event_state;
-    }
-
     /** Get magnitude of joystick movement
     * @returns value in range 0.0 to 1.0
     */
+
     float get_mag();
 
     /** Get angle of joystick movement
diff -r 4b02786450c0 -r a6ead8050c23 Obstacle.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Obstacle.cpp	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,34 @@
+#include "Obstacle.h"
+
+ 
+Obstacle::Obstacle(){
+    
+}
+    
+Obstacle::~Obstacle(){
+    
+}
+void Obstacle::init(){
+        
+       _x=20;
+       _y=10;
+}
+    
+void Obstacle::draw(N5110 &lcd){
+        
+     lcd.drawRect(_x,_y,20,2,FILL_BLACK);
+            
+}
+    
+void Obstacle::new_obstacle(){
+        
+        srand(time(NULL));
+        _x=rand()%(84-20); 
+        _y=rand()%(47-10);
+}
+ 
+Vector2D Obstacle::get_obstacle_pos() {
+    Vector2D p = {_x,_y};
+    return p;    
+}   
+        
diff -r 4b02786450c0 -r a6ead8050c23 Obstacle.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Obstacle.h	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,45 @@
+#ifndef OBSTACLE_H
+#define OBSTACLE_H
+ 
+#include "Gamepad.h"
+#include "mbed.h"
+#include "N5110.h"
+/** The bullet Class
+* @brief This program define the obstacle
+* @author Yang Hongxiao
+* @date 15/5/2020
+*/
+
+ 
+class Obstacle{
+    
+public:
+/** Constructor */
+Obstacle();
+/** Destructor */
+~Obstacle();
+/** Set the inital value
+ * @param the value of the Obstacle pos.x (int _x),the value of the Obstacle pos.y (int _y),
+ */                
+void init();
+/**draw the Obstacle
+ * @param the screen(N5110 &lcd)
+ */
+void draw(N5110 &lcd);   
+/** Get the obstacle position
+ * @return the current position
+ */  
+Vector2D get_obstacle_pos();
+/** Get the new obstacle position
+ * @return the current position of the new obstacle
+ */  
+void new_obstacle();
+            
+
+       
+private:    
+    int _x;
+    int _y;
+    
+};
+#endif
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 Platform.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platform.cpp	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,60 @@
+#include "Platform.h"
+
+Platform::Platform()
+{
+
+}
+Platform::~Platform()
+{
+
+}
+
+void Platform::init(int y,int h,int w)
+{
+    
+    _x=WIDTH/2 - w/2;
+    _y=y;
+    _h = h;
+    _w = w;
+    _vv = 2;  // default speed
+    _score = 0;
+  
+}
+
+
+void Platform::draw(N5110 &lcd)
+{
+    lcd.drawRect(_x,_y,_w,_h,FILL_BLACK);
+}
+
+void Platform::update(Direction d,float mag)
+{
+     
+     _vv = int(mag*10.0f);
+        if (d == W) {    //West
+        _x-=_vv;
+    } else if (d == E) {  //East
+        _x+=_vv;
+    }
+     if (_x < 1) {
+        _x = 1;
+    }
+    if (_x > WIDTH - _w - 1) {
+        _x = WIDTH - _w - 1;
+    }
+}
+     
+
+Vector2D Platform::get_pos() {
+    Vector2D p = {_x,_y};
+    return p;    
+}
+void Platform::add_score()
+{
+    _score++;
+}
+int Platform::get_score()
+{
+    return _score;
+}
+
diff -r 4b02786450c0 -r a6ead8050c23 Platform.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Platform.h	Thu May 14 17:00:34 2020 +0000
@@ -0,0 +1,57 @@
+#ifndef PLATFORM_H
+#define PLATFORM_H
+
+
+#include "mbed.h"
+#include "N5110.h"
+#include "Gamepad.h"
+/** The bullet Class
+* @brief This program define the bullets
+* @author Yang Hongxiao
+* @date 15/5/2020
+*/
+
+
+class Platform
+{
+public:
+/** Constructor */
+Platform();
+/** Destructor */
+~Platform();
+/** Set the inital value
+ * @param the y_position of the platformthe(int y), the value of the platform width (int w),the value of the platform height (int h),
+ */
+    void init(int y,int h,int w);
+/** draw the platform
+ * @param the screen(N5110 &lcd)
+ */
+    void draw(N5110 &lcd);
+/** Set the update value
+ * @param Direction (Direction d),magnitude(float mag)
+ */
+    void update(Direction d,float mag);
+/** Get the position
+ * @return the current position
+ */
+    Vector2D get_pos();
+/** Add the score
+ * @return the score++
+ */
+    void add_score();
+/** Get the score
+ * @return the current score
+ */
+    int get_score();
+
+
+private:
+
+    int _h;     //the height of the platform
+    int _w;     //the width of the platform
+    int _x;
+    int _y;
+    int _vv;     //the speed of the platform
+    int _score;
+};
+#endif
\ No newline at end of file
diff -r 4b02786450c0 -r a6ead8050c23 main.cpp
--- a/main.cpp	Sun Apr 26 13:17:59 2020 +0000
+++ b/main.cpp	Thu May 14 17:00:34 2020 +0000
@@ -1,11 +1,135 @@
-/* ELEC2645 Embedded Systems Project School of Electronic & Electrical Engineering University of Leeds 
- Name: Yang Hongxiao
- Username: el17hy
- Student ID Number: 201199678
- Date: 4th May,2020
- */ 
+/*
+ELEC2645 Embedded Systems Project
+School of Electronic & Electrical Engineering
+University of Leeds
+Name:  Yang Hongxiao
+Username:   el17hy
+Student ID Number:   201199678
+Date:    15/5/2020
+*/
+#include "mbed.h"
+#include "Gamepad.h"
+#include "N5110.h"
+#include "Control.h"
+#include "Bullet.h"
+#include "Platform.h"
+#include "Obstacle.h"
+
+
+#define PLATFORM_W 10
+#define PLATFORM_H 2
+#define R 2          //the size of the ball
+#define VV 3         //the velocity of the ball
+
+
+
+struct UserInput {
+    Direction d;
+    float mag;
+};
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
+Gamepad pad;
+Bullet bullet;
+Platform p;
+Control con;
+
+
+void init();
+void bullet_init();
+void render();
+void welcome();
+void introduction();
+void introduction1();
+
+
+
+int main()
+{
+
 
- #include "mbed.h"
- #include "Gamepad.h"
- #include "N5110.h"
- 
\ No newline at end of file
+    int fps = 8;  
+while(1){
+    init();     
+    welcome(); 
+    introduction();      //the introduction of the game
+    introduction1();
+    render(); 
+    wait(1.0f/fps); 
+   
+while (1){
+  
+        con.read_input(pad);
+        con.update(pad);
+        render();
+        wait(1.0f/fps);
+        
+         if( pad.check_event(Gamepad::B_PRESSED) == true) {
+             lcd.refresh();
+             wait(1.0f/fps);
+             break;
+        }
+        }
+    }
+}
+
+void init()
+{
+
+    lcd.init();
+    pad.init();
+    con.init(PLATFORM_W,PLATFORM_H,R,VV);
+
+}
+
+void render()
+{
+    lcd.clear();  
+    con.draw(lcd);
+    lcd.refresh();
+}
+
+void welcome() {
+    
+    lcd.printString("     Shoot!    ",0,1);
+    lcd.printString("  Press Start ",0,4);
+    lcd.refresh();
+
+    while ( pad.check_event(Gamepad::START_PRESSED) == false) {
+        pad.leds_on();
+        wait(0.1);
+        pad.leds_off();
+        wait(0.1);
+    }
+ 
+}
+void introduction() {
+    lcd.clear();
+    lcd.printString("  shoot the  ",0,1);
+    lcd.printString("   obstacle  ",0,2);
+    lcd.printString("   Press A   ",0,3);
+    lcd.printString("  to continue ",0,4);
+    lcd.refresh();
+    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
+        pad.leds_on();
+        wait(0.1);
+        pad.leds_off();
+        wait(0.1);
+    }
+ 
+}
+void introduction1() {
+    lcd.clear();
+    lcd.printString("  If you want ",0,1);
+    lcd.printString("   to  quit  ",0,2);
+    lcd.printString("   Press B   ",0,3);
+    lcd.printString("   to quit  ",0,4);
+    lcd.refresh();
+
+    while ( pad.check_event(Gamepad::A_PRESSED) == false) {
+        pad.leds_on();
+        wait(0.1);
+        pad.leds_off();
+        wait(0.1);
+    }
+ 
+}
diff -r 4b02786450c0 -r a6ead8050c23 mbed.bld
--- a/mbed.bld	Sun Apr 26 13:17:59 2020 +0000
+++ b/mbed.bld	Thu May 14 17:00:34 2020 +0000
@@ -1,1 +1,1 @@
-https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/176b8275d35d
\ No newline at end of file