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.
Gold/Gold.cpp
- Committer:
- ZhongYufan
- Date:
- 2020-04-26
- Revision:
- 8:c5969685cf02
- Parent:
- 1:9c7bb3db32bc
- Child:
- 10:51870f8e2e1a
File content as of revision 8:c5969685cf02:
#include "Gold.h"
Gold::Gold()
{
}
Gold::~Gold()
{
}
void Gold::init(int gold_num)
{
_gold_num = gold_num;
_gold_left = _gold_num;
srand(time(NULL));
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);
}
}
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.refresh();
}
void Gold::update()
{
for (int i=0;i<_gold_num;i++){
while(_gold_caught[i]==1){
_y[i]-=1;
if (_y[i]<=14) {
gold_reached(i);
}
}
}
}
void Gold::gold_caught(int caught_i)
{
_gold_caught[caught_i]=1;
}
void Gold::gold_reached(int reached_i)
{
_gold_reached[reached_i]=1;
_gold_left--;
}
int Gold::get_reached_num()
{
int n=0;
for (int i=0;i<_gold_num;i++){
if (_gold_reached[i]==1){
n+=1;
}
}
return n;
}
int Gold::get_left_num()
{
int left_n = _gold_left;
return left_n;
}
Vector2D Gold::get_pos(int gold_i)
{
Vector2D p = {_x[gold_i],_y[gold_i]};
return p;
}
void Gold::set_pos(Vector2D p) {
for (int i=0;i<_gold_num;i++){
while(_gold_reached[i]==0){
_x[i] = p.x;
_y[i] = p.y;
}
}
}