Simple fish eat program

Dependencies:   mbed mbed-rtos N5110 ShiftReg Tone

Committer:
el18a2k
Date:
Wed Apr 21 16:26:26 2021 +0000
Revision:
13:183bd19f3d7d
Parent:
12:20ac766b3175
Child:
14:f1552b691274
bugs seem to of disapeared

Who changed what in which revision?

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