Simplified Doodle Jump game for mbed
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed-rtos mbed wave_player
main.cpp@3:141c57be5a2d, 2016-03-15 (annotated)
- Committer:
- bhill42
- Date:
- Tue Mar 15 02:34:14 2016 +0000
- Revision:
- 3:141c57be5a2d
- Parent:
- 2:e76f3f3c85c0
Final
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhill42 | 3:141c57be5a2d | 1 | // Simple game of Doodle Jump with a BMP image, sound, and IMU control. |
bhill42 | 0:adf8cf9eba73 | 2 | #include "mbed.h" |
bhill42 | 1:bdeb188cb474 | 3 | #include "rtos.h" |
bhill42 | 3:141c57be5a2d | 4 | #include "SDFileSystem.h" /* SD card library */ |
bhill42 | 3:141c57be5a2d | 5 | #include "uLCD_4DGL.h" /* LCD library */ |
bhill42 | 3:141c57be5a2d | 6 | #include "LSM9DS1.h" /* IMU library */ |
bhill42 | 3:141c57be5a2d | 7 | #include "wave_player.h" /* wav file playing */ |
bhill42 | 3:141c57be5a2d | 8 | #include "myBMP.h" /* BMP image handling */ |
bhill42 | 1:bdeb188cb474 | 9 | #include <stdlib.h> /* srand, rand */ |
bhill42 | 1:bdeb188cb474 | 10 | #include <time.h> /* time */ |
bhill42 | 3:141c57be5a2d | 11 | |
bhill42 | 3:141c57be5a2d | 12 | //#define PI 3.14159 |
bhill42 | 3:141c57be5a2d | 13 | //#define DECLINATION -4.94 // Declination (degrees) in Atlanta,GA. |
bhill42 | 1:bdeb188cb474 | 14 | #define GRAVITY 3 /* acceleration per frame */ |
bhill42 | 1:bdeb188cb474 | 15 | #define MAX_PLATFORMS 5 |
bhill42 | 1:bdeb188cb474 | 16 | #define MAX_ENEMIES 2 |
bhill42 | 1:bdeb188cb474 | 17 | #define PLAYER_WIDTH 9 |
bhill42 | 1:bdeb188cb474 | 18 | #define PLAYER_HEIGHT 10 |
bhill42 | 2:e76f3f3c85c0 | 19 | // range of y values platforms can be between |
bhill42 | 2:e76f3f3c85c0 | 20 | #define MIN_PLATFORM_INTERVAL 15 |
bhill42 | 2:e76f3f3c85c0 | 21 | #define MAX_PLATFORM_INTERVAL 40 |
bhill42 | 2:e76f3f3c85c0 | 22 | #define PLATFORM_WIDTH 20 |
bhill42 | 2:e76f3f3c85c0 | 23 | #define PLATFORM_HEIGHT 5 |
bhill42 | 2:e76f3f3c85c0 | 24 | #define INIT_PLAYER_VELOCITY -15 |
bhill42 | 0:adf8cf9eba73 | 25 | |
bhill42 | 0:adf8cf9eba73 | 26 | |
bhill42 | 3:141c57be5a2d | 27 | uLCD_4DGL uLCD(p28,p27,p30); // serial tx, serial rx, reset pin |
bhill42 | 0:adf8cf9eba73 | 28 | LSM9DS1 IMU(p9, p10, 0xD6, 0x3C); |
bhill42 | 0:adf8cf9eba73 | 29 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
bhill42 | 3:141c57be5a2d | 30 | AnalogOut DACout(p18); //analog out for speaker |
bhill42 | 3:141c57be5a2d | 31 | wave_player waver(&DACout); |
bhill42 | 0:adf8cf9eba73 | 32 | |
bhill42 | 0:adf8cf9eba73 | 33 | DigitalOut myled1(LED1); |
bhill42 | 3:141c57be5a2d | 34 | DigitalOut myled2(LED2); |
bhill42 | 0:adf8cf9eba73 | 35 | |
bhill42 | 0:adf8cf9eba73 | 36 | Mutex sound_mutex; |
bhill42 | 3:141c57be5a2d | 37 | Thread * t1 = NULL; // must set to null for initial delete, calling delete on a null pointer is safe |
bhill42 | 0:adf8cf9eba73 | 38 | |
bhill42 | 1:bdeb188cb474 | 39 | typedef struct xycoord { |
bhill42 | 1:bdeb188cb474 | 40 | int x; |
bhill42 | 1:bdeb188cb474 | 41 | int y; |
bhill42 | 1:bdeb188cb474 | 42 | } xycoord; |
bhill42 | 1:bdeb188cb474 | 43 | |
bhill42 | 1:bdeb188cb474 | 44 | xycoord platforms[MAX_PLATFORMS]; |
bhill42 | 3:141c57be5a2d | 45 | |
bhill42 | 3:141c57be5a2d | 46 | // play a song of given filename, void const * input for use with threads |
bhill42 | 3:141c57be5a2d | 47 | // thread safe, will not try to play more than one sound at once |
bhill42 | 3:141c57be5a2d | 48 | void playSound(void const * args); |
bhill42 | 3:141c57be5a2d | 49 | |
bhill42 | 3:141c57be5a2d | 50 | // return index of platform with lowest y coordinates (highest on screen) |
bhill42 | 3:141c57be5a2d | 51 | int getHighestPlatform(); |
bhill42 | 3:141c57be5a2d | 52 | |
bhill42 | 3:141c57be5a2d | 53 | // return index of platform with highest y coordinates (lowst on screen) |
bhill42 | 3:141c57be5a2d | 54 | int getLowestPlatform(); |
bhill42 | 3:141c57be5a2d | 55 | |
bhill42 | 3:141c57be5a2d | 56 | // if a generate x, y coordinates above highest platform |
bhill42 | 3:141c57be5a2d | 57 | void getNextPlatform(int &x, int &y); |
bhill42 | 3:141c57be5a2d | 58 | |
bhill42 | 3:141c57be5a2d | 59 | // create all platforms |
bhill42 | 3:141c57be5a2d | 60 | void initPlatforms(); |
bhill42 | 1:bdeb188cb474 | 61 | |
bhill42 | 3:141c57be5a2d | 62 | // try to play a sound, do nothing if mutex locked |
bhill42 | 3:141c57be5a2d | 63 | void trySound(const char * file); |
bhill42 | 3:141c57be5a2d | 64 | |
bhill42 | 3:141c57be5a2d | 65 | int main() |
bhill42 | 3:141c57be5a2d | 66 | { |
bhill42 | 3:141c57be5a2d | 67 | wait(2.0f); // wait a bit for initialization |
bhill42 | 3:141c57be5a2d | 68 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 69 | // jack up LCD baud rate to max for fast display |
bhill42 | 3:141c57be5a2d | 70 | uLCD.baudrate(3000000); |
bhill42 | 3:141c57be5a2d | 71 | trySound("/sd/DMX.wav"); // play init sound |
bhill42 | 3:141c57be5a2d | 72 | |
bhill42 | 3:141c57be5a2d | 73 | // set up IMU |
bhill42 | 3:141c57be5a2d | 74 | uLCD.printf("Calibrating IMU, place on flat surface. Please wait..."); |
bhill42 | 3:141c57be5a2d | 75 | IMU.begin(); |
bhill42 | 3:141c57be5a2d | 76 | if (!IMU.begin()) { |
bhill42 | 3:141c57be5a2d | 77 | uLCD.printf("Failed to communicate with LSM9DS1.\n"); |
bhill42 | 3:141c57be5a2d | 78 | wait(5.0f); |
bhill42 | 3:141c57be5a2d | 79 | return -1; |
bhill42 | 3:141c57be5a2d | 80 | } |
bhill42 | 3:141c57be5a2d | 81 | IMU.calibrate(1); |
bhill42 | 3:141c57be5a2d | 82 | IMU.calibrateMag(0); |
bhill42 | 3:141c57be5a2d | 83 | |
bhill42 | 3:141c57be5a2d | 84 | //start countdown timer for player to start playing |
bhill42 | 3:141c57be5a2d | 85 | |
bhill42 | 3:141c57be5a2d | 86 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 87 | uLCD.locate(0,8); |
bhill42 | 3:141c57be5a2d | 88 | uLCD.printf("Starting in: 3"); |
bhill42 | 3:141c57be5a2d | 89 | wait(1.0f); |
bhill42 | 3:141c57be5a2d | 90 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 91 | uLCD.locate(0,8); |
bhill42 | 3:141c57be5a2d | 92 | uLCD.printf("Starting in: 2"); |
bhill42 | 3:141c57be5a2d | 93 | wait(1.0f); |
bhill42 | 3:141c57be5a2d | 94 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 95 | uLCD.locate(0,8); |
bhill42 | 3:141c57be5a2d | 96 | uLCD.printf("Starting in: 1"); |
bhill42 | 3:141c57be5a2d | 97 | wait(0.6f); |
bhill42 | 3:141c57be5a2d | 98 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 99 | uLCD.locate(0,15); |
bhill42 | 3:141c57be5a2d | 100 | uLCD.printf("TA SAYS WHAT"); |
bhill42 | 3:141c57be5a2d | 101 | |
bhill42 | 3:141c57be5a2d | 102 | |
bhill42 | 3:141c57be5a2d | 103 | |
bhill42 | 3:141c57be5a2d | 104 | //float fx=63.0,fy=63.0,vx=1.0,vy=0.4; |
bhill42 | 3:141c57be5a2d | 105 | // x, y is player x and y, player_velocity is player movement in pixels/frame, absolute_y is the y pixels the camera has "moved" up, old_x old_y for drawing over old player image |
bhill42 | 3:141c57be5a2d | 106 | int x = 64+(PLAYER_WIDTH + 1)/2, y = 127-PLAYER_HEIGHT; //,radius=4; |
bhill42 | 3:141c57be5a2d | 107 | int player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 3:141c57be5a2d | 108 | int absolute_y = 0; |
bhill42 | 3:141c57be5a2d | 109 | int old_x = x, old_y = y; |
bhill42 | 3:141c57be5a2d | 110 | uLCD.background_color(BLACK); |
bhill42 | 3:141c57be5a2d | 111 | |
bhill42 | 3:141c57be5a2d | 112 | |
bhill42 | 3:141c57be5a2d | 113 | |
bhill42 | 3:141c57be5a2d | 114 | float ax; // accelerometer reading |
bhill42 | 3:141c57be5a2d | 115 | |
bhill42 | 3:141c57be5a2d | 116 | RGBApixel *Colors = new RGBApixel [2]; |
bhill42 | 3:141c57be5a2d | 117 | |
bhill42 | 3:141c57be5a2d | 118 | // init random number generator, remove text from IMU |
bhill42 | 3:141c57be5a2d | 119 | srand(time(0)); |
bhill42 | 3:141c57be5a2d | 120 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 121 | |
bhill42 | 3:141c57be5a2d | 122 | // initialize platforms and prepare them for drawing |
bhill42 | 3:141c57be5a2d | 123 | initPlatforms(); |
bhill42 | 3:141c57be5a2d | 124 | int lowestPlatformIndex; |
bhill42 | 3:141c57be5a2d | 125 | int platx; |
bhill42 | 3:141c57be5a2d | 126 | int platy; |
bhill42 | 3:141c57be5a2d | 127 | |
bhill42 | 3:141c57be5a2d | 128 | // store the pixel values of player in memory to reduce the drawing time per frame |
bhill42 | 3:141c57be5a2d | 129 | // note that this image should be small, mbed's memory is not very large |
bhill42 | 3:141c57be5a2d | 130 | int * color_array = new int[PLAYER_WIDTH*PLAYER_HEIGHT]; |
bhill42 | 3:141c57be5a2d | 131 | ReadColorsFromFile("/sd/stardesu.bmp", Colors, color_array, x, y); |
bhill42 | 3:141c57be5a2d | 132 | |
bhill42 | 3:141c57be5a2d | 133 | // make score color white |
bhill42 | 3:141c57be5a2d | 134 | uLCD.color(WHITE); |
bhill42 | 3:141c57be5a2d | 135 | |
bhill42 | 3:141c57be5a2d | 136 | // enter main game loop |
bhill42 | 3:141c57be5a2d | 137 | while (1) |
bhill42 | 3:141c57be5a2d | 138 | { |
bhill42 | 3:141c57be5a2d | 139 | // read from IMU |
bhill42 | 3:141c57be5a2d | 140 | while(!IMU.accelAvailable()); |
bhill42 | 3:141c57be5a2d | 141 | IMU.readAccel(); |
bhill42 | 3:141c57be5a2d | 142 | ax = IMU.calcAccel(IMU.ax); |
bhill42 | 3:141c57be5a2d | 143 | |
bhill42 | 3:141c57be5a2d | 144 | // get lowest platform |
bhill42 | 3:141c57be5a2d | 145 | lowestPlatformIndex = getLowestPlatform(); |
bhill42 | 3:141c57be5a2d | 146 | platx = platforms[lowestPlatformIndex].x; |
bhill42 | 3:141c57be5a2d | 147 | platy = platforms[lowestPlatformIndex].y; |
bhill42 | 3:141c57be5a2d | 148 | |
bhill42 | 3:141c57be5a2d | 149 | // platform is off screen, create a new one |
bhill42 | 3:141c57be5a2d | 150 | if (platy > 127) |
bhill42 | 3:141c57be5a2d | 151 | { |
bhill42 | 3:141c57be5a2d | 152 | getNextPlatform(platx, platy); |
bhill42 | 3:141c57be5a2d | 153 | platforms[lowestPlatformIndex].y = platy; |
bhill42 | 3:141c57be5a2d | 154 | platforms[lowestPlatformIndex].x = platx; |
bhill42 | 3:141c57be5a2d | 155 | } |
bhill42 | 3:141c57be5a2d | 156 | |
bhill42 | 3:141c57be5a2d | 157 | // check collisions of player with platforms |
bhill42 | 3:141c57be5a2d | 158 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 3:141c57be5a2d | 159 | { |
bhill42 | 3:141c57be5a2d | 160 | if (player_velocity > 0 && (x+PLAYER_WIDTH) > platforms[i].x && x < (platforms[i].x + PLATFORM_WIDTH) && (y+PLAYER_HEIGHT) >= (platforms[i].y) && (y+PLAYER_HEIGHT) <= (platforms[i].y + PLATFORM_HEIGHT+7)) |
bhill42 | 3:141c57be5a2d | 161 | { |
bhill42 | 3:141c57be5a2d | 162 | player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 3:141c57be5a2d | 163 | y = platforms[i].y-(PLAYER_HEIGHT-1); |
bhill42 | 3:141c57be5a2d | 164 | // bounce sound |
bhill42 | 3:141c57be5a2d | 165 | trySound("/sd/DMX3.wav"); |
bhill42 | 3:141c57be5a2d | 166 | break; |
bhill42 | 3:141c57be5a2d | 167 | } |
bhill42 | 3:141c57be5a2d | 168 | } |
bhill42 | 3:141c57be5a2d | 169 | |
bhill42 | 3:141c57be5a2d | 170 | |
bhill42 | 3:141c57be5a2d | 171 | |
bhill42 | 3:141c57be5a2d | 172 | //erase old player location by drawing over it |
bhill42 | 3:141c57be5a2d | 173 | uLCD.filled_rectangle(old_x, old_y, old_x+PLAYER_WIDTH, old_y+PLAYER_HEIGHT, BLACK); |
bhill42 | 3:141c57be5a2d | 174 | |
bhill42 | 3:141c57be5a2d | 175 | // if player is at middle of screen, stop moving player and start moving platforms relative to player |
bhill42 | 3:141c57be5a2d | 176 | // "halfway point" is at pixal 64 = 63, force player to be unable to go above 63 |
bhill42 | 3:141c57be5a2d | 177 | if (y < 63) |
bhill42 | 3:141c57be5a2d | 178 | { |
bhill42 | 3:141c57be5a2d | 179 | int difference = 63 - y; |
bhill42 | 3:141c57be5a2d | 180 | y = 63; |
bhill42 | 3:141c57be5a2d | 181 | absolute_y+=difference; |
bhill42 | 3:141c57be5a2d | 182 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 3:141c57be5a2d | 183 | { |
bhill42 | 3:141c57be5a2d | 184 | platx = platforms[i].x; |
bhill42 | 3:141c57be5a2d | 185 | platy = platforms[i].y; |
bhill42 | 3:141c57be5a2d | 186 | // draw over old location |
bhill42 | 3:141c57be5a2d | 187 | uLCD.filled_rectangle(platx, platy, platx+PLATFORM_WIDTH, platy+PLATFORM_HEIGHT, BLACK); |
bhill42 | 3:141c57be5a2d | 188 | platforms[i].y+=difference; |
bhill42 | 3:141c57be5a2d | 189 | } |
bhill42 | 3:141c57be5a2d | 190 | } |
bhill42 | 3:141c57be5a2d | 191 | // draw platforms |
bhill42 | 3:141c57be5a2d | 192 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 3:141c57be5a2d | 193 | { |
bhill42 | 3:141c57be5a2d | 194 | uLCD.filled_rectangle(platforms[i].x, platforms[i].y, platforms[i].x + PLATFORM_WIDTH - 1, platforms[i].y + PLATFORM_HEIGHT - 1, GREEN); |
bhill42 | 3:141c57be5a2d | 195 | } |
bhill42 | 3:141c57be5a2d | 196 | // draw new player location |
bhill42 | 3:141c57be5a2d | 197 | //ReadBMPFromFile("/sd/stardesu.bmp", Colors, &uLCD, x,y); |
bhill42 | 3:141c57be5a2d | 198 | DrawColorstoLCD(color_array, &uLCD, x, y, PLAYER_WIDTH, PLAYER_HEIGHT); |
bhill42 | 3:141c57be5a2d | 199 | wait(.05f); |
bhill42 | 3:141c57be5a2d | 200 | // print absolute y as player score |
bhill42 | 3:141c57be5a2d | 201 | uLCD.locate(0, 0); |
bhill42 | 3:141c57be5a2d | 202 | uLCD.printf("Score: %i", absolute_y); |
bhill42 | 3:141c57be5a2d | 203 | |
bhill42 | 3:141c57be5a2d | 204 | // save previous player coordinates, update player coordinates |
bhill42 | 3:141c57be5a2d | 205 | old_x = x; |
bhill42 | 3:141c57be5a2d | 206 | old_y = y; |
bhill42 | 3:141c57be5a2d | 207 | x = 64-PLAYER_WIDTH/2 + (int)(64.0f * ax); |
bhill42 | 3:141c57be5a2d | 208 | y += player_velocity; |
bhill42 | 3:141c57be5a2d | 209 | // apply "gravity" and "terminal velocity" to current player velocity |
bhill42 | 3:141c57be5a2d | 210 | player_velocity += GRAVITY; |
bhill42 | 3:141c57be5a2d | 211 | if (player_velocity > -1*INIT_PLAYER_VELOCITY) |
bhill42 | 3:141c57be5a2d | 212 | { |
bhill42 | 3:141c57be5a2d | 213 | player_velocity = -1*INIT_PLAYER_VELOCITY; |
bhill42 | 3:141c57be5a2d | 214 | } |
bhill42 | 3:141c57be5a2d | 215 | |
bhill42 | 3:141c57be5a2d | 216 | |
bhill42 | 3:141c57be5a2d | 217 | |
bhill42 | 3:141c57be5a2d | 218 | |
bhill42 | 3:141c57be5a2d | 219 | |
bhill42 | 3:141c57be5a2d | 220 | |
bhill42 | 3:141c57be5a2d | 221 | // check game over condition |
bhill42 | 3:141c57be5a2d | 222 | if ( y >= 127-PLAYER_HEIGHT && player_velocity > 0) |
bhill42 | 3:141c57be5a2d | 223 | { |
bhill42 | 3:141c57be5a2d | 224 | // kill player, game over |
bhill42 | 3:141c57be5a2d | 225 | wait(.1f); |
bhill42 | 3:141c57be5a2d | 226 | trySound("/sd/RR.wav"); |
bhill42 | 3:141c57be5a2d | 227 | trySound("/sd/RR.wav"); |
bhill42 | 3:141c57be5a2d | 228 | wait(.1f); |
bhill42 | 3:141c57be5a2d | 229 | while(1) |
bhill42 | 3:141c57be5a2d | 230 | { |
bhill42 | 3:141c57be5a2d | 231 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 232 | for (int i = 0; i < 20; i++) |
bhill42 | 3:141c57be5a2d | 233 | { |
bhill42 | 3:141c57be5a2d | 234 | uLCD.color(RED); |
bhill42 | 3:141c57be5a2d | 235 | uLCD.printf("GET REKT"); |
bhill42 | 3:141c57be5a2d | 236 | uLCD.color(GREEN); |
bhill42 | 3:141c57be5a2d | 237 | uLCD.printf("GET REKT"); |
bhill42 | 3:141c57be5a2d | 238 | } |
bhill42 | 3:141c57be5a2d | 239 | wait(.1f); |
bhill42 | 3:141c57be5a2d | 240 | uLCD.cls(); |
bhill42 | 3:141c57be5a2d | 241 | for (int i = 0; i < 80; i++) |
bhill42 | 3:141c57be5a2d | 242 | { |
bhill42 | 3:141c57be5a2d | 243 | uLCD.color(RED); |
bhill42 | 3:141c57be5a2d | 244 | uLCD.printf("(="); |
bhill42 | 3:141c57be5a2d | 245 | uLCD.color(GREEN); |
bhill42 | 3:141c57be5a2d | 246 | uLCD.printf("(="); |
bhill42 | 3:141c57be5a2d | 247 | } |
bhill42 | 3:141c57be5a2d | 248 | wait(.1f); |
bhill42 | 3:141c57be5a2d | 249 | } |
bhill42 | 3:141c57be5a2d | 250 | //player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 3:141c57be5a2d | 251 | //y = 127-PLAYER_HEIGHT; |
bhill42 | 3:141c57be5a2d | 252 | } |
bhill42 | 3:141c57be5a2d | 253 | } // end of main loop |
bhill42 | 3:141c57be5a2d | 254 | } // end of main |
bhill42 | 3:141c57be5a2d | 255 | |
bhill42 | 3:141c57be5a2d | 256 | // function implementations |
bhill42 | 3:141c57be5a2d | 257 | |
bhill42 | 3:141c57be5a2d | 258 | void playSound(void const * args) |
bhill42 | 0:adf8cf9eba73 | 259 | { |
bhill42 | 0:adf8cf9eba73 | 260 | sound_mutex.lock(); |
bhill42 | 3:141c57be5a2d | 261 | FILE *wave_file; |
bhill42 | 3:141c57be5a2d | 262 | char const * actual_args = (char const *)args; |
bhill42 | 3:141c57be5a2d | 263 | wave_file = fopen(actual_args,"r"); // change to whatever sound |
bhill42 | 0:adf8cf9eba73 | 264 | if (wave_file == NULL) |
bhill42 | 0:adf8cf9eba73 | 265 | { |
bhill42 | 0:adf8cf9eba73 | 266 | myled1 = 1; |
bhill42 | 0:adf8cf9eba73 | 267 | } |
bhill42 | 0:adf8cf9eba73 | 268 | else |
bhill42 | 0:adf8cf9eba73 | 269 | { |
bhill42 | 0:adf8cf9eba73 | 270 | myled2 = 1; |
bhill42 | 0:adf8cf9eba73 | 271 | } |
bhill42 | 0:adf8cf9eba73 | 272 | waver.play(wave_file); |
bhill42 | 0:adf8cf9eba73 | 273 | fclose(wave_file); |
bhill42 | 0:adf8cf9eba73 | 274 | sound_mutex.unlock(); |
bhill42 | 0:adf8cf9eba73 | 275 | } |
bhill42 | 2:e76f3f3c85c0 | 276 | |
bhill42 | 3:141c57be5a2d | 277 | |
bhill42 | 2:e76f3f3c85c0 | 278 | int getHighestPlatform() |
bhill42 | 2:e76f3f3c85c0 | 279 | { |
bhill42 | 2:e76f3f3c85c0 | 280 | int highest_index = 0; |
bhill42 | 2:e76f3f3c85c0 | 281 | for (int i = 1; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 282 | { |
bhill42 | 2:e76f3f3c85c0 | 283 | if (platforms[i].y < platforms[highest_index].y) |
bhill42 | 2:e76f3f3c85c0 | 284 | highest_index = i; |
bhill42 | 2:e76f3f3c85c0 | 285 | } |
bhill42 | 2:e76f3f3c85c0 | 286 | return highest_index; |
bhill42 | 2:e76f3f3c85c0 | 287 | } |
bhill42 | 2:e76f3f3c85c0 | 288 | |
bhill42 | 3:141c57be5a2d | 289 | |
bhill42 | 3:141c57be5a2d | 290 | int getLowestPlatform() |
bhill42 | 3:141c57be5a2d | 291 | { |
bhill42 | 3:141c57be5a2d | 292 | int lowest_index = 0; |
bhill42 | 3:141c57be5a2d | 293 | for (int i = 1; i < MAX_PLATFORMS; i++) |
bhill42 | 3:141c57be5a2d | 294 | { |
bhill42 | 3:141c57be5a2d | 295 | if (platforms[i].y > platforms[lowest_index].y) |
bhill42 | 3:141c57be5a2d | 296 | lowest_index = i; |
bhill42 | 3:141c57be5a2d | 297 | } |
bhill42 | 3:141c57be5a2d | 298 | return lowest_index; |
bhill42 | 3:141c57be5a2d | 299 | } |
bhill42 | 3:141c57be5a2d | 300 | |
bhill42 | 3:141c57be5a2d | 301 | |
bhill42 | 2:e76f3f3c85c0 | 302 | void getNextPlatform(int &x, int &y) |
bhill42 | 2:e76f3f3c85c0 | 303 | { |
bhill42 | 2:e76f3f3c85c0 | 304 | x = rand()%(128-PLATFORM_WIDTH+1); |
bhill42 | 2:e76f3f3c85c0 | 305 | y = platforms[getHighestPlatform()].y - (rand()%(MAX_PLATFORM_INTERVAL-MIN_PLATFORM_INTERVAL+1) + MIN_PLATFORM_INTERVAL); |
bhill42 | 2:e76f3f3c85c0 | 306 | } |
bhill42 | 2:e76f3f3c85c0 | 307 | |
bhill42 | 3:141c57be5a2d | 308 | |
bhill42 | 2:e76f3f3c85c0 | 309 | void initPlatforms() |
bhill42 | 2:e76f3f3c85c0 | 310 | { |
bhill42 | 2:e76f3f3c85c0 | 311 | // first set all to 0 |
bhill42 | 2:e76f3f3c85c0 | 312 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 313 | { |
bhill42 | 2:e76f3f3c85c0 | 314 | platforms[i].x = 0; |
bhill42 | 2:e76f3f3c85c0 | 315 | platforms[i].y = 127; |
bhill42 | 2:e76f3f3c85c0 | 316 | } |
bhill42 | 2:e76f3f3c85c0 | 317 | int x; |
bhill42 | 2:e76f3f3c85c0 | 318 | int y; |
bhill42 | 2:e76f3f3c85c0 | 319 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 320 | { |
bhill42 | 2:e76f3f3c85c0 | 321 | getNextPlatform(x, y); |
bhill42 | 2:e76f3f3c85c0 | 322 | platforms[i].x = x; |
bhill42 | 2:e76f3f3c85c0 | 323 | platforms[i].y = y; |
bhill42 | 2:e76f3f3c85c0 | 324 | } |
bhill42 | 2:e76f3f3c85c0 | 325 | } |
bhill42 | 3:141c57be5a2d | 326 | |
bhill42 | 3:141c57be5a2d | 327 | |
bhill42 | 3:141c57be5a2d | 328 | void trySound(const char * file) |
bhill42 | 0:adf8cf9eba73 | 329 | { |
bhill42 | 0:adf8cf9eba73 | 330 | if (sound_mutex.trylock()) |
bhill42 | 0:adf8cf9eba73 | 331 | { |
bhill42 | 0:adf8cf9eba73 | 332 | delete t1; |
bhill42 | 3:141c57be5a2d | 333 | t1 = new Thread(playSound, (void*)file); |
bhill42 | 0:adf8cf9eba73 | 334 | sound_mutex.unlock(); |
bhill42 | 0:adf8cf9eba73 | 335 | } |
bhill42 | 0:adf8cf9eba73 | 336 | } |