This is the first version of a Fishing Mini-game. It uses a navigational switch as a button and a joystick, playing wav files from a SD card, and a uLCD screen to show the fishing game

Dependencies:   4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player

Committer:
jd0205
Date:
Thu Mar 17 18:47:33 2016 +0000
Revision:
1:8dd8fafa7fc8
Parent:
0:5d811b6879d5
Added comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jd0205 0:5d811b6879d5 1 class Fish
jd0205 0:5d811b6879d5 2 {
jd0205 0:5d811b6879d5 3 public:
jd0205 0:5d811b6879d5 4 Fish();
jd0205 0:5d811b6879d5 5 void reset();
jd0205 0:5d811b6879d5 6 int read();
jd0205 0:5d811b6879d5 7 int x();
jd0205 0:5d811b6879d5 8 int y();
jd0205 0:5d811b6879d5 9 void move(int n);
jd0205 0:5d811b6879d5 10 void movement();
jd0205 0:5d811b6879d5 11 bool failed();
jd0205 0:5d811b6879d5 12 private:
jd0205 0:5d811b6879d5 13 int x_pos;
jd0205 0:5d811b6879d5 14 int y_pos;
jd0205 0:5d811b6879d5 15 int dir;
jd0205 0:5d811b6879d5 16 int size;
jd0205 0:5d811b6879d5 17 int time;
jd0205 0:5d811b6879d5 18 int clk;
jd0205 0:5d811b6879d5 19 };
jd0205 0:5d811b6879d5 20
jd0205 0:5d811b6879d5 21 Fish::Fish() {
jd0205 0:5d811b6879d5 22 x_pos = 64;
jd0205 0:5d811b6879d5 23 y_pos = 64;
jd0205 0:5d811b6879d5 24 if (rand() % 100 > 50) {
jd0205 0:5d811b6879d5 25 dir = 1;
jd0205 0:5d811b6879d5 26 } else {
jd0205 0:5d811b6879d5 27 dir = -1;
jd0205 0:5d811b6879d5 28 }
jd0205 1:8dd8fafa7fc8 29 size = (rand() % 100) + 20; //Size of the fish will determine how fast it can move
jd0205 1:8dd8fafa7fc8 30 time = ((rand() % 5) + 1)*5; //Time determines how long it will either head left or right
jd0205 1:8dd8fafa7fc8 31 clk = 0; //Shows how much time has passed
jd0205 0:5d811b6879d5 32 }
jd0205 0:5d811b6879d5 33
jd0205 0:5d811b6879d5 34 void Fish::reset() {
jd0205 0:5d811b6879d5 35 x_pos = 64;
jd0205 0:5d811b6879d5 36 y_pos = 64;
jd0205 0:5d811b6879d5 37 if (rand() % 100 > 50) {
jd0205 0:5d811b6879d5 38 dir = 1;
jd0205 0:5d811b6879d5 39 } else {
jd0205 0:5d811b6879d5 40 dir = -1;
jd0205 0:5d811b6879d5 41 }
jd0205 0:5d811b6879d5 42 size = (rand() % 100) + 20;
jd0205 0:5d811b6879d5 43 time = ((rand() % 5) + 1)*5;
jd0205 0:5d811b6879d5 44 clk = 0;
jd0205 0:5d811b6879d5 45 }
jd0205 0:5d811b6879d5 46
jd0205 0:5d811b6879d5 47 int Fish::x() {
jd0205 0:5d811b6879d5 48 return x_pos;
jd0205 0:5d811b6879d5 49 }
jd0205 0:5d811b6879d5 50
jd0205 0:5d811b6879d5 51 int Fish::y() {
jd0205 0:5d811b6879d5 52 return y_pos;
jd0205 0:5d811b6879d5 53 }
jd0205 0:5d811b6879d5 54
jd0205 1:8dd8fafa7fc8 55 void Fish::move(int n) { //Changes the x-position of the fish or moves it
jd0205 0:5d811b6879d5 56 x_pos = x_pos + n;
jd0205 0:5d811b6879d5 57 }
jd0205 0:5d811b6879d5 58
jd0205 1:8dd8fafa7fc8 59 void Fish::movement() { //The movement of the fish, basically going back and forth
jd0205 0:5d811b6879d5 60 if(clk >= time) {
jd0205 0:5d811b6879d5 61 clk = 0;
jd0205 0:5d811b6879d5 62 time = ((rand() % 5) + 1)*5;
jd0205 0:5d811b6879d5 63 dir = dir * -1;
jd0205 0:5d811b6879d5 64 } else {
jd0205 0:5d811b6879d5 65 if (clk < (size/15)) {
jd0205 0:5d811b6879d5 66 move(dir * clk);
jd0205 0:5d811b6879d5 67 } else {
jd0205 0:5d811b6879d5 68 move(dir * (size/15));
jd0205 0:5d811b6879d5 69 }
jd0205 0:5d811b6879d5 70 clk++;
jd0205 0:5d811b6879d5 71 }
jd0205 0:5d811b6879d5 72 }
jd0205 0:5d811b6879d5 73
jd0205 1:8dd8fafa7fc8 74 bool Fish::failed() { //When it reaches the outer bounds, the fish has escaped
jd0205 0:5d811b6879d5 75 if(x_pos < 30 || x_pos > 98) {
jd0205 0:5d811b6879d5 76 return false;
jd0205 0:5d811b6879d5 77 } else {
jd0205 0:5d811b6879d5 78 return true;
jd0205 0:5d811b6879d5 79 }
jd0205 0:5d811b6879d5 80 }