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