big fish game
Dependencies: 4DGL-uLCD-SE FatFileSystem MPR121_Demo SDFileSystem mbed wave_player
main.cpp@0:eabfe3700dd6, 2016-03-17 (annotated)
- Committer:
- hcha30
- Date:
- Thu Mar 17 17:03:46 2016 +0000
- Revision:
- 0:eabfe3700dd6
big fish game
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hcha30 | 0:eabfe3700dd6 | 1 | #include <mbed.h> |
hcha30 | 0:eabfe3700dd6 | 2 | #include <mpr121.h> |
hcha30 | 0:eabfe3700dd6 | 3 | #include "uLCD_4DGL.h" |
hcha30 | 0:eabfe3700dd6 | 4 | #include "SDFileSystem.h" |
hcha30 | 0:eabfe3700dd6 | 5 | #include "SongPlayer.h" |
hcha30 | 0:eabfe3700dd6 | 6 | #include "wave_player.h" |
hcha30 | 0:eabfe3700dd6 | 7 | |
hcha30 | 0:eabfe3700dd6 | 8 | uLCD_4DGL uLCD(p9,p10,p11); |
hcha30 | 0:eabfe3700dd6 | 9 | InterruptIn interrupt(p26); |
hcha30 | 0:eabfe3700dd6 | 10 | I2C i2c(p28, p27); |
hcha30 | 0:eabfe3700dd6 | 11 | Mpr121 mpr121(&i2c, Mpr121::ADD_VSS); |
hcha30 | 0:eabfe3700dd6 | 12 | AnalogOut DACout(p18); // speaker |
hcha30 | 0:eabfe3700dd6 | 13 | wave_player waver(&DACout); // wav player |
hcha30 | 0:eabfe3700dd6 | 14 | |
hcha30 | 0:eabfe3700dd6 | 15 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
hcha30 | 0:eabfe3700dd6 | 16 | |
hcha30 | 0:eabfe3700dd6 | 17 | void playSound(char * wav); |
hcha30 | 0:eabfe3700dd6 | 18 | |
hcha30 | 0:eabfe3700dd6 | 19 | int keypad(void); |
hcha30 | 0:eabfe3700dd6 | 20 | int * drawfishi(int, int, int,int, int, int, int, int); |
hcha30 | 0:eabfe3700dd6 | 21 | |
hcha30 | 0:eabfe3700dd6 | 22 | class FISHI |
hcha30 | 0:eabfe3700dd6 | 23 | { |
hcha30 | 0:eabfe3700dd6 | 24 | public: |
hcha30 | 0:eabfe3700dd6 | 25 | int x; |
hcha30 | 0:eabfe3700dd6 | 26 | int y; |
hcha30 | 0:eabfe3700dd6 | 27 | int ox; //oldx |
hcha30 | 0:eabfe3700dd6 | 28 | int oy; //oldy |
hcha30 | 0:eabfe3700dd6 | 29 | int r; //radius of fishi head |
hcha30 | 0:eabfe3700dd6 | 30 | int c; //fishi color |
hcha30 | 0:eabfe3700dd6 | 31 | }; |
hcha30 | 0:eabfe3700dd6 | 32 | int i, score, big; |
hcha30 | 0:eabfe3700dd6 | 33 | FISHI player; |
hcha30 | 0:eabfe3700dd6 | 34 | FISHI scary1; |
hcha30 | 0:eabfe3700dd6 | 35 | FISHI scary2; |
hcha30 | 0:eabfe3700dd6 | 36 | FISHI scary3; |
hcha30 | 0:eabfe3700dd6 | 37 | int move = 4; //player fishi move by 4 |
hcha30 | 0:eabfe3700dd6 | 38 | int bgC = 0xC4E6FF; //background color (water) |
hcha30 | 0:eabfe3700dd6 | 39 | |
hcha30 | 0:eabfe3700dd6 | 40 | void playSound(char * wav) |
hcha30 | 0:eabfe3700dd6 | 41 | { |
hcha30 | 0:eabfe3700dd6 | 42 | while(true) { |
hcha30 | 0:eabfe3700dd6 | 43 | FILE *wave_file; |
hcha30 | 0:eabfe3700dd6 | 44 | wave_file=fopen(wav,"r"); |
hcha30 | 0:eabfe3700dd6 | 45 | if(wave_file == NULL) { |
hcha30 | 0:eabfe3700dd6 | 46 | uLCD.locate(9,0); |
hcha30 | 0:eabfe3700dd6 | 47 | uLCD.printf("ERROR_SD"); |
hcha30 | 0:eabfe3700dd6 | 48 | return; |
hcha30 | 0:eabfe3700dd6 | 49 | } |
hcha30 | 0:eabfe3700dd6 | 50 | waver.play(wave_file); |
hcha30 | 0:eabfe3700dd6 | 51 | fclose(wave_file); |
hcha30 | 0:eabfe3700dd6 | 52 | } |
hcha30 | 0:eabfe3700dd6 | 53 | } |
hcha30 | 0:eabfe3700dd6 | 54 | |
hcha30 | 0:eabfe3700dd6 | 55 | int main() |
hcha30 | 0:eabfe3700dd6 | 56 | { |
hcha30 | 0:eabfe3700dd6 | 57 | //player fishi default |
hcha30 | 0:eabfe3700dd6 | 58 | player.x = 64; |
hcha30 | 0:eabfe3700dd6 | 59 | player.y = 64; |
hcha30 | 0:eabfe3700dd6 | 60 | player.r = 7; |
hcha30 | 0:eabfe3700dd6 | 61 | player.c = 0xffcd8e; //orange |
hcha30 | 0:eabfe3700dd6 | 62 | player.ox = player.x; |
hcha30 | 0:eabfe3700dd6 | 63 | player.oy = player.y; |
hcha30 | 0:eabfe3700dd6 | 64 | |
hcha30 | 0:eabfe3700dd6 | 65 | //scary fishi default |
hcha30 | 0:eabfe3700dd6 | 66 | scary1.c = 0x000000; |
hcha30 | 0:eabfe3700dd6 | 67 | scary1.r = 7; |
hcha30 | 0:eabfe3700dd6 | 68 | scary1.x; //0 or 128 |
hcha30 | 0:eabfe3700dd6 | 69 | scary2.c = 0x000000; |
hcha30 | 0:eabfe3700dd6 | 70 | scary2.r = 7; |
hcha30 | 0:eabfe3700dd6 | 71 | scary2.x; //0 or 128 |
hcha30 | 0:eabfe3700dd6 | 72 | scary3.c = 0x000000; |
hcha30 | 0:eabfe3700dd6 | 73 | scary3.r = 7; |
hcha30 | 0:eabfe3700dd6 | 74 | scary3.x; //0 or 128 |
hcha30 | 0:eabfe3700dd6 | 75 | |
hcha30 | 0:eabfe3700dd6 | 76 | int eatC = 0x8CDF59; //eatColor = green. you eat this to score |
hcha30 | 0:eabfe3700dd6 | 77 | int e1, e2, e3, e4, eat1, eat2, eat3, eat4; |
hcha30 | 0:eabfe3700dd6 | 78 | int side1, side2, side3; //starting side of scary fishi. left is 0, right is 1. |
hcha30 | 0:eabfe3700dd6 | 79 | // scary fishi moving speed |
hcha30 | 0:eabfe3700dd6 | 80 | int s = 3; |
hcha30 | 0:eabfe3700dd6 | 81 | int s2 = 6; |
hcha30 | 0:eabfe3700dd6 | 82 | int s3 = 9; |
hcha30 | 0:eabfe3700dd6 | 83 | // scary fishi y position randome generate |
hcha30 | 0:eabfe3700dd6 | 84 | int genrand1 = 1; |
hcha30 | 0:eabfe3700dd6 | 85 | int genrand2 = 1; |
hcha30 | 0:eabfe3700dd6 | 86 | int genrand3 = 1; |
hcha30 | 0:eabfe3700dd6 | 87 | |
hcha30 | 0:eabfe3700dd6 | 88 | int y1 = 0; |
hcha30 | 0:eabfe3700dd6 | 89 | int y2 = 0; |
hcha30 | 0:eabfe3700dd6 | 90 | int y3 = 0; |
hcha30 | 0:eabfe3700dd6 | 91 | |
hcha30 | 0:eabfe3700dd6 | 92 | int dir; //touchpad input |
hcha30 | 0:eabfe3700dd6 | 93 | uLCD.cls(); |
hcha30 | 0:eabfe3700dd6 | 94 | int game = 0; //game stages. 0 = welcom. 1 = play. 2 = game over |
hcha30 | 0:eabfe3700dd6 | 95 | uLCD.baudrate(500000); |
hcha30 | 0:eabfe3700dd6 | 96 | |
hcha30 | 0:eabfe3700dd6 | 97 | //welcome stage |
hcha30 | 0:eabfe3700dd6 | 98 | uLCD.filled_rectangle(0,0,128,128,DGREY); |
hcha30 | 0:eabfe3700dd6 | 99 | while (game == 0) { |
hcha30 | 0:eabfe3700dd6 | 100 | uLCD.locate(1,5); |
hcha30 | 0:eabfe3700dd6 | 101 | uLCD.textbackground_color(DGREY); |
hcha30 | 0:eabfe3700dd6 | 102 | //uLCD.text_string(" WELCOME TO BIG FISH GAME ", 1, 4, FONT_7X8, 0xFFFFFF); |
hcha30 | 0:eabfe3700dd6 | 103 | // |
hcha30 | 0:eabfe3700dd6 | 104 | uLCD.text_string("Press any key \n to start\n\n Use key 1,4,5,6\n\nGood Luck!", 1, 1, FONT_7X8, 0xFFFFFF); |
hcha30 | 0:eabfe3700dd6 | 105 | |
hcha30 | 0:eabfe3700dd6 | 106 | //uLCD.printf("Press any key \n to start\n\n Use key 1,4,5,6"); |
hcha30 | 0:eabfe3700dd6 | 107 | /* |
hcha30 | 0:eabfe3700dd6 | 108 | uLCD.locate(6,10); uLCD.printf("\n 9"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 109 | uLCD.locate(6,10); uLCD.printf("\n 8"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 110 | uLCD.locate(6,10); uLCD.printf("\n 7"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 111 | uLCD.locate(6,10); uLCD.printf("\n 6"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 112 | uLCD.locate(6,10); uLCD.printf("\n 5"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 113 | uLCD.locate(6,10); uLCD.printf("\n 4"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 114 | uLCD.locate(6,10); uLCD.printf("\n 3"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 115 | uLCD.locate(6,10); uLCD.printf("\n 2"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 116 | uLCD.locate(6,10); uLCD.printf("\n 1"); wait(1); |
hcha30 | 0:eabfe3700dd6 | 117 | */ |
hcha30 | 0:eabfe3700dd6 | 118 | |
hcha30 | 0:eabfe3700dd6 | 119 | if (keypad() != 0) { |
hcha30 | 0:eabfe3700dd6 | 120 | game = 1; |
hcha30 | 0:eabfe3700dd6 | 121 | uLCD.filled_rectangle(0,0,128,128,bgC); |
hcha30 | 0:eabfe3700dd6 | 122 | } |
hcha30 | 0:eabfe3700dd6 | 123 | } |
hcha30 | 0:eabfe3700dd6 | 124 | |
hcha30 | 0:eabfe3700dd6 | 125 | //game stage |
hcha30 | 0:eabfe3700dd6 | 126 | while (game == 1) { |
hcha30 | 0:eabfe3700dd6 | 127 | |
hcha30 | 0:eabfe3700dd6 | 128 | if (player.x-player.r-23<=e2+4-2 && player.x+player.r>=e2-4-4 && player.y-player.r<=eat2+4 && player.y+player.r>=eat2-4) { |
hcha30 | 0:eabfe3700dd6 | 129 | uLCD.filled_rectangle(e2-4-4,eat2-4,e2-2+4,eat2+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 130 | e2 = 0; |
hcha30 | 0:eabfe3700dd6 | 131 | |
hcha30 | 0:eabfe3700dd6 | 132 | score++; |
hcha30 | 0:eabfe3700dd6 | 133 | // playSound("/sd/coin.wav"); |
hcha30 | 0:eabfe3700dd6 | 134 | } |
hcha30 | 0:eabfe3700dd6 | 135 | if (player.x-player.r-23<=e3+4-6 && player.x+player.r>=e3-4-6 && player.y-player.r<=eat3+4 && player.y+player.r>=eat3-4) { |
hcha30 | 0:eabfe3700dd6 | 136 | uLCD.filled_rectangle(e3-6-4,eat3-4,e3-6+4,eat3+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 137 | e3 = 0; |
hcha30 | 0:eabfe3700dd6 | 138 | |
hcha30 | 0:eabfe3700dd6 | 139 | score++; |
hcha30 | 0:eabfe3700dd6 | 140 | // playSound("/sd/coin.wav"); |
hcha30 | 0:eabfe3700dd6 | 141 | } |
hcha30 | 0:eabfe3700dd6 | 142 | if (player.x-player.r-23<=e4+4-8 && player.x+player.r>=e4-4-8 && player.y-player.r<=eat4+4 && player.y+player.r>=eat4-4) { |
hcha30 | 0:eabfe3700dd6 | 143 | uLCD.filled_rectangle(e4-8-4,eat4-4,e4-8+4,eat4+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 144 | e4 = 0; |
hcha30 | 0:eabfe3700dd6 | 145 | |
hcha30 | 0:eabfe3700dd6 | 146 | score++; |
hcha30 | 0:eabfe3700dd6 | 147 | //playSound("/sd/coin.wav"); |
hcha30 | 0:eabfe3700dd6 | 148 | } |
hcha30 | 0:eabfe3700dd6 | 149 | |
hcha30 | 0:eabfe3700dd6 | 150 | //eat this and score!!! |
hcha30 | 0:eabfe3700dd6 | 151 | if (e1>135 || e1 == 0) { |
hcha30 | 0:eabfe3700dd6 | 152 | eat1 = rand() % (128-4) + 4; |
hcha30 | 0:eabfe3700dd6 | 153 | e1 = 0; |
hcha30 | 0:eabfe3700dd6 | 154 | } |
hcha30 | 0:eabfe3700dd6 | 155 | uLCD.filled_rectangle(e1-2-4,eat1-4,e1-2+4,eat1+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 156 | uLCD.filled_circle(e1, eat1, 4, eatC); |
hcha30 | 0:eabfe3700dd6 | 157 | |
hcha30 | 0:eabfe3700dd6 | 158 | |
hcha30 | 0:eabfe3700dd6 | 159 | if (e2>135 || e2 == 0) { |
hcha30 | 0:eabfe3700dd6 | 160 | eat2 = rand() % (128-4) + 4; |
hcha30 | 0:eabfe3700dd6 | 161 | e2 = 0; |
hcha30 | 0:eabfe3700dd6 | 162 | } |
hcha30 | 0:eabfe3700dd6 | 163 | uLCD.filled_rectangle(e2-4-4,eat2-4,e2-2+4,eat2+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 164 | uLCD.filled_circle(e2, eat2, 4, eatC); |
hcha30 | 0:eabfe3700dd6 | 165 | |
hcha30 | 0:eabfe3700dd6 | 166 | if (e3>135 || e3 == 0) { |
hcha30 | 0:eabfe3700dd6 | 167 | eat3 = rand() % (128-4) + 4; |
hcha30 | 0:eabfe3700dd6 | 168 | e3 = 0; |
hcha30 | 0:eabfe3700dd6 | 169 | } |
hcha30 | 0:eabfe3700dd6 | 170 | uLCD.filled_rectangle(e3-6-4,eat3-4,e3-6+4,eat3+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 171 | uLCD.filled_circle(e3, eat3, 4, eatC); |
hcha30 | 0:eabfe3700dd6 | 172 | |
hcha30 | 0:eabfe3700dd6 | 173 | if (e1>135 || e1 == 0) { |
hcha30 | 0:eabfe3700dd6 | 174 | eat4 = rand() % (128-4) + 4; |
hcha30 | 0:eabfe3700dd6 | 175 | e4 = 0; |
hcha30 | 0:eabfe3700dd6 | 176 | } |
hcha30 | 0:eabfe3700dd6 | 177 | uLCD.filled_rectangle(e4-8-4,eat4-4,e4-8+4,eat4+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 178 | uLCD.filled_circle(e4, eat4, 4, eatC); |
hcha30 | 0:eabfe3700dd6 | 179 | //Check Scary Fish vs My Fish Collision // |
hcha30 | 0:eabfe3700dd6 | 180 | int y_s1_p, y_s2_p, y_s3_p; |
hcha30 | 0:eabfe3700dd6 | 181 | int x_s1_p, x_s2_p, x_s3_p; |
hcha30 | 0:eabfe3700dd6 | 182 | int y_p_s1, y_p_s2, y_p_s3; |
hcha30 | 0:eabfe3700dd6 | 183 | int x_p_s1, x_p_s2, x_p_s3; |
hcha30 | 0:eabfe3700dd6 | 184 | |
hcha30 | 0:eabfe3700dd6 | 185 | x_s1_p= abs(scary1.x - player.x); |
hcha30 | 0:eabfe3700dd6 | 186 | x_s2_p= abs(scary2.x - player.x); |
hcha30 | 0:eabfe3700dd6 | 187 | x_s3_p= abs(scary3.x - player.x); |
hcha30 | 0:eabfe3700dd6 | 188 | int x_sp_arr[] = {x_s1_p, x_s2_p, x_s3_p}; |
hcha30 | 0:eabfe3700dd6 | 189 | |
hcha30 | 0:eabfe3700dd6 | 190 | y_s1_p= abs(scary1.y - player.y); |
hcha30 | 0:eabfe3700dd6 | 191 | y_s2_p= abs(scary2.y - player.y); |
hcha30 | 0:eabfe3700dd6 | 192 | y_s3_p= abs(scary3.y - player.y); |
hcha30 | 0:eabfe3700dd6 | 193 | int y_sp_arr[] = {y_s1_p, y_s2_p, y_s3_p}; |
hcha30 | 0:eabfe3700dd6 | 194 | |
hcha30 | 0:eabfe3700dd6 | 195 | x_p_s1= abs( player.x - scary1.x ); |
hcha30 | 0:eabfe3700dd6 | 196 | x_p_s2= abs( player.x - scary2.x ); |
hcha30 | 0:eabfe3700dd6 | 197 | x_p_s3= abs( player.x - scary3.x ); |
hcha30 | 0:eabfe3700dd6 | 198 | int x_ps_arr[] = {x_p_s1, x_p_s2, x_p_s3}; |
hcha30 | 0:eabfe3700dd6 | 199 | |
hcha30 | 0:eabfe3700dd6 | 200 | y_p_s1= abs( player.y - scary1.y ); |
hcha30 | 0:eabfe3700dd6 | 201 | y_p_s2= abs( player.y - scary2.y ); |
hcha30 | 0:eabfe3700dd6 | 202 | y_p_s3= abs( player.y - scary3.y ); |
hcha30 | 0:eabfe3700dd6 | 203 | int y_ps_arr[] = {y_p_s1, y_p_s2, y_p_s3}; |
hcha30 | 0:eabfe3700dd6 | 204 | |
hcha30 | 0:eabfe3700dd6 | 205 | |
hcha30 | 0:eabfe3700dd6 | 206 | ///if (side1==0|| side2==0 || side3==0){ |
hcha30 | 0:eabfe3700dd6 | 207 | if ((x_sp_arr[0]<32 && y_sp_arr[0] <15) || (x_sp_arr[1]<32 && y_sp_arr[1] <15 )|| (x_sp_arr[2]<32 && y_sp_arr[2] <15)|| (x_sp_arr[0]<32 && y_ps_arr[0] <15)|| (x_sp_arr[1]<32 && y_ps_arr[1] <15)|| (x_sp_arr[2]<32 && y_ps_arr[2] <15) ) { |
hcha30 | 0:eabfe3700dd6 | 208 | game=3; |
hcha30 | 0:eabfe3700dd6 | 209 | } |
hcha30 | 0:eabfe3700dd6 | 210 | if ((x_ps_arr[0]<32 && y_sp_arr[0]<15) ||( x_ps_arr[1]<32 && y_sp_arr[1] <15 )|| (x_ps_arr[2]<32 && y_sp_arr[2] <15)|| (x_ps_arr[0]<32 && y_ps_arr[0] <15)|| (x_ps_arr[1]<32 && y_ps_arr[1] <15)|| (x_ps_arr[2]<32 && y_ps_arr[2]<15) ) { |
hcha30 | 0:eabfe3700dd6 | 211 | game=3; |
hcha30 | 0:eabfe3700dd6 | 212 | // } |
hcha30 | 0:eabfe3700dd6 | 213 | |
hcha30 | 0:eabfe3700dd6 | 214 | |
hcha30 | 0:eabfe3700dd6 | 215 | }//Comes from Left |
hcha30 | 0:eabfe3700dd6 | 216 | |
hcha30 | 0:eabfe3700dd6 | 217 | if (side1==1 || side2==1 || side3==1){ |
hcha30 | 0:eabfe3700dd6 | 218 | if ( (x_sp_arr[0]<15 && y_sp_arr[0]<15) ||( x_sp_arr[1]<15 && y_sp_arr[1]<15) || (x_sp_arr[2]<15 && y_sp_arr[2]<15) || (x_sp_arr[0]<32 && y_ps_arr[0]<15) || (x_sp_arr[1]<32 && y_ps_arr[1]<10) ||( x_sp_arr[2]<32 && y_ps_arr[2] <15) ) { |
hcha30 | 0:eabfe3700dd6 | 219 | game=3; |
hcha30 | 0:eabfe3700dd6 | 220 | } |
hcha30 | 0:eabfe3700dd6 | 221 | if ( (x_ps_arr[0]<15 && y_sp_arr[0] <15) || ( x_ps_arr[1]<15 && y_sp_arr[1] <15) || (x_ps_arr[2]<15 && y_sp_arr[2]<15) || (x_ps_arr[0]<32 && y_ps_arr[0]<10 ) || ( x_ps_arr[1]<32 && y_ps_arr[1]<10) ||( x_ps_arr[2]<32 && y_ps_arr[2]<15) ) { |
hcha30 | 0:eabfe3700dd6 | 222 | game=3; |
hcha30 | 0:eabfe3700dd6 | 223 | } |
hcha30 | 0:eabfe3700dd6 | 224 | |
hcha30 | 0:eabfe3700dd6 | 225 | |
hcha30 | 0:eabfe3700dd6 | 226 | } |
hcha30 | 0:eabfe3700dd6 | 227 | |
hcha30 | 0:eabfe3700dd6 | 228 | //USER FISHI |
hcha30 | 0:eabfe3700dd6 | 229 | dir = keyint(); |
hcha30 | 0:eabfe3700dd6 | 230 | int * newfishi; |
hcha30 | 0:eabfe3700dd6 | 231 | newfishi = drawfishi(player.x,player.y,player.ox,player.oy,player.r,player.c,dir,2); |
hcha30 | 0:eabfe3700dd6 | 232 | player.x = newfishi[0]; |
hcha30 | 0:eabfe3700dd6 | 233 | player.y = newfishi[1]; |
hcha30 | 0:eabfe3700dd6 | 234 | player.ox = newfishi[2]; |
hcha30 | 0:eabfe3700dd6 | 235 | player.oy = newfishi[3]; |
hcha30 | 0:eabfe3700dd6 | 236 | |
hcha30 | 0:eabfe3700dd6 | 237 | //scary fishi |
hcha30 | 0:eabfe3700dd6 | 238 | if (genrand1) { |
hcha30 | 0:eabfe3700dd6 | 239 | y1 = rand() % (128-scary1.r) + scary1.r; |
hcha30 | 0:eabfe3700dd6 | 240 | genrand1 = 0; |
hcha30 | 0:eabfe3700dd6 | 241 | scary1.x = y1%2==0 ? 0 : 128; |
hcha30 | 0:eabfe3700dd6 | 242 | side1 = scary1.x==0 ? 0 : 1; |
hcha30 | 0:eabfe3700dd6 | 243 | } |
hcha30 | 0:eabfe3700dd6 | 244 | if (genrand2) { |
hcha30 | 0:eabfe3700dd6 | 245 | y2 = rand() % (128-scary2.r) + scary2.r; |
hcha30 | 0:eabfe3700dd6 | 246 | genrand2 = 0; |
hcha30 | 0:eabfe3700dd6 | 247 | scary2.x = y2%2==0 ? 0 : 128; |
hcha30 | 0:eabfe3700dd6 | 248 | side2 = scary2.x==0 ? 0 : 1; |
hcha30 | 0:eabfe3700dd6 | 249 | } |
hcha30 | 0:eabfe3700dd6 | 250 | if (genrand3) { |
hcha30 | 0:eabfe3700dd6 | 251 | y3 = rand() % (128-scary3.r) + scary3.r; |
hcha30 | 0:eabfe3700dd6 | 252 | genrand3 = 0; |
hcha30 | 0:eabfe3700dd6 | 253 | scary3.x = y3%2==0 ? 0 : 128; |
hcha30 | 0:eabfe3700dd6 | 254 | side3 = scary3.x==0 ? 0 : 1; |
hcha30 | 0:eabfe3700dd6 | 255 | } |
hcha30 | 0:eabfe3700dd6 | 256 | //scary fishi move in x by s |
hcha30 | 0:eabfe3700dd6 | 257 | scary1.x = side1==0 ? scary1.x+s : scary1.x-s; |
hcha30 | 0:eabfe3700dd6 | 258 | scary2.x = side2==0 ? scary2.x+s2 : scary2.x-s2; |
hcha30 | 0:eabfe3700dd6 | 259 | scary3.x = side3==0 ? scary3.x+s3 : scary3.x-s3; |
hcha30 | 0:eabfe3700dd6 | 260 | scary1.y = y1; |
hcha30 | 0:eabfe3700dd6 | 261 | scary2.y = y2; |
hcha30 | 0:eabfe3700dd6 | 262 | scary3.y = y3; |
hcha30 | 0:eabfe3700dd6 | 263 | |
hcha30 | 0:eabfe3700dd6 | 264 | newfishi = drawfishi(scary1.x,scary1.y,scary1.ox,scary1.oy,scary1.r,scary1.c,-1,side1); |
hcha30 | 0:eabfe3700dd6 | 265 | scary1.x = newfishi[0]; |
hcha30 | 0:eabfe3700dd6 | 266 | scary1.y = newfishi[1]; |
hcha30 | 0:eabfe3700dd6 | 267 | scary1.ox = newfishi[2]; |
hcha30 | 0:eabfe3700dd6 | 268 | scary1.oy = newfishi[3]; |
hcha30 | 0:eabfe3700dd6 | 269 | |
hcha30 | 0:eabfe3700dd6 | 270 | newfishi = drawfishi(scary2.x,scary2.y,scary2.ox,scary2.oy,scary2.r,scary2.c,-1,side2); |
hcha30 | 0:eabfe3700dd6 | 271 | scary2.x = newfishi[0]; |
hcha30 | 0:eabfe3700dd6 | 272 | scary2.y = newfishi[1]; |
hcha30 | 0:eabfe3700dd6 | 273 | scary2.ox = newfishi[2]; |
hcha30 | 0:eabfe3700dd6 | 274 | scary2.oy = newfishi[3]; |
hcha30 | 0:eabfe3700dd6 | 275 | |
hcha30 | 0:eabfe3700dd6 | 276 | newfishi = drawfishi(scary3.x,scary3.y,scary3.ox,scary3.oy,scary3.r,scary3.c,-1,side3); |
hcha30 | 0:eabfe3700dd6 | 277 | scary3.x = newfishi[0]; |
hcha30 | 0:eabfe3700dd6 | 278 | scary3.y = newfishi[1]; |
hcha30 | 0:eabfe3700dd6 | 279 | scary3.ox = newfishi[2]; |
hcha30 | 0:eabfe3700dd6 | 280 | scary3.oy = newfishi[3]; |
hcha30 | 0:eabfe3700dd6 | 281 | |
hcha30 | 0:eabfe3700dd6 | 282 | if(scary1.x<-23 || scary1.x>128+23) genrand1 = 1; |
hcha30 | 0:eabfe3700dd6 | 283 | if(scary2.x<-23 || scary2.x>128+23) genrand2 = 1; |
hcha30 | 0:eabfe3700dd6 | 284 | if(scary3.x<-23 || scary3.x>128+23) genrand3 = 1; |
hcha30 | 0:eabfe3700dd6 | 285 | |
hcha30 | 0:eabfe3700dd6 | 286 | if (player.x-player.r-22<=e1 && player.x+player.r>=e1-4 && player.y-player.r<=eat1+4 && player.y+player.r>=eat1-4) { |
hcha30 | 0:eabfe3700dd6 | 287 | uLCD.filled_rectangle(e1-4,eat1-4,e1+4,eat1+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 288 | e1 = 0; |
hcha30 | 0:eabfe3700dd6 | 289 | score++; |
hcha30 | 0:eabfe3700dd6 | 290 | } else e1=e1+2; |
hcha30 | 0:eabfe3700dd6 | 291 | if (player.x-player.r-23<=e2+4-2 && player.x+player.r>=e2-4-4 && player.y-player.r<=eat2+4 && player.y+player.r>=eat2-4) { |
hcha30 | 0:eabfe3700dd6 | 292 | uLCD.filled_rectangle(e2-4-4,eat2-4,e2-2+4,eat2+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 293 | e2 = 0; |
hcha30 | 0:eabfe3700dd6 | 294 | score++; |
hcha30 | 0:eabfe3700dd6 | 295 | } else e2=e2+4; |
hcha30 | 0:eabfe3700dd6 | 296 | if (player.x-player.r-23<=e3+4-6 && player.x+player.r>=e3-4-6 && player.y-player.r<=eat3+4 && player.y+player.r>=eat3-4) { |
hcha30 | 0:eabfe3700dd6 | 297 | uLCD.filled_rectangle(e3-6-4,eat3-4,e3-6+4,eat3+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 298 | e3 = 0; |
hcha30 | 0:eabfe3700dd6 | 299 | score++; |
hcha30 | 0:eabfe3700dd6 | 300 | } else e3=e3+6; |
hcha30 | 0:eabfe3700dd6 | 301 | if (player.x-player.r-23<=e4+4-8 && player.x+player.r>=e4-4-8 && player.y-player.r<=eat4+4 && player.y+player.r>=eat4-4) { |
hcha30 | 0:eabfe3700dd6 | 302 | uLCD.filled_rectangle(e4-8-4,eat4-4,e4-8+4,eat4+4,bgC); |
hcha30 | 0:eabfe3700dd6 | 303 | e4 = 0; |
hcha30 | 0:eabfe3700dd6 | 304 | score++; |
hcha30 | 0:eabfe3700dd6 | 305 | } else e4=e4+8; |
hcha30 | 0:eabfe3700dd6 | 306 | |
hcha30 | 0:eabfe3700dd6 | 307 | //display score |
hcha30 | 0:eabfe3700dd6 | 308 | uLCD.locate(0,0); |
hcha30 | 0:eabfe3700dd6 | 309 | uLCD.textbackground_color(0x000000); |
hcha30 | 0:eabfe3700dd6 | 310 | uLCD.printf("%d", score ); |
hcha30 | 0:eabfe3700dd6 | 311 | |
hcha30 | 0:eabfe3700dd6 | 312 | wait (0.05); |
hcha30 | 0:eabfe3700dd6 | 313 | // playSound("/sd/coin.wav"); |
hcha30 | 0:eabfe3700dd6 | 314 | |
hcha30 | 0:eabfe3700dd6 | 315 | //game over when you score 10 |
hcha30 | 0:eabfe3700dd6 | 316 | if (score >= 5) { |
hcha30 | 0:eabfe3700dd6 | 317 | |
hcha30 | 0:eabfe3700dd6 | 318 | game = 2; |
hcha30 | 0:eabfe3700dd6 | 319 | uLCD.filled_rectangle(0,0,128,128,0xC5EFAC); |
hcha30 | 0:eabfe3700dd6 | 320 | } |
hcha30 | 0:eabfe3700dd6 | 321 | } |
hcha30 | 0:eabfe3700dd6 | 322 | |
hcha30 | 0:eabfe3700dd6 | 323 | // game over |
hcha30 | 0:eabfe3700dd6 | 324 | while (game == 2) { |
hcha30 | 0:eabfe3700dd6 | 325 | |
hcha30 | 0:eabfe3700dd6 | 326 | if (score >= 5) { |
hcha30 | 0:eabfe3700dd6 | 327 | uLCD.textbackground_color(0xC5EFAC); |
hcha30 | 0:eabfe3700dd6 | 328 | uLCD.text_string("YOU WIN", 1, 4, FONT_7X8, BLACK); |
hcha30 | 0:eabfe3700dd6 | 329 | } |
hcha30 | 0:eabfe3700dd6 | 330 | playSound("/sd/clap.wav"); |
hcha30 | 0:eabfe3700dd6 | 331 | |
hcha30 | 0:eabfe3700dd6 | 332 | uLCD.filled_rectangle(0,0,128,128,0xFC3D39); |
hcha30 | 0:eabfe3700dd6 | 333 | uLCD.textbackground_color(0xFC3D39); |
hcha30 | 0:eabfe3700dd6 | 334 | uLCD.text_string("YOU WON", 1, 4, FONT_7X8, BLACK); |
hcha30 | 0:eabfe3700dd6 | 335 | wait(100); |
hcha30 | 0:eabfe3700dd6 | 336 | } |
hcha30 | 0:eabfe3700dd6 | 337 | |
hcha30 | 0:eabfe3700dd6 | 338 | while (game==3) { |
hcha30 | 0:eabfe3700dd6 | 339 | |
hcha30 | 0:eabfe3700dd6 | 340 | uLCD.filled_rectangle(0,0,128,128,0xF03D39); |
hcha30 | 0:eabfe3700dd6 | 341 | uLCD.textbackground_color(0XF00000); |
hcha30 | 0:eabfe3700dd6 | 342 | uLCD.text_string("GAME OVER", 3, 4, FONT_7X8, BLACK); |
hcha30 | 0:eabfe3700dd6 | 343 | playSound("/sd/ending.wav"); |
hcha30 | 0:eabfe3700dd6 | 344 | wait(100); |
hcha30 | 0:eabfe3700dd6 | 345 | } |
hcha30 | 0:eabfe3700dd6 | 346 | } |
hcha30 | 0:eabfe3700dd6 | 347 | |
hcha30 | 0:eabfe3700dd6 | 348 | int * drawfishi(int x, int y, int ox, int oy, int r, int c, int dir, int side) |
hcha30 | 0:eabfe3700dd6 | 349 | { |
hcha30 | 0:eabfe3700dd6 | 350 | int out[4]; |
hcha30 | 0:eabfe3700dd6 | 351 | if (dir > 0) { |
hcha30 | 0:eabfe3700dd6 | 352 | x = dir==4 ? x-move : dir==6 ? x+move : x; |
hcha30 | 0:eabfe3700dd6 | 353 | y = dir==1 ? y-move : dir==5 ? y+move : y; |
hcha30 | 0:eabfe3700dd6 | 354 | } |
hcha30 | 0:eabfe3700dd6 | 355 | out[0] = x; |
hcha30 | 0:eabfe3700dd6 | 356 | out[1] = y; |
hcha30 | 0:eabfe3700dd6 | 357 | out[2] = ox; |
hcha30 | 0:eabfe3700dd6 | 358 | out[3] = oy; |
hcha30 | 0:eabfe3700dd6 | 359 | |
hcha30 | 0:eabfe3700dd6 | 360 | if (dir >0) { |
hcha30 | 0:eabfe3700dd6 | 361 | //remove old fishi |
hcha30 | 0:eabfe3700dd6 | 362 | uLCD.filled_rectangle(ox+r,oy+r,ox-23,oy-1-r,bgC); |
hcha30 | 0:eabfe3700dd6 | 363 | uLCD.filled_circle(x,y,r,c); //head |
hcha30 | 0:eabfe3700dd6 | 364 | for (i=0; i<5; i++) |
hcha30 | 0:eabfe3700dd6 | 365 | uLCD.line(x-23+i,y-3+i/2,x-23+i,y+3-i/2,c); //tail |
hcha30 | 0:eabfe3700dd6 | 366 | for (i=0; i<17; i++) |
hcha30 | 0:eabfe3700dd6 | 367 | uLCD.line(x-19+i,y-i/2,x-19+i,y+i/2,c); //body |
hcha30 | 0:eabfe3700dd6 | 368 | uLCD.filled_circle(x+2,y-3,1,0x000000); //eye |
hcha30 | 0:eabfe3700dd6 | 369 | for (i=2; i<5; i++) |
hcha30 | 0:eabfe3700dd6 | 370 | uLCD.line(x-2-i,y-i,x-2-i,y+i,0xFFFFFF); //fin |
hcha30 | 0:eabfe3700dd6 | 371 | out[2] = x; |
hcha30 | 0:eabfe3700dd6 | 372 | out[3] = y; |
hcha30 | 0:eabfe3700dd6 | 373 | return out; |
hcha30 | 0:eabfe3700dd6 | 374 | } |
hcha30 | 0:eabfe3700dd6 | 375 | if (dir == -1 && side == 0) { |
hcha30 | 0:eabfe3700dd6 | 376 | uLCD.filled_rectangle(ox+r,oy+r,ox-23,oy-1-r,bgC); |
hcha30 | 0:eabfe3700dd6 | 377 | uLCD.filled_circle(x,y,r,c); //head |
hcha30 | 0:eabfe3700dd6 | 378 | for (i=0; i<5; i++) |
hcha30 | 0:eabfe3700dd6 | 379 | uLCD.line(x-23+i,y-3+i/2,x-23+i,y+3-i/2,c); //tail |
hcha30 | 0:eabfe3700dd6 | 380 | for (i=0; i<17; i++) |
hcha30 | 0:eabfe3700dd6 | 381 | uLCD.line(x-19+i,y-i/2,x-19+i,y+i/2,c); //body |
hcha30 | 0:eabfe3700dd6 | 382 | out[2] = x; |
hcha30 | 0:eabfe3700dd6 | 383 | out[3] = y; |
hcha30 | 0:eabfe3700dd6 | 384 | return out; |
hcha30 | 0:eabfe3700dd6 | 385 | } |
hcha30 | 0:eabfe3700dd6 | 386 | |
hcha30 | 0:eabfe3700dd6 | 387 | if (dir == -1 && side == 1) { |
hcha30 | 0:eabfe3700dd6 | 388 | uLCD.filled_rectangle(ox-r,oy-r-1,ox+23,oy+r,bgC); |
hcha30 | 0:eabfe3700dd6 | 389 | uLCD.filled_circle(x,y,r,c); //head |
hcha30 | 0:eabfe3700dd6 | 390 | for (i=0; i<5; i++) |
hcha30 | 0:eabfe3700dd6 | 391 | uLCD.line(x+23-i,y-3+i/2,x+23-i,y+3-i/2,c); //tail |
hcha30 | 0:eabfe3700dd6 | 392 | for (i=0; i<17; i++) |
hcha30 | 0:eabfe3700dd6 | 393 | uLCD.line(x+19-i,y-i/2,x+19-i,y+i/2,c); //body |
hcha30 | 0:eabfe3700dd6 | 394 | out[2] = x; |
hcha30 | 0:eabfe3700dd6 | 395 | out[3] = y; |
hcha30 | 0:eabfe3700dd6 | 396 | return out; |
hcha30 | 0:eabfe3700dd6 | 397 | } |
hcha30 | 0:eabfe3700dd6 | 398 | return out; |
hcha30 | 0:eabfe3700dd6 | 399 | } |
hcha30 | 0:eabfe3700dd6 | 400 | |
hcha30 | 0:eabfe3700dd6 | 401 | int keypad() |
hcha30 | 0:eabfe3700dd6 | 402 | { |
hcha30 | 0:eabfe3700dd6 | 403 | int value=mpr121.read(0x00); |
hcha30 | 0:eabfe3700dd6 | 404 | |
hcha30 | 0:eabfe3700dd6 | 405 | switch (value) { |
hcha30 | 0:eabfe3700dd6 | 406 | case 0x40: // -> |
hcha30 | 0:eabfe3700dd6 | 407 | return 6; |
hcha30 | 0:eabfe3700dd6 | 408 | |
hcha30 | 0:eabfe3700dd6 | 409 | case 0x10: //<- |
hcha30 | 0:eabfe3700dd6 | 410 | return 4; |
hcha30 | 0:eabfe3700dd6 | 411 | |
hcha30 | 0:eabfe3700dd6 | 412 | case 0x20: // down |
hcha30 | 0:eabfe3700dd6 | 413 | return 5; |
hcha30 | 0:eabfe3700dd6 | 414 | |
hcha30 | 0:eabfe3700dd6 | 415 | case 0x02: // up |
hcha30 | 0:eabfe3700dd6 | 416 | return 1; |
hcha30 | 0:eabfe3700dd6 | 417 | } |
hcha30 | 0:eabfe3700dd6 | 418 | return 0; |
hcha30 | 0:eabfe3700dd6 | 419 | } |