Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

Committer:
el18a2k
Date:
Wed Apr 21 11:08:02 2021 +0000
Revision:
11:7c1e2a9303d3
Parent:
10:e221bd1ce3ec
Child:
12:20ac766b3175
highscore file writing complete

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el18a2k 10:e221bd1ce3ec 1 #include "Game.h"
el18a2k 10:e221bd1ce3ec 2
el18a2k 10:e221bd1ce3ec 3 ShiftReg _shift;
el18a2k 10:e221bd1ce3ec 4 Graphics _graphics;
el18a2k 10:e221bd1ce3ec 5
el18a2k 11:7c1e2a9303d3 6 BusOut _RGB_led(p24,p23,p22); //RGB bus MSB - LSB, inverted logic 1 = low
el18a2k 11:7c1e2a9303d3 7
el18a2k 10:e221bd1ce3ec 8 //plays the game
el18a2k 10:e221bd1ce3ec 9 void Game::gamePlay(N5110 &lcd, DIRECTION direction){
el18a2k 10:e221bd1ce3ec 10 lcd.clear();
el18a2k 10:e221bd1ce3ec 11 lcd.drawRect(0,0,40,9, FILL_TRANSPARENT); //creates the growth bar
el18a2k 10:e221bd1ce3ec 12 lcd.drawRect(0,8,84,40,FILL_TRANSPARENT); //draws screen border
el18a2k 10:e221bd1ce3ec 13 lcd.drawRect(0,0,rect_width,9, FILL_BLACK); //fills the growth bar
el18a2k 10:e221bd1ce3ec 14
el18a2k 10:e221bd1ce3ec 15 //check if enemy fish if fully off screen
el18a2k 10:e221bd1ce3ec 16 if(X == -24){
el18a2k 10:e221bd1ce3ec 17 X = 85; //reset enemy fish position to 85
el18a2k 11:7c1e2a9303d3 18 Y = rand() % 36 + 9; //randomly generate new Y position for enemy fish
el18a2k 10:e221bd1ce3ec 19 E_FISH = rand() % 4 + 1; //randomly select a new enemy fish
el18a2k 10:e221bd1ce3ec 20 }
el18a2k 10:e221bd1ce3ec 21
el18a2k 10:e221bd1ce3ec 22 fishPos(direction);
el18a2k 10:e221bd1ce3ec 23 drawFish(lcd);
el18a2k 10:e221bd1ce3ec 24 enemyFish(lcd);
el18a2k 10:e221bd1ce3ec 25 Collision();
el18a2k 10:e221bd1ce3ec 26 Score(lcd);
el18a2k 10:e221bd1ce3ec 27 Lives();
el18a2k 10:e221bd1ce3ec 28
el18a2k 10:e221bd1ce3ec 29 X--;
el18a2k 10:e221bd1ce3ec 30 lcd.refresh();
el18a2k 10:e221bd1ce3ec 31 wait_ms(1000/30);
el18a2k 10:e221bd1ce3ec 32 }
el18a2k 10:e221bd1ce3ec 33
el18a2k 10:e221bd1ce3ec 34 //initiates game. 20, 20, 1
el18a2k 10:e221bd1ce3ec 35 void Game::init(N5110 &lcd, int x, int y, int Fish_size){
el18a2k 10:e221bd1ce3ec 36 _x = x;
el18a2k 10:e221bd1ce3ec 37 _y = y;
el18a2k 10:e221bd1ce3ec 38 X = 85;
el18a2k 10:e221bd1ce3ec 39 Y = 20;
el18a2k 10:e221bd1ce3ec 40 FISH_SIZE = Fish_size;
el18a2k 10:e221bd1ce3ec 41 E_FISH = 1;
el18a2k 10:e221bd1ce3ec 42 SCORE = 0;
el18a2k 10:e221bd1ce3ec 43 check_score = 0;
el18a2k 10:e221bd1ce3ec 44 rect_width = 0;
el18a2k 10:e221bd1ce3ec 45 barIncrement = 0;
el18a2k 10:e221bd1ce3ec 46 check_width = 0;
el18a2k 10:e221bd1ce3ec 47 lives = 3;
el18a2k 10:e221bd1ce3ec 48 _shift.write(0x4F); //initiate lives
el18a2k 10:e221bd1ce3ec 49
el18a2k 10:e221bd1ce3ec 50 //draw fish
el18a2k 10:e221bd1ce3ec 51 drawFish(lcd);
el18a2k 10:e221bd1ce3ec 52 }
el18a2k 10:e221bd1ce3ec 53
el18a2k 10:e221bd1ce3ec 54 //draws player fish
el18a2k 10:e221bd1ce3ec 55 void Game::drawFish(N5110 &lcd){
el18a2k 10:e221bd1ce3ec 56
el18a2k 10:e221bd1ce3ec 57 if (FISH_SIZE == 1){
el18a2k 10:e221bd1ce3ec 58 //draw first fish
el18a2k 10:e221bd1ce3ec 59 _graphics.Fish_1(lcd, _x, _y);
el18a2k 10:e221bd1ce3ec 60 FISH_HEIGHT = 4;
el18a2k 10:e221bd1ce3ec 61 FISH_WIDTH = 9;
el18a2k 10:e221bd1ce3ec 62 CENTRE_X = _x + (FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 63 CENTRE_Y = _y + (FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 64
el18a2k 10:e221bd1ce3ec 65 }else if(FISH_SIZE == 2){
el18a2k 10:e221bd1ce3ec 66 //draw second fish
el18a2k 10:e221bd1ce3ec 67 _graphics.Fish_2(lcd, _x, _y);
el18a2k 10:e221bd1ce3ec 68 FISH_HEIGHT = 6;
el18a2k 10:e221bd1ce3ec 69 FISH_WIDTH = 14;
el18a2k 10:e221bd1ce3ec 70 CENTRE_X = _x + (FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 71 CENTRE_Y = _y + (FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 72
el18a2k 10:e221bd1ce3ec 73 }else if(FISH_SIZE == 3){
el18a2k 10:e221bd1ce3ec 74 //draw third fish
el18a2k 10:e221bd1ce3ec 75 _graphics.Fish_3(lcd, _x, _y);
el18a2k 10:e221bd1ce3ec 76 FISH_HEIGHT = 8;
el18a2k 10:e221bd1ce3ec 77 FISH_WIDTH = 17;
el18a2k 10:e221bd1ce3ec 78 CENTRE_X = _x + (FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 79 CENTRE_Y = _y + (FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 80
el18a2k 10:e221bd1ce3ec 81 }else if(FISH_SIZE == 4){
el18a2k 10:e221bd1ce3ec 82 //draw final fish
el18a2k 10:e221bd1ce3ec 83 _graphics.Fish_4(lcd, _x, _y);
el18a2k 10:e221bd1ce3ec 84 FISH_HEIGHT = 12;
el18a2k 10:e221bd1ce3ec 85 FISH_WIDTH = 23;
el18a2k 10:e221bd1ce3ec 86 CENTRE_X = _x + (FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 87 CENTRE_Y = _y + (FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 88 }
el18a2k 10:e221bd1ce3ec 89 }
el18a2k 10:e221bd1ce3ec 90
el18a2k 10:e221bd1ce3ec 91 //updates fish position
el18a2k 10:e221bd1ce3ec 92 void Game::fishPos(DIRECTION direction){
el18a2k 10:e221bd1ce3ec 93
el18a2k 10:e221bd1ce3ec 94 //takes the joystick input and alters the value and x & y accordingly
el18a2k 10:e221bd1ce3ec 95 if(direction.joy == N){
el18a2k 10:e221bd1ce3ec 96 _y--;
el18a2k 10:e221bd1ce3ec 97 }else if(direction.joy == S){
el18a2k 10:e221bd1ce3ec 98 _y++;
el18a2k 10:e221bd1ce3ec 99 }else if(direction.joy == E){
el18a2k 10:e221bd1ce3ec 100 _x++;
el18a2k 10:e221bd1ce3ec 101 }else if(direction.joy == W){
el18a2k 10:e221bd1ce3ec 102 _x--;
el18a2k 10:e221bd1ce3ec 103 }else if(direction.joy == NE){
el18a2k 10:e221bd1ce3ec 104 _y--;
el18a2k 10:e221bd1ce3ec 105 _x++;
el18a2k 10:e221bd1ce3ec 106 }else if(direction.joy == NW){
el18a2k 10:e221bd1ce3ec 107 _y--;
el18a2k 10:e221bd1ce3ec 108 _x--;
el18a2k 10:e221bd1ce3ec 109 }else if(direction.joy == SE){
el18a2k 10:e221bd1ce3ec 110 _y++;
el18a2k 10:e221bd1ce3ec 111 _x++;
el18a2k 10:e221bd1ce3ec 112 }else if(direction.joy == SW){
el18a2k 10:e221bd1ce3ec 113 _y++;
el18a2k 10:e221bd1ce3ec 114 _x--;
el18a2k 10:e221bd1ce3ec 115 }else if(direction.joy == CENTRE){
el18a2k 10:e221bd1ce3ec 116 _x = _x;
el18a2k 10:e221bd1ce3ec 117 _y = _y;
el18a2k 10:e221bd1ce3ec 118 }
el18a2k 10:e221bd1ce3ec 119
el18a2k 10:e221bd1ce3ec 120 //creates the upper x and y boundaries
el18a2k 10:e221bd1ce3ec 121 x_bound = _x + FISH_WIDTH;
el18a2k 10:e221bd1ce3ec 122 y_bound = _y + FISH_HEIGHT;
el18a2k 10:e221bd1ce3ec 123
el18a2k 10:e221bd1ce3ec 124 //check play position agains the boundaries of the playing area
el18a2k 10:e221bd1ce3ec 125 if(_x <= 1){
el18a2k 10:e221bd1ce3ec 126 _x = 1;
el18a2k 11:7c1e2a9303d3 127
el18a2k 10:e221bd1ce3ec 128 }else if (x_bound >= 82){
el18a2k 10:e221bd1ce3ec 129 _x = 82 - FISH_WIDTH;
el18a2k 11:7c1e2a9303d3 130
el18a2k 10:e221bd1ce3ec 131 }else if (_y <= 9){
el18a2k 10:e221bd1ce3ec 132 _y = 9;
el18a2k 11:7c1e2a9303d3 133
el18a2k 10:e221bd1ce3ec 134 }else if (y_bound >= 47){
el18a2k 10:e221bd1ce3ec 135 _y = 47 - FISH_HEIGHT;
el18a2k 10:e221bd1ce3ec 136 }
el18a2k 10:e221bd1ce3ec 137 }
el18a2k 10:e221bd1ce3ec 138
el18a2k 10:e221bd1ce3ec 139 //Keeps score
el18a2k 10:e221bd1ce3ec 140 int Game::Score(N5110 &lcd){
el18a2k 10:e221bd1ce3ec 141 //print score to screen
el18a2k 10:e221bd1ce3ec 142 s_length = sprintf(_score,"%2d",SCORE);
el18a2k 10:e221bd1ce3ec 143 lcd.printString(_score,54,0);
el18a2k 10:e221bd1ce3ec 144
el18a2k 10:e221bd1ce3ec 145 //score limits to grow fish
el18a2k 10:e221bd1ce3ec 146 if (SCORE < 100){
el18a2k 10:e221bd1ce3ec 147 FISH_SIZE = 1; //smallest fish
el18a2k 10:e221bd1ce3ec 148 barIncrement = 4; //increments the width of the growth bar by 4 pixels
el18a2k 11:7c1e2a9303d3 149
el18a2k 10:e221bd1ce3ec 150 }else if(SCORE >= 100 && SCORE < 300){
el18a2k 10:e221bd1ce3ec 151 FISH_SIZE = 2; //fish 2
el18a2k 10:e221bd1ce3ec 152 barIncrement = 2;
el18a2k 11:7c1e2a9303d3 153
el18a2k 10:e221bd1ce3ec 154 }else if (SCORE >= 300 && SCORE <= 700){
el18a2k 10:e221bd1ce3ec 155 FISH_SIZE = 3; //fish 3
el18a2k 10:e221bd1ce3ec 156 barIncrement = 1;
el18a2k 11:7c1e2a9303d3 157
el18a2k 10:e221bd1ce3ec 158 }else if (SCORE >= 700){
el18a2k 10:e221bd1ce3ec 159 FISH_SIZE = 4; //largest fish
el18a2k 10:e221bd1ce3ec 160 barIncrement = 0;
el18a2k 10:e221bd1ce3ec 161 }
el18a2k 10:e221bd1ce3ec 162
el18a2k 10:e221bd1ce3ec 163 //increases the growth bar width each time a the score increases
el18a2k 10:e221bd1ce3ec 164 if(check_score != SCORE){
el18a2k 10:e221bd1ce3ec 165 if(FISH_SIZE == 1){
el18a2k 10:e221bd1ce3ec 166 rect_width = rect_width + 4;
el18a2k 11:7c1e2a9303d3 167
el18a2k 10:e221bd1ce3ec 168 }else if(FISH_SIZE == 2){
el18a2k 10:e221bd1ce3ec 169 rect_width = rect_width + 2;
el18a2k 11:7c1e2a9303d3 170
el18a2k 10:e221bd1ce3ec 171 }else if(FISH_SIZE == 3){
el18a2k 10:e221bd1ce3ec 172 rect_width = rect_width + 1;
el18a2k 11:7c1e2a9303d3 173
el18a2k 10:e221bd1ce3ec 174 }else if(FISH_SIZE == 4){
el18a2k 10:e221bd1ce3ec 175 rect_width = 40;
el18a2k 10:e221bd1ce3ec 176 }
el18a2k 10:e221bd1ce3ec 177
el18a2k 10:e221bd1ce3ec 178 check_score = SCORE;
el18a2k 10:e221bd1ce3ec 179 }
el18a2k 10:e221bd1ce3ec 180
el18a2k 10:e221bd1ce3ec 181 //check for change in bar increment if true reset the width of the growth bar
el18a2k 10:e221bd1ce3ec 182 if(check_width != barIncrement){
el18a2k 10:e221bd1ce3ec 183 if(FISH_SIZE == 4){ //if largest fish than growth bar is max
el18a2k 10:e221bd1ce3ec 184 rect_width = 40;
el18a2k 11:7c1e2a9303d3 185
el18a2k 10:e221bd1ce3ec 186 }else{
el18a2k 10:e221bd1ce3ec 187 rect_width = 0;
el18a2k 10:e221bd1ce3ec 188 }
el18a2k 10:e221bd1ce3ec 189 check_width = barIncrement; //set new check width to new bar increment
el18a2k 10:e221bd1ce3ec 190 }
el18a2k 10:e221bd1ce3ec 191
el18a2k 10:e221bd1ce3ec 192 return SCORE;
el18a2k 10:e221bd1ce3ec 193 }
el18a2k 10:e221bd1ce3ec 194
el18a2k 10:e221bd1ce3ec 195 //keeps track of lives
el18a2k 10:e221bd1ce3ec 196 int Game::Lives(){
el18a2k 10:e221bd1ce3ec 197 if (lives == 3){
el18a2k 10:e221bd1ce3ec 198 _shift.write(0x4F); //write 3
el18a2k 11:7c1e2a9303d3 199
el18a2k 10:e221bd1ce3ec 200 }else if (lives == 2){
el18a2k 10:e221bd1ce3ec 201 _shift.write(0x5B); //write 2
el18a2k 11:7c1e2a9303d3 202
el18a2k 10:e221bd1ce3ec 203 }else if (lives == 1){
el18a2k 10:e221bd1ce3ec 204 _shift.write(0x06); //write 1
el18a2k 11:7c1e2a9303d3 205
el18a2k 10:e221bd1ce3ec 206 }else if (lives == 0){
el18a2k 10:e221bd1ce3ec 207 _shift.write(0x3F); //write 0
el18a2k 10:e221bd1ce3ec 208 }
el18a2k 10:e221bd1ce3ec 209 return lives;
el18a2k 10:e221bd1ce3ec 210 }
el18a2k 10:e221bd1ce3ec 211
el18a2k 10:e221bd1ce3ec 212 //checks for collision with other fish
el18a2k 10:e221bd1ce3ec 213 int Game::Collision(){
el18a2k 10:e221bd1ce3ec 214 centre_xpos = E_CENTRE_X - CENTRE_X;
el18a2k 10:e221bd1ce3ec 215 centre_ypos = E_CENTRE_Y - CENTRE_Y;
el18a2k 10:e221bd1ce3ec 216
el18a2k 10:e221bd1ce3ec 217 if((centre_xpos == 2 && centre_ypos <= 1) && (centre_xpos == 2 && centre_ypos >= -1)){
el18a2k 10:e221bd1ce3ec 218 if(FISH_SIZE < E_FISH){
el18a2k 10:e221bd1ce3ec 219 //remove one life
el18a2k 11:7c1e2a9303d3 220 lives--;
el18a2k 11:7c1e2a9303d3 221 //flicker busout
el18a2k 11:7c1e2a9303d3 222 _RGB_led.write(6); //red
el18a2k 11:7c1e2a9303d3 223 wait_ms(100);
el18a2k 11:7c1e2a9303d3 224 _RGB_led.write(7); //off
el18a2k 11:7c1e2a9303d3 225
el18a2k 10:e221bd1ce3ec 226 }else if(FISH_SIZE >= E_FISH){
el18a2k 10:e221bd1ce3ec 227 //increase score by 10
el18a2k 10:e221bd1ce3ec 228 SCORE = SCORE + 10;
el18a2k 10:e221bd1ce3ec 229 //remove eaten fish
el18a2k 10:e221bd1ce3ec 230 X = -23;
el18a2k 11:7c1e2a9303d3 231 //flicker busout
el18a2k 11:7c1e2a9303d3 232 i = 0;
el18a2k 11:7c1e2a9303d3 233 while(i < 3){
el18a2k 11:7c1e2a9303d3 234 colour = rand() % 6 + 1;
el18a2k 11:7c1e2a9303d3 235 _RGB_led.write(colour);
el18a2k 11:7c1e2a9303d3 236 wait_ms(100);
el18a2k 11:7c1e2a9303d3 237 _RGB_led.write(7);
el18a2k 11:7c1e2a9303d3 238 i++;
el18a2k 11:7c1e2a9303d3 239 }
el18a2k 11:7c1e2a9303d3 240 _RGB_led.write(7);
el18a2k 11:7c1e2a9303d3 241 i = 0;
el18a2k 10:e221bd1ce3ec 242 }
el18a2k 10:e221bd1ce3ec 243 collision = 1;
el18a2k 10:e221bd1ce3ec 244 }else{
el18a2k 10:e221bd1ce3ec 245 collision = 0;
el18a2k 10:e221bd1ce3ec 246 }
el18a2k 10:e221bd1ce3ec 247 return collision;
el18a2k 10:e221bd1ce3ec 248 }
el18a2k 10:e221bd1ce3ec 249
el18a2k 10:e221bd1ce3ec 250 //generates enemy fish
el18a2k 10:e221bd1ce3ec 251 void Game::enemyFish(N5110 &lcd){
el18a2k 10:e221bd1ce3ec 252 if (E_FISH == 1){
el18a2k 10:e221bd1ce3ec 253 //draw first fish
el18a2k 10:e221bd1ce3ec 254 _graphics.E_Fish_1(lcd, X, Y);
el18a2k 10:e221bd1ce3ec 255 E_FISH_HEIGHT = 4;
el18a2k 10:e221bd1ce3ec 256 E_FISH_WIDTH = 9;
el18a2k 10:e221bd1ce3ec 257 E_CENTRE_X = X + (E_FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 258 E_CENTRE_Y = Y + (E_FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 259
el18a2k 10:e221bd1ce3ec 260 }else if(E_FISH == 2){
el18a2k 10:e221bd1ce3ec 261 //draw second fish
el18a2k 10:e221bd1ce3ec 262 _graphics.E_Fish_2(lcd, X, Y);
el18a2k 10:e221bd1ce3ec 263 E_FISH_HEIGHT = 6;
el18a2k 10:e221bd1ce3ec 264 E_FISH_WIDTH = 14;
el18a2k 10:e221bd1ce3ec 265 E_CENTRE_X = X + (E_FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 266 E_CENTRE_Y = Y + (E_FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 267
el18a2k 10:e221bd1ce3ec 268 }else if(E_FISH == 3){
el18a2k 10:e221bd1ce3ec 269 //draw third fish
el18a2k 10:e221bd1ce3ec 270 _graphics.E_Fish_3(lcd, X, Y);
el18a2k 10:e221bd1ce3ec 271 E_FISH_HEIGHT = 8;
el18a2k 10:e221bd1ce3ec 272 E_FISH_WIDTH = 17;
el18a2k 10:e221bd1ce3ec 273 E_CENTRE_X = X + (E_FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 274 E_CENTRE_Y = Y + (E_FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 275
el18a2k 10:e221bd1ce3ec 276 }else if(E_FISH == 4){
el18a2k 10:e221bd1ce3ec 277 //draw final fish
el18a2k 10:e221bd1ce3ec 278 _graphics.E_Fish_4(lcd, X, Y);
el18a2k 10:e221bd1ce3ec 279 E_FISH_HEIGHT = 12;
el18a2k 10:e221bd1ce3ec 280 E_FISH_WIDTH = 23;
el18a2k 10:e221bd1ce3ec 281 E_CENTRE_X = X + (E_FISH_WIDTH/2);
el18a2k 10:e221bd1ce3ec 282 E_CENTRE_Y = Y + (E_FISH_HEIGHT/2);
el18a2k 10:e221bd1ce3ec 283 }
el18a2k 10:e221bd1ce3ec 284 }