Simplified Doodle Jump game for mbed
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed-rtos mbed wave_player
main.cpp@2:e76f3f3c85c0, 2016-03-14 (annotated)
- Committer:
- bhill42
- Date:
- Mon Mar 14 20:36:52 2016 +0000
- Revision:
- 2:e76f3f3c85c0
- Parent:
- 1:bdeb188cb474
- Child:
- 3:141c57be5a2d
Before BMP changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bhill42 | 0:adf8cf9eba73 | 1 | // uLCD-144-G2 demo program for uLCD-4GL LCD driver library |
bhill42 | 0:adf8cf9eba73 | 2 | // |
bhill42 | 0:adf8cf9eba73 | 3 | #include "mbed.h" |
bhill42 | 1:bdeb188cb474 | 4 | #include "rtos.h" |
bhill42 | 0:adf8cf9eba73 | 5 | #include "SDFileSystem.h" |
bhill42 | 1:bdeb188cb474 | 6 | #include "uLCD_4DGL.h" |
bhill42 | 1:bdeb188cb474 | 7 | #include "LSM9DS1.h" |
bhill42 | 0:adf8cf9eba73 | 8 | #include "wave_player.h" |
bhill42 | 1:bdeb188cb474 | 9 | #include "myBMP.h" |
bhill42 | 1:bdeb188cb474 | 10 | #include <stdlib.h> /* srand, rand */ |
bhill42 | 1:bdeb188cb474 | 11 | #include <time.h> /* time */ |
bhill42 | 0:adf8cf9eba73 | 12 | #define PI 3.14159 |
bhill42 | 0:adf8cf9eba73 | 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 | 0:adf8cf9eba73 | 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 | |
bhill42 | 0:adf8cf9eba73 | 30 | SDFileSystem sd(p5, p6, p7, p8, "sd"); |
bhill42 | 0:adf8cf9eba73 | 31 | |
bhill42 | 0:adf8cf9eba73 | 32 | DigitalOut myled1(LED1); |
bhill42 | 0:adf8cf9eba73 | 33 | DigitalOut myled2(LED2); |
bhill42 | 2:e76f3f3c85c0 | 34 | DigitalOut myled3(LED3); |
bhill42 | 2:e76f3f3c85c0 | 35 | DigitalOut myled4(LED4); |
bhill42 | 0:adf8cf9eba73 | 36 | AnalogOut DACout(p18); //analog out for speaker |
bhill42 | 0:adf8cf9eba73 | 37 | wave_player waver(&DACout); |
bhill42 | 0:adf8cf9eba73 | 38 | FILE *wave_file; |
bhill42 | 0:adf8cf9eba73 | 39 | |
bhill42 | 0:adf8cf9eba73 | 40 | Mutex sound_mutex; |
bhill42 | 2:e76f3f3c85c0 | 41 | Thread * t1 = NULL; |
bhill42 | 0:adf8cf9eba73 | 42 | |
bhill42 | 1:bdeb188cb474 | 43 | typedef struct xycoord { |
bhill42 | 1:bdeb188cb474 | 44 | int x; |
bhill42 | 1:bdeb188cb474 | 45 | int y; |
bhill42 | 1:bdeb188cb474 | 46 | } xycoord; |
bhill42 | 1:bdeb188cb474 | 47 | |
bhill42 | 1:bdeb188cb474 | 48 | xycoord platforms[MAX_PLATFORMS]; |
bhill42 | 1:bdeb188cb474 | 49 | xycoord enemies[MAX_ENEMIES]; |
bhill42 | 1:bdeb188cb474 | 50 | xycoord player; |
bhill42 | 2:e76f3f3c85c0 | 51 | int player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 2:e76f3f3c85c0 | 52 | int absolute_y = 0; |
bhill42 | 1:bdeb188cb474 | 53 | |
bhill42 | 0:adf8cf9eba73 | 54 | void playSound(void const * args) //play a song of given filename |
bhill42 | 0:adf8cf9eba73 | 55 | { |
bhill42 | 0:adf8cf9eba73 | 56 | sound_mutex.lock(); |
bhill42 | 0:adf8cf9eba73 | 57 | wave_file = fopen("/sd/DMX.wav","r"); // change to whatever sound |
bhill42 | 0:adf8cf9eba73 | 58 | if (wave_file == NULL) |
bhill42 | 0:adf8cf9eba73 | 59 | { |
bhill42 | 0:adf8cf9eba73 | 60 | myled1 = 1; |
bhill42 | 0:adf8cf9eba73 | 61 | } |
bhill42 | 0:adf8cf9eba73 | 62 | else |
bhill42 | 0:adf8cf9eba73 | 63 | { |
bhill42 | 0:adf8cf9eba73 | 64 | myled2 = 1; |
bhill42 | 0:adf8cf9eba73 | 65 | } |
bhill42 | 0:adf8cf9eba73 | 66 | |
bhill42 | 0:adf8cf9eba73 | 67 | waver.play(wave_file); |
bhill42 | 0:adf8cf9eba73 | 68 | fclose(wave_file); |
bhill42 | 0:adf8cf9eba73 | 69 | sound_mutex.unlock(); |
bhill42 | 0:adf8cf9eba73 | 70 | } |
bhill42 | 2:e76f3f3c85c0 | 71 | |
bhill42 | 2:e76f3f3c85c0 | 72 | int getHighestPlatform() |
bhill42 | 2:e76f3f3c85c0 | 73 | { |
bhill42 | 2:e76f3f3c85c0 | 74 | int highest_index = 0; |
bhill42 | 2:e76f3f3c85c0 | 75 | for (int i = 1; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 76 | { |
bhill42 | 2:e76f3f3c85c0 | 77 | if (platforms[i].y < platforms[highest_index].y) |
bhill42 | 2:e76f3f3c85c0 | 78 | highest_index = i; |
bhill42 | 2:e76f3f3c85c0 | 79 | } |
bhill42 | 2:e76f3f3c85c0 | 80 | return highest_index; |
bhill42 | 2:e76f3f3c85c0 | 81 | } |
bhill42 | 2:e76f3f3c85c0 | 82 | |
bhill42 | 2:e76f3f3c85c0 | 83 | void getNextPlatform(int &x, int &y) |
bhill42 | 2:e76f3f3c85c0 | 84 | { |
bhill42 | 2:e76f3f3c85c0 | 85 | x = rand()%(128-PLATFORM_WIDTH+1); |
bhill42 | 2:e76f3f3c85c0 | 86 | y = platforms[getHighestPlatform()].y - (rand()%(MAX_PLATFORM_INTERVAL-MIN_PLATFORM_INTERVAL+1) + MIN_PLATFORM_INTERVAL); |
bhill42 | 2:e76f3f3c85c0 | 87 | } |
bhill42 | 2:e76f3f3c85c0 | 88 | |
bhill42 | 2:e76f3f3c85c0 | 89 | void initPlatforms() |
bhill42 | 2:e76f3f3c85c0 | 90 | { |
bhill42 | 2:e76f3f3c85c0 | 91 | // first set all to 0 |
bhill42 | 2:e76f3f3c85c0 | 92 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 93 | { |
bhill42 | 2:e76f3f3c85c0 | 94 | platforms[i].x = 0; |
bhill42 | 2:e76f3f3c85c0 | 95 | platforms[i].y = 127; |
bhill42 | 2:e76f3f3c85c0 | 96 | } |
bhill42 | 2:e76f3f3c85c0 | 97 | int x; |
bhill42 | 2:e76f3f3c85c0 | 98 | int y; |
bhill42 | 2:e76f3f3c85c0 | 99 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 100 | { |
bhill42 | 2:e76f3f3c85c0 | 101 | getNextPlatform(x, y); |
bhill42 | 2:e76f3f3c85c0 | 102 | platforms[i].x = x; |
bhill42 | 2:e76f3f3c85c0 | 103 | platforms[i].y = y; |
bhill42 | 2:e76f3f3c85c0 | 104 | } |
bhill42 | 2:e76f3f3c85c0 | 105 | } |
bhill42 | 0:adf8cf9eba73 | 106 | |
bhill42 | 0:adf8cf9eba73 | 107 | int main() |
bhill42 | 0:adf8cf9eba73 | 108 | { |
bhill42 | 2:e76f3f3c85c0 | 109 | wait(2); |
bhill42 | 1:bdeb188cb474 | 110 | uLCD.cls(); |
bhill42 | 0:adf8cf9eba73 | 111 | //Thread t1(playSound); |
bhill42 | 0:adf8cf9eba73 | 112 | if (sound_mutex.trylock()) |
bhill42 | 0:adf8cf9eba73 | 113 | { |
bhill42 | 0:adf8cf9eba73 | 114 | delete t1; |
bhill42 | 0:adf8cf9eba73 | 115 | t1 = new Thread(playSound); |
bhill42 | 0:adf8cf9eba73 | 116 | sound_mutex.unlock(); |
bhill42 | 0:adf8cf9eba73 | 117 | } |
bhill42 | 2:e76f3f3c85c0 | 118 | |
bhill42 | 0:adf8cf9eba73 | 119 | |
bhill42 | 0:adf8cf9eba73 | 120 | uLCD.printf("Calibrating IMU, place on flat surface. Please wait..."); |
bhill42 | 0:adf8cf9eba73 | 121 | IMU.begin(); |
bhill42 | 0:adf8cf9eba73 | 122 | if (!IMU.begin()) { |
bhill42 | 0:adf8cf9eba73 | 123 | uLCD.printf("Failed to communicate with LSM9DS1.\n"); |
bhill42 | 2:e76f3f3c85c0 | 124 | wait(5); |
bhill42 | 2:e76f3f3c85c0 | 125 | return -1; |
bhill42 | 0:adf8cf9eba73 | 126 | } |
bhill42 | 0:adf8cf9eba73 | 127 | IMU.calibrate(1); |
bhill42 | 0:adf8cf9eba73 | 128 | IMU.calibrateMag(0); |
bhill42 | 0:adf8cf9eba73 | 129 | |
bhill42 | 0:adf8cf9eba73 | 130 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
bhill42 | 0:adf8cf9eba73 | 131 | |
bhill42 | 2:e76f3f3c85c0 | 132 | //float fx=63.0,fy=63.0,vx=1.0,vy=0.4; |
bhill42 | 2:e76f3f3c85c0 | 133 | int x=64+(PLAYER_WIDTH + 1)/2,y=127-PLAYER_HEIGHT,radius=4; |
bhill42 | 0:adf8cf9eba73 | 134 | uLCD.background_color(BLACK); |
bhill42 | 0:adf8cf9eba73 | 135 | |
bhill42 | 0:adf8cf9eba73 | 136 | |
bhill42 | 0:adf8cf9eba73 | 137 | |
bhill42 | 0:adf8cf9eba73 | 138 | float ax; |
bhill42 | 0:adf8cf9eba73 | 139 | |
bhill42 | 1:bdeb188cb474 | 140 | RGBApixel *Colors = new RGBApixel [2]; |
bhill42 | 0:adf8cf9eba73 | 141 | |
bhill42 | 1:bdeb188cb474 | 142 | srand(time(0)); |
bhill42 | 2:e76f3f3c85c0 | 143 | uLCD.cls(); |
bhill42 | 2:e76f3f3c85c0 | 144 | |
bhill42 | 2:e76f3f3c85c0 | 145 | initPlatforms(); |
bhill42 | 2:e76f3f3c85c0 | 146 | int highestPlatformIndex; |
bhill42 | 2:e76f3f3c85c0 | 147 | int platx; |
bhill42 | 2:e76f3f3c85c0 | 148 | int platy; |
bhill42 | 2:e76f3f3c85c0 | 149 | |
bhill42 | 0:adf8cf9eba73 | 150 | while (1) { |
bhill42 | 2:e76f3f3c85c0 | 151 | |
bhill42 | 0:adf8cf9eba73 | 152 | while(!IMU.accelAvailable()); |
bhill42 | 0:adf8cf9eba73 | 153 | IMU.readAccel(); |
bhill42 | 2:e76f3f3c85c0 | 154 | ax = IMU.calcAccel(IMU.ax); |
bhill42 | 2:e76f3f3c85c0 | 155 | |
bhill42 | 2:e76f3f3c85c0 | 156 | |
bhill42 | 2:e76f3f3c85c0 | 157 | highestPlatformIndex = getHighestPlatform(); |
bhill42 | 2:e76f3f3c85c0 | 158 | platy = platforms[highestPlatformIndex].y; |
bhill42 | 2:e76f3f3c85c0 | 159 | if (platy > 127) |
bhill42 | 2:e76f3f3c85c0 | 160 | { |
bhill42 | 2:e76f3f3c85c0 | 161 | getNextPlatform(platx, platy); |
bhill42 | 2:e76f3f3c85c0 | 162 | platforms[highestPlatformIndex].y = platy; |
bhill42 | 2:e76f3f3c85c0 | 163 | platforms[highestPlatformIndex].x = platx; |
bhill42 | 2:e76f3f3c85c0 | 164 | } |
bhill42 | 0:adf8cf9eba73 | 165 | |
bhill42 | 0:adf8cf9eba73 | 166 | |
bhill42 | 0:adf8cf9eba73 | 167 | |
bhill42 | 0:adf8cf9eba73 | 168 | |
bhill42 | 2:e76f3f3c85c0 | 169 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 170 | { |
bhill42 | 2:e76f3f3c85c0 | 171 | uLCD.filled_rectangle(platforms[i].x, platforms[i].y, platforms[i].x + PLATFORM_WIDTH - 1, platforms[i].y + PLATFORM_HEIGHT - 1, GREEN); |
bhill42 | 2:e76f3f3c85c0 | 172 | } |
bhill42 | 0:adf8cf9eba73 | 173 | |
bhill42 | 1:bdeb188cb474 | 174 | |
bhill42 | 0:adf8cf9eba73 | 175 | //draw ball |
bhill42 | 1:bdeb188cb474 | 176 | //uLCD.filled_circle(x, y, radius, RED); |
bhill42 | 2:e76f3f3c85c0 | 177 | |
bhill42 | 2:e76f3f3c85c0 | 178 | for (int i = 0; i < MAX_PLATFORMS; i++) |
bhill42 | 2:e76f3f3c85c0 | 179 | { |
bhill42 | 2:e76f3f3c85c0 | 180 | if (player_velocity > 0 && (x+PLAYER_WIDTH) > platforms[i].x && x < (platforms[i].x + PLATFORM_WIDTH) && (y+PLAYER_HEIGHT) >= (platforms[i].y-3) && (y+PLAYER_HEIGHT) <= (platforms[i].y + 6)) |
bhill42 | 2:e76f3f3c85c0 | 181 | { |
bhill42 | 2:e76f3f3c85c0 | 182 | |
bhill42 | 2:e76f3f3c85c0 | 183 | player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 2:e76f3f3c85c0 | 184 | |
bhill42 | 2:e76f3f3c85c0 | 185 | |
bhill42 | 2:e76f3f3c85c0 | 186 | } |
bhill42 | 2:e76f3f3c85c0 | 187 | |
bhill42 | 2:e76f3f3c85c0 | 188 | } |
bhill42 | 2:e76f3f3c85c0 | 189 | |
bhill42 | 2:e76f3f3c85c0 | 190 | |
bhill42 | 2:e76f3f3c85c0 | 191 | |
bhill42 | 1:bdeb188cb474 | 192 | ReadBMPFromFile("/sd/stardesu.bmp", Colors, &uLCD, x,y); |
bhill42 | 2:e76f3f3c85c0 | 193 | uLCD.locate(0, 0); |
bhill42 | 2:e76f3f3c85c0 | 194 | uLCD.printf("(%i, %i)", x, y); |
bhill42 | 0:adf8cf9eba73 | 195 | //bounce off edge walls and slow down a bit? |
bhill42 | 0:adf8cf9eba73 | 196 | //erase old ball location |
bhill42 | 1:bdeb188cb474 | 197 | uLCD.filled_rectangle(x, y, x+PLAYER_WIDTH, y+PLAYER_HEIGHT, BLACK); |
bhill42 | 2:e76f3f3c85c0 | 198 | |
bhill42 | 0:adf8cf9eba73 | 199 | |
bhill42 | 2:e76f3f3c85c0 | 200 | x = 64-PLAYER_WIDTH/2 + (int)(64.0f * ax); |
bhill42 | 2:e76f3f3c85c0 | 201 | y += player_velocity; |
bhill42 | 2:e76f3f3c85c0 | 202 | player_velocity += GRAVITY; |
bhill42 | 2:e76f3f3c85c0 | 203 | if ( y >= 127-PLAYER_HEIGHT) |
bhill42 | 2:e76f3f3c85c0 | 204 | { |
bhill42 | 2:e76f3f3c85c0 | 205 | player_velocity = INIT_PLAYER_VELOCITY; |
bhill42 | 2:e76f3f3c85c0 | 206 | y = 127-PLAYER_HEIGHT; |
bhill42 | 2:e76f3f3c85c0 | 207 | } |
bhill42 | 0:adf8cf9eba73 | 208 | |
bhill42 | 0:adf8cf9eba73 | 209 | |
bhill42 | 2:e76f3f3c85c0 | 210 | wait(.05); |
bhill42 | 0:adf8cf9eba73 | 211 | } |
bhill42 | 0:adf8cf9eba73 | 212 | |
bhill42 | 0:adf8cf9eba73 | 213 | } |