Meteor defense project

Dependencies:   N5110 mbed

Committer:
jasper0712
Date:
Fri Apr 21 14:12:28 2017 +0000
Revision:
33:61c69c46a941
Parent:
32:580d74825810
Child:
34:6ac9541d4c31
i think the game crash caused by bomb is solved. now i can put the spawn struct back

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jasper0712 1:f5cda0674f24 1 #include "Spawn.h"
jasper0712 0:5fbc0fbc9d30 2
jasper0712 1:f5cda0674f24 3 Spawn::Spawn()
jasper0712 1:f5cda0674f24 4 {
jasper0712 1:f5cda0674f24 5
jasper0712 1:f5cda0674f24 6 }
jasper0712 1:f5cda0674f24 7
jasper0712 1:f5cda0674f24 8 Spawn::~Spawn()
jasper0712 1:f5cda0674f24 9 {
jasper0712 1:f5cda0674f24 10
jasper0712 1:f5cda0674f24 11 }
jasper0712 32:580d74825810 12 void Spawn::init(int w) {
jasper0712 32:580d74825810 13 healthA = 7 + w * 2;
jasper0712 32:580d74825810 14 healthC = 150 + w * 5;
jasper0712 32:580d74825810 15 healthDE = 20 + w ;
jasper0712 32:580d74825810 16 spawnRateA = 32 + w; //32
jasper0712 32:580d74825810 17 spawnRateB = 1 + w / 2; //1
jasper0712 33:61c69c46a941 18 spawnRateC = 1 + w ; //2
jasper0712 32:580d74825810 19 spawnRateDE = 1 + w/ 2; //1
jasper0712 32:580d74825810 20 //randomise the seed. To get different spawn pattern all the time.
jasper0712 32:580d74825810 21 int t = time(NULL);
jasper0712 32:580d74825810 22 //printf("t = %d \n",t);
jasper0712 32:580d74825810 23 srand(t);
jasper0712 18:d82059ce929b 24 }
jasper0712 20:32f115462bbc 25 void Spawn::randomizeSpawn(int Arr[][Rows], char cArr[][Rows]) {
jasper0712 24:d6187d39f09b 26 //there is 4 kind of different spawn - a, b, c and d/e.
jasper0712 23:6d197a452d7c 27 if (stopSpawnA == 1) { //spawn for this row
jasper0712 25:edd6a95607b1 28 spawnA(Arr, cArr);
jasper0712 23:6d197a452d7c 29 stopSpawnA= 0;
jasper0712 19:7ccbb19703f9 30 } else { //stop spawn for the next row
jasper0712 23:6d197a452d7c 31 stopSpawnA = 1;
jasper0712 24:d6187d39f09b 32 spawnC(Arr, cArr);
jasper0712 13:38cbce17d7d7 33 }
jasper0712 1:f5cda0674f24 34 for ( int x = 0; x < Cols; x++) {
jasper0712 20:32f115462bbc 35 spawnB(x, Arr, cArr);
jasper0712 24:d6187d39f09b 36 spawnDE(x, Arr, cArr);
jasper0712 1:f5cda0674f24 37 }
jasper0712 7:3be4369131c6 38 //printf("at x=2, y=0. value = %d \n",Arr1[2][0]);
jasper0712 1:f5cda0674f24 39 }
jasper0712 20:32f115462bbc 40 void Spawn::spawnA(int Arr[][Rows],char cArr[][Rows]) {
jasper0712 19:7ccbb19703f9 41 for ( int x = 0; x < Cols; x++) {
jasper0712 19:7ccbb19703f9 42 //32/200 chance of spawning for A.
jasper0712 19:7ccbb19703f9 43 //can depends on level
jasper0712 32:580d74825810 44 bool TrueFalseA = (rand() % 220) < spawnRateA;
jasper0712 19:7ccbb19703f9 45 if (TrueFalseA) {
jasper0712 19:7ccbb19703f9 46 //spawning from the top of the screen
jasper0712 20:32f115462bbc 47 Arr[x][0] = healthA;
jasper0712 20:32f115462bbc 48 cArr[x][0] = 'a';
jasper0712 19:7ccbb19703f9 49 //printf("spawn set at x = is %d \n",x);
jasper0712 19:7ccbb19703f9 50 }
jasper0712 19:7ccbb19703f9 51 }
jasper0712 19:7ccbb19703f9 52 }
jasper0712 20:32f115462bbc 53 void Spawn::spawnB(int x, int Arr[][Rows],char cArr[][Rows]) {
jasper0712 32:580d74825810 54 bool TrueFalseB = (rand() % 220) < spawnRateB;
jasper0712 19:7ccbb19703f9 55 if (TrueFalseB) {
jasper0712 20:32f115462bbc 56 Arr[x][0] = 100000;
jasper0712 20:32f115462bbc 57 cArr[x][0] = 'b';
jasper0712 19:7ccbb19703f9 58 }
jasper0712 19:7ccbb19703f9 59 }
jasper0712 23:6d197a452d7c 60 void Spawn::spawnC(int Arr[][Rows],char cArr[][Rows]) {
jasper0712 23:6d197a452d7c 61 for ( int x = 0; x < Cols; x++) {
jasper0712 32:580d74825810 62 bool TrueFalseC = (rand() % 220) < spawnRateC;
jasper0712 23:6d197a452d7c 63 if (TrueFalseC) {
jasper0712 23:6d197a452d7c 64 Arr[x][1] = healthC;
jasper0712 23:6d197a452d7c 65 cArr[x][1] = 'c';
jasper0712 23:6d197a452d7c 66 cArr[x-1][0] = 'l';
jasper0712 23:6d197a452d7c 67 cArr[x+1][0] = 'r';
jasper0712 23:6d197a452d7c 68 }
jasper0712 19:7ccbb19703f9 69 }
jasper0712 19:7ccbb19703f9 70 }
jasper0712 24:d6187d39f09b 71 void Spawn::spawnDE(int x, int Arr[][Rows], char cArr[][Rows]) {
jasper0712 32:580d74825810 72 bool TrueFalseD = (rand() % 220) < spawnRateDE;
jasper0712 23:6d197a452d7c 73 if (TrueFalseD) {
jasper0712 23:6d197a452d7c 74 bool LeftRightD = (rand() % 10) <5; //its a 50-50 chance
jasper0712 23:6d197a452d7c 75 if (LeftRightD) {
jasper0712 24:d6187d39f09b 76 Arr[x][0] = healthDE;
jasper0712 23:6d197a452d7c 77 cArr[x][0] = 'd'; //d denotes spawnD starts by moving left
jasper0712 23:6d197a452d7c 78 } else {
jasper0712 24:d6187d39f09b 79 Arr[x][0] = healthDE;
jasper0712 23:6d197a452d7c 80 cArr[x][0] = 'e'; //e denotes spawnD starts by moving right
jasper0712 23:6d197a452d7c 81 }
jasper0712 23:6d197a452d7c 82 }
jasper0712 22:2e75b50b26f0 83 }
jasper0712 20:32f115462bbc 84 void Spawn::moveSpawnABC(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) {
jasper0712 2:2c60c92bf65b 85 //printf("x=2,y =0. value = %d \n",array[2][0]);
jasper0712 1:f5cda0674f24 86 //scan through the whole array
jasper0712 13:38cbce17d7d7 87 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 88 for (int y = 0; y < Rows; y++) {
jasper0712 27:d4926f19c12a 89 if (cArr[x][y] == 'a' || cArr[x][y] == 'b' || cArr[x][y] == 'c' || cArr[x][y] == 'l' || cArr[x][y] == 'r' || cArr[x][y] == 'm') {
jasper0712 23:6d197a452d7c 90 Arr2[x][y+1] = Arr[x][y]; cArr2[x][y+1] = cArr[x][y]; //move everything down a row
jasper0712 2:2c60c92bf65b 91 } else {
jasper0712 23:6d197a452d7c 92 Arr2[x][y+1] = 0; cArr2[x][y+1] = '\0';
jasper0712 1:f5cda0674f24 93 }
jasper0712 1:f5cda0674f24 94 }
jasper0712 1:f5cda0674f24 95 }
jasper0712 6:a554424e4517 96 printf("at x=2, y=47. number is =%d \n",Arr2[2][47]);
jasper0712 1:f5cda0674f24 97 }
jasper0712 24:d6187d39f09b 98 void Spawn::moveSpawnDE(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //spawnD moves in a zig zag behavior.
jasper0712 23:6d197a452d7c 99 for (int x = 0; x < Cols; x++) { // moves other direction when hit 'L' or 'R' of spawnC and walls.
jasper0712 23:6d197a452d7c 100 for (int y = 0; y < Rows; y++) { //jumps over spawnA and spawnB
jasper0712 24:d6187d39f09b 101 if (cArr[x][y] == 'd') { //d = move left & down
jasper0712 24:d6187d39f09b 102 movementD(x, y, Arr, Arr2, cArr, cArr2);
jasper0712 24:d6187d39f09b 103 } else if ( cArr[x][y] == 'e') { //e = move right & down
jasper0712 24:d6187d39f09b 104 movementE(x, y, Arr, Arr2, cArr, cArr2);
jasper0712 23:6d197a452d7c 105 }
jasper0712 23:6d197a452d7c 106 }
jasper0712 23:6d197a452d7c 107 }
jasper0712 23:6d197a452d7c 108 }
jasper0712 27:d4926f19c12a 109 void Spawn::movementD(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //d = move left & down
jasper0712 27:d4926f19c12a 110 if (cArr[x-1][y+1] == 'a' || cArr[x-1][y] == 'b' ) { //if the path is blocked by spawnA or spawnB
jasper0712 24:d6187d39f09b 111 cArr2[x-2][y+2] = 'd'; Arr2[x-2][y+2] = Arr[x][y]; //jumps over it
jasper0712 24:d6187d39f09b 112 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 113 //printf("jump over at [%d][%d] \n",x-1,y+1);
jasper0712 27:d4926f19c12a 114 } else if (x == 0 || x == 83 || cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r' || cArr[x-1][y+1] == 'm') { //if it hits the wall or 'l' or 'r' or 'm'.
jasper0712 24:d6187d39f09b 115 if (cArr[x-1][y+1] == 'l' || cArr[x-1][y+1] == 'r') {
jasper0712 25:edd6a95607b1 116 //printf("char = %c \n",cArr[x-1][y+1]);
jasper0712 24:d6187d39f09b 117 }
jasper0712 24:d6187d39f09b 118 cArr2[x+1][y+1] = 'e'; Arr2[x+1][y+1] = Arr[x][y]; //start moving right & down
jasper0712 24:d6187d39f09b 119 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 120 //printf("spawn d at x = %d is shifted to e \n",x);
jasper0712 24:d6187d39f09b 121 } else { //if nothing is blocking the path
jasper0712 24:d6187d39f09b 122 cArr2[x-1][y+1] = cArr[x][y]; Arr2[x-1][y+1] = Arr[x][y]; //move as usual.
jasper0712 24:d6187d39f09b 123 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 124 //printf("work pls. \n");
jasper0712 24:d6187d39f09b 125 }
jasper0712 24:d6187d39f09b 126 }
jasper0712 27:d4926f19c12a 127 void Spawn::movementE(int x, int y, int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]) { //e = move right & down
jasper0712 27:d4926f19c12a 128 if (cArr[x+1][y+1] == 'a' || cArr[x+1][y] == 'b' ) { //if the path is blocked by spawnA or spawnB
jasper0712 24:d6187d39f09b 129 cArr2[x+2][y+2] = 'e'; Arr2[x+2][y+2] = Arr[x][y]; //jumps over it
jasper0712 24:d6187d39f09b 130 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 131 //printf("jump over at [%d][%d] \n",x+1,y+1);
jasper0712 27:d4926f19c12a 132 } else if (x == 0 || x == 83 || cArr[x+1][y+1] == 'l' || cArr[x+1][y+1] == 'r' || cArr[x+1][y+1] == 'm') { //if it hits the wall or 'l' or 'r' or 'm'.
jasper0712 24:d6187d39f09b 133 cArr2[x-1][y+1] = 'd'; Arr2[x-1][y+1] = Arr[x][y]; //start moving left & down
jasper0712 24:d6187d39f09b 134 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 135 //printf("spawn d at x = %d is shifted to e \n",x);
jasper0712 24:d6187d39f09b 136 } else { //if nothing is blocking the path
jasper0712 24:d6187d39f09b 137 cArr2[x+1][y+1] = cArr[x][y]; Arr2[x+1][y+1] = Arr[x][y]; //move as usual.
jasper0712 24:d6187d39f09b 138 cArr2[x][y] = '\0'; Arr2[x][y] = 0;
jasper0712 24:d6187d39f09b 139 //printf("work pls. \n");
jasper0712 24:d6187d39f09b 140 }
jasper0712 24:d6187d39f09b 141 }
jasper0712 20:32f115462bbc 142 void Spawn::moveSpawnB(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows]){
jasper0712 13:38cbce17d7d7 143 //printf("moving spawn B \n");
jasper0712 13:38cbce17d7d7 144 for (int x = 0; x < Cols; x++) {
jasper0712 13:38cbce17d7d7 145 for (int y = 0; y < Rows; y++) {
jasper0712 25:edd6a95607b1 146 //deleteChar(x, y, Arr2, cArr2);
jasper0712 20:32f115462bbc 147 if (cArr[x][y] == 'b') {
jasper0712 20:32f115462bbc 148 Arr2[x][y+1] = Arr[x][y];
jasper0712 20:32f115462bbc 149 cArr2[x][y+1] = 'b';
jasper0712 20:32f115462bbc 150 cArr2[x][y] = '\0';
jasper0712 20:32f115462bbc 151 Arr2[x][y] = 0;
jasper0712 13:38cbce17d7d7 152 }
jasper0712 13:38cbce17d7d7 153 }
jasper0712 13:38cbce17d7d7 154 }
jasper0712 13:38cbce17d7d7 155 }
jasper0712 20:32f115462bbc 156 void Spawn::updateSpawn(int Arr[][Rows], int Arr2[][Rows], char cArr[][Rows], char cArr2[][Rows], N5110 &lcd) {
jasper0712 24:d6187d39f09b 157 for ( int y = 0; y < Rows; y++) {
jasper0712 24:d6187d39f09b 158 for ( int x = 0; x < Cols; x++) {
jasper0712 21:e5585569a938 159 deleteChar(x, y, Arr2, cArr2); //cleaning up the array
jasper0712 27:d4926f19c12a 160 if (cArr2[x][y] == 'a' || cArr2[x][y] == 'd' || cArr2[x][y] == 'e') {
jasper0712 1:f5cda0674f24 161 lcd.setPixel(x,y);
jasper0712 23:6d197a452d7c 162 Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 23:6d197a452d7c 163 cArr[x][y] = cArr2[x][y];
jasper0712 20:32f115462bbc 164 } else if (cArr2[x][y] == 'b') {
jasper0712 27:d4926f19c12a 165 lcd.setPixel(x,y); lcd.setPixel(x,y-1);
jasper0712 23:6d197a452d7c 166 Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 20:32f115462bbc 167 cArr[x][y] = 'b';
jasper0712 27:d4926f19c12a 168 } else if (cArr2[x][y] == 'c') {// l and r (left and right) side of the spawn C for bigger hit box & to deflect spawnDE.
jasper0712 24:d6187d39f09b 169 if (cArr2[x-1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced
jasper0712 24:d6187d39f09b 170 cArr[x-1][y-1] = 'l'; cArr2[x-1][y-1] = 'l';
jasper0712 24:d6187d39f09b 171 }
jasper0712 24:d6187d39f09b 172 if (cArr2[x+1][y-1] == '\0'){ //this code to prevent b, d and e from getting replaced
jasper0712 24:d6187d39f09b 173 cArr[x+1][y-1] = 'r'; cArr2[x+1][y-1] = 'r';
jasper0712 24:d6187d39f09b 174 }
jasper0712 27:d4926f19c12a 175 cArr[x][y-1] = 'm'; //to deflect spawnDE.
jasper0712 24:d6187d39f09b 176 cArr[x][y] = cArr2[x][y]; Arr[x][y] = Arr2[x][y]; //to update the main array
jasper0712 24:d6187d39f09b 177 lcd.setPixel(x,y); lcd.setPixel(x,y-1);
jasper0712 24:d6187d39f09b 178 lcd.setPixel(x+1,y-1); lcd.setPixel(x-1,y-1);
jasper0712 24:d6187d39f09b 179 } else {
jasper0712 20:32f115462bbc 180 Arr[x][y] = 0;
jasper0712 20:32f115462bbc 181 cArr[x][y] = '\0';
jasper0712 1:f5cda0674f24 182 }
jasper0712 1:f5cda0674f24 183 }
jasper0712 1:f5cda0674f24 184 }
jasper0712 1:f5cda0674f24 185 }
jasper0712 21:e5585569a938 186 void Spawn::deleteChar(int x, int y, int Arr2[][Rows], char cArr2[][Rows]) { // to clean up the array after killing the spawn.
jasper0712 26:140515d80457 187 if (Arr2[x][y] <= 0) {
jasper0712 20:32f115462bbc 188 cArr2[x][y] = '\0';
jasper0712 24:d6187d39f09b 189 //printf("char at Arr2[%d][%d] cleared for good. \n",x,y);
jasper0712 26:140515d80457 190 }
jasper0712 13:38cbce17d7d7 191 }
jasper0712 31:1c0e47931e84 192
jasper0712 31:1c0e47931e84 193