Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: Gold/Gold.cpp
- Revision:
- 19:08c582bcdc98
- Parent:
- 12:07a9f2140d9b
diff -r d4fccdf8d90e -r 08c582bcdc98 Gold/Gold.cpp
--- a/Gold/Gold.cpp Tue May 12 12:38:14 2020 +0000
+++ b/Gold/Gold.cpp Wed May 13 14:07:20 2020 +0000
@@ -9,40 +9,42 @@
{
}
-
+// initialise the gold
void Gold::init(int gold_num)
{
- _gold_num = gold_num;
- _gold_left = _gold_num;
- srand(time(NULL));
- _gold_speed=1;
- for (int i=0;i<_gold_num;i++){
- _gold_caught[i] = 0;
- _gold_reached[i] = 0;
-
- _x[i] = 3+(rand()%78);
- _y[i] = 19+(rand()%27);
- }
-
-
+ _gold_num = gold_num; //number of gold
+ _gold_left = _gold_num; //left number
+ //create random positions for golds
+ srand(time(NULL));
+ _gold_speed=1;
+ for (int i=0;i<_gold_num;i++){
+ _gold_caught[i] = 0;
+ _gold_reached[i] = 0;
+ _x[i] = 3+(rand()%78);
+ _y[i] = 19+(rand()%27);
+ }
}
+//draw the golds
void Gold::draw(N5110 &lcd)
{
for (int i=0;i<9;i++){
if(_gold_reached[i]==0){
- lcd.drawCircle(_x[i],_y[i],2,FILL_BLACK); // lcd.drawCircle(_x[i],_y[i],3,FILL_BLACK);
+ lcd.drawCircle(_x[i],_y[i],2,FILL_BLACK); // draw circles as golds
}
}
lcd.refresh();
}
+//update the positions of golds
void Gold::update()
{
for (int i=0;i<_gold_num;i++) {
-
+ //if the gold is been caught, it will go up
if (_gold_caught[i]==1&&_gold_reached[i]==0) {
_y[i]-=_gold_speed;
+ //if gold reached the upper boundary, reached flag will be 1
+ //and left number will -1
if (_y[i]<=17) {
_gold_reached[i]=1;
_gold_left--;
@@ -51,11 +53,13 @@
}
}
+//mark the gold has been caught
void Gold::gold_caught(int caught_i)
{
_gold_caught[caught_i]=1;
}
+// return the lifted gold number
int Gold::get_reached_num()
{
int n=0;
@@ -67,27 +71,31 @@
return n;
}
+//reyurn the number of left gold
int Gold::get_left_num()
{
int left_n = _gold_left;
return left_n;
}
+//return the position of gold with serial number gold_i
Vector2D Gold::get_pos(int gold_i)
{
Vector2D p = {_x[gold_i],_y[gold_i]};
return p;
}
+//set the position of gold with serial number gold_i
void Gold::set_pos(Vector2D p) {
for (int i=0;i<_gold_num;i++){
if(_gold_reached[i]==0){
_x[i] = p.x;
_y[i] = p.y;
- }
+ }
}
}
+//set the rising speed of gold
void Gold::set_speed(int speed) {
_gold_speed = speed;
}
\ No newline at end of file