ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el19zf

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
el19zf
Date:
Wed Apr 29 05:10:57 2020 +0000
Parent:
6:dce38fe4e092
Child:
8:8287d2ef965d
Commit message:
change the initial position of shots, and add some comment

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
shot/shot.cpp Show annotated file Show diff for this revision Revisions of this file
shot/shot.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Apr 28 15:54:41 2020 +0000
+++ b/main.cpp	Wed Apr 29 05:10:57 2020 +0000
@@ -1,4 +1,4 @@
-/* 
+/*
 ELEC2645 Embedded Systems Project
 School of Electronic & Electrical Engineering
 University of Leeds
@@ -30,24 +30,25 @@
 volatile int timer_flag = 0;
 
 //    functions
-void flip() { 
+void flip()
+{
     timer_flag = 1;
 }
 void init();
 void control_people();
-void  shot_update();
+void shot_update();
 
 
 int main()
 {
     //initial
     init();
-    
+
     ticker.attach(&flip,5);
-    
+
     //a infinite loop to control position of the people, update the game state
     while(1) {
-        if(timer_flag == 1){
+        if(timer_flag == 1) {
             timer_flag = 0;
             if(shot._size < 30)
                 shot._size = shot._size + 2;
@@ -59,10 +60,10 @@
         lcd.refresh();
         //printf("shot refresh\n");
         //printf("size = %d\n",shot._size);
-        wait_ms(100);//fps = 10
+        wait_ms(200);//fps = 5
 
     }
-        
+
 }
 
 
@@ -72,8 +73,8 @@
     lcd.setContrast(0.5);
     engine.init();
     pad.init();
+    shot.init();
     lcd.refresh();
-    shot.init();
 }
 
 void control_people()
@@ -84,9 +85,8 @@
 }
 void shot_update()
 {
-        shot.update();
-        shot.delete_shot();
-        shot.gen_shot();
-        shot.draw(lcd);
+    shot.update();
+    shot.delete_shot();
+    shot.gen_shot();
+    shot.draw(lcd);
 }
-        
\ No newline at end of file
--- a/shot/shot.cpp	Tue Apr 28 15:54:41 2020 +0000
+++ b/shot/shot.cpp	Wed Apr 29 05:10:57 2020 +0000
@@ -1,121 +1,123 @@
 #include "shot.h"
 
+int shots[4][3][3] = {
+    {       {1,1,0},{1,1,0},{0,0,1},        },
+    {       {1,0,0},{1,1,1},{1,0,0},        },
+    {       {1,1,1},{0,1,0},{0,1,0},        },
+    {       {0,1,0},{1,1,1},{0,1,0},        }
+};
 
-shot::shot() {
-    
-}
+shot::shot()
+{
 
-shot::~shot() {
-    
 }
 
-const int shots[4][3][3] = {
-{       
-        {1,1,0},
-        {1,1,0},
-        {0,0,1},        },
-{       
-        {1,0,0},
-        {1,1,1},
-        {1,0,0},        },
+shot::~shot()
 {
-        {1,1,1},
-        {0,1,0},
-        {0,1,0},        },
+
+}
+
+void shot::init()
 {
-        {0,1,0},
-        {1,1,1},
-        {0,1,0},        }
-};
 
-void shot::init() {
-        
-        _size = 10;
-        _p.resize(_size);
-        srand(time(NULL));
-        for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-            init_pos(i);
-            (*i).type = rand_type();//randomise initial type 
-            (*i).dir = rand() % 8;//randomise initial direction
-        }
-        
+    _size = 10;
+    _p.resize(_size);
+    srand(time(NULL));
+    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
+        init_pos(i);
+        (*i).type = rand() % 4;//randomise initial type
+        (*i).dir = rand() % 6;//randomise initial direction
+    }
+
 }
 
-/*Direction shot::rand_dir() {
-    
-    srand(time(NULL));
-    int d_num = rand() % 8; //randomise initial direction
-    if (d_num == 0) { d = N; } else
-    if (d_num == 1) { d = NE; } else
-    if (d_num == 2) { d = E; } else
-    if (d_num == 3) { d = SE; } else
-    if (d_num == 4) { d = S; } else
-    if (d_num == 5) { d = SW; } else
-    if (d_num == 6) { d = W; } else
-                    { d = NW;}   
-    return d;
-}*/
+void shot::init_pos(shot_posandtype* i)
+{
 
-int shot::rand_type() {
+    int num_pos = rand() % 6;//randomise initial position
+    if (num_pos == 0) {
+        (*i).x = 0;
+        (*i).y = HEIGHT/4;
+    }//left
+    if (num_pos == 1) {
+        (*i).x = 0;
+        (*i).y = HEIGHT/2;
+    }//left
+    if (num_pos == 2) {
+        (*i).x = 0;
+        (*i).y = 3*HEIGHT/4;
+    }//left
+    if (num_pos == 3) {
+        (*i).x = WIDTH-3;
+        (*i).y = HEIGHT/4;
+    }//right
+    if (num_pos == 4) {
+        (*i).x = WIDTH-3;
+        (*i).y = HEIGHT/2;
+    }//right
+    if (num_pos == 5) {
+        (*i).x = WIDTH-3;
+        (*i).y = 3*HEIGHT/4;
+    }//right
 
-    int type = rand() % 4;//set 4 type of shot
-    return type; 
 }
 
-void shot::init_pos(shot_posandtype* i) {
-    
-    int num_pos = rand_type();
-    if (num_pos == 0) {  (*i).x = (WIDTH/2-1);
-                         (*i).y = 0;           }//up
-    if (num_pos == 1) {  (*i).x = (WIDTH/2-1);
-                         (*i).y = (HEIGHT-3);  }//down
-    if (num_pos == 2) {  (*i).x = 0;
-                         (*i).y = (HEIGHT/2-1);}//left
-    if (num_pos == 3) {  (*i).x = (WIDTH-3);
-                         (*i).y = (HEIGHT/2-1);}//right
-}
-
-void shot::gen_shot() {
+void shot::gen_shot()
+{
     _p.resize(_size);
     for(std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-        if(((*i).x == 0)&&((*i).y == 0)){
+        if(((*i).x == 0)&&((*i).y == 0)) {
             init_pos(i);
-            (*i).type = rand_type();//randomise initial type 
-            (*i).dir = rand() % 8;//randomise initial direction
+            (*i).type = rand() % 4;//randomise initial type
+            (*i).dir = rand() % 6;//randomise initial direction
         }
-    }    
+    }
 }
 
 
 
-void shot::update() {
+void shot::update()
+{
     for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
-        if ((*i).dir == 0) {     (*i).x +=0; (*i).y +=2; } else
-        if ((*i).dir == 1) {     (*i).x +=1; (*i).y +=1; } else
-        if ((*i).dir == 2) {     (*i).x +=2; (*i).y +=0; } else
-        if ((*i).dir == 3) {     (*i).x +=1; (*i).y -=1; } else
-        if ((*i).dir == 4) {     (*i).x +=0; (*i).y -=2; } else
-        if ((*i).dir == 5) {     (*i).x -=1; (*i).y -=1; } else
-        if ((*i).dir == 6) {     (*i).x -=2; (*i).y -=0; } else
-        if ((*i).dir == 7) {     (*i).x -=1; (*i).y +=1; }  
-    }  
+        if ((*i).dir == 0) {
+            (*i).x +=1;
+            (*i).y +=1;//SE
+        } else if ((*i).dir == 1) {
+            (*i).x +=1;
+            (*i).y +=0;//E
+        } else if ((*i).dir == 2) {
+            (*i).x +=1;
+            (*i).y -=1;//NE
+        } else if ((*i).dir == 3) {
+            (*i).x -=1;
+            (*i).y -=1;//NW
+        } else if ((*i).dir == 4) {
+            (*i).x -=1;
+            (*i).y -=0;//W
+        } else if ((*i).dir == 5) {
+            (*i).x -=1;    
+            (*i).y +=1;//SW
+        }
+    }
 }
 
 
-void shot::draw(N5110 &lcd){
-    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
+void shot::draw(N5110 &lcd)
+{
+    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
         lcd.drawSprite((*i).x,(*i).y,3,3,(int*)shots[(*i).type]);
-        printf("coordinate = %d,%d\n",(*i).x,(*i).y);
+        //printf("coordinate = %d,%d\n",(*i).x,(*i).y);
     }
 }
 
-void shot::delete_shot() {
-    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++){
-        if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)){
+void shot::delete_shot()
+{
+    for (std::vector<shot_posandtype>::iterator i = _p.begin(); i < _p.end(); i++) {
+        // if beyoud border, delete it and generate new one, keep total number constant
+        if(((*i).x < 0)||((*i).x > WIDTH)||((*i).y < 0)||((*i).y > HEIGHT)) {
             init_pos(i);
             (*i).type = ((*i).type + 2)%4;
-            (*i).dir = ((*i).dir + 1)%4;
+            (*i).dir = ((*i).dir + 1)%6; // increase randomness
         }
     }
 }
-            
\ No newline at end of file
--- a/shot/shot.h	Tue Apr 28 15:54:41 2020 +0000
+++ b/shot/shot.h	Wed Apr 29 05:10:57 2020 +0000
@@ -13,53 +13,47 @@
     @author Zeyu Feng
     @13 April 2020
    */
-   
+
+
 
-    
 
-struct shot_posandtype{
+struct shot_posandtype {
     int x;
     int y;
     int type;
     int dir;
 };
 
-class shot {
- 
-public:  
+class shot
+{
+
+public:
     //constructor
     shot();
     //destructor
     ~shot();
-    
+
     void init();
-    
+
     void init_pos(shot_posandtype* i);
-    
-    //int rand_dir();random direction of shot
-    
-    int rand_type();//give a random number from 0 - 3
-    
-    void gen_shot();//3 types direction 
-    
+
+    void gen_shot();
+
     void update();
-    
-    
-    
+
     void draw(N5110 &lcd);
-    
+
     void delete_shot();
-    
-    int _size;
-    
+
+    int _size;//use for resize number of vector
+
 private:
-    
+
     std::vector<shot_posandtype> _p;
     
-    
 };
 #endif
-    
-    
-    
+
 
+
+