Yufan Zhong / Mbed 2 deprecated GOLD_MINER

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Gold.cpp Source File

Gold.cpp

00001 #include "Gold.h"
00002 
00003 Gold::Gold()
00004 {
00005 
00006 }
00007 
00008 Gold::~Gold()
00009 {
00010 
00011 }
00012 // initialise the gold
00013 void Gold::init(int gold_num)
00014 {
00015      _gold_num = gold_num; //number of gold
00016      _gold_left = _gold_num; //left number
00017       //create random positions for golds
00018      srand(time(NULL)); 
00019      _gold_speed=1;
00020      for (int i=0;i<_gold_num;i++){
00021        _gold_caught[i] = 0;
00022        _gold_reached[i] = 0;
00023        _x[i] = 3+(rand()%78);
00024        _y[i] = 19+(rand()%27); 
00025      }
00026 }
00027 
00028 //draw the golds
00029 void Gold::draw(N5110 &lcd)
00030 {
00031     for (int i=0;i<9;i++){
00032         if(_gold_reached[i]==0){
00033           lcd.drawCircle(_x[i],_y[i],2,FILL_BLACK); // draw circles as golds
00034           }
00035     } 
00036     lcd.refresh();
00037 }
00038 
00039 //update the positions of golds
00040 void Gold::update()
00041 {
00042     for (int i=0;i<_gold_num;i++) {
00043         //if the gold is been caught, it will go up
00044         if (_gold_caught[i]==1&&_gold_reached[i]==0) {
00045             _y[i]-=_gold_speed;
00046             //if gold reached the upper boundary, reached flag will be 1
00047             //and left number will -1
00048            if (_y[i]<=17) {
00049               _gold_reached[i]=1;
00050               _gold_left--;
00051            }
00052         }
00053     }
00054 }
00055 
00056 //mark the gold has been caught
00057 void Gold::gold_caught(int caught_i)
00058 {
00059     _gold_caught[caught_i]=1;
00060 }
00061 
00062 // return the lifted gold number
00063 int Gold::get_reached_num()
00064 {
00065     int n=0;
00066     for (int i=0;i<_gold_num;i++){
00067         if (_gold_reached[i]==1){
00068              n+=1;
00069             }
00070         }
00071     return n;
00072 }
00073 
00074 //reyurn the number of left gold
00075 int Gold::get_left_num()
00076 {
00077     int left_n = _gold_left;
00078     return left_n;
00079 }
00080 
00081 //return the position of gold with serial number gold_i
00082 Vector2D Gold::get_pos(int gold_i)
00083 {
00084     Vector2D p = {_x[gold_i],_y[gold_i]};
00085     return p;
00086 }
00087 
00088 //set the position of gold with serial number gold_i
00089 void Gold::set_pos(Vector2D p) {
00090     for (int i=0;i<_gold_num;i++){
00091         if(_gold_reached[i]==0){
00092           _x[i] = p.x;
00093           _y[i] = p.y;
00094         }
00095     }
00096 }
00097 
00098 //set the rising speed of gold
00099 void Gold::set_speed(int speed) {
00100     _gold_speed = speed;
00101 }