mbed Ball In the Hole Game
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed wave_player
main.cpp@2:04fe2cb3b815, 2016-03-17 (annotated)
- Committer:
- kthlee
- Date:
- Thu Mar 17 16:35:26 2016 +0000
- Revision:
- 2:04fe2cb3b815
- Parent:
- 1:2acd36169ca1
changed 5 to 10 (winning score)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kthlee | 0:cd3cae45447e | 1 | #include "LSM9DS1.h" |
kthlee | 0:cd3cae45447e | 2 | #include "uLCD_4DGL.h" |
kthlee | 0:cd3cae45447e | 3 | #include <stdlib.h> |
kthlee | 0:cd3cae45447e | 4 | #include <time.h> |
kthlee | 0:cd3cae45447e | 5 | #include "wave_player.h" |
kthlee | 0:cd3cae45447e | 6 | #include "SDFileSystem.h" |
kthlee | 0:cd3cae45447e | 7 | |
kthlee | 0:cd3cae45447e | 8 | SDFileSystem sd(p5, p6, p7, p8, "sd"); //SD card |
kthlee | 0:cd3cae45447e | 9 | uLCD_4DGL uLCD(p9,p10,p11); |
kthlee | 0:cd3cae45447e | 10 | Serial pc(USBTX, USBRX); |
kthlee | 0:cd3cae45447e | 11 | AnalogOut mySpeaker(p18); //speaker |
kthlee | 0:cd3cae45447e | 12 | wave_player player(&mySpeaker); // wav player |
kthlee | 0:cd3cae45447e | 13 | // |
kthlee | 0:cd3cae45447e | 14 | // |
kthlee | 0:cd3cae45447e | 15 | int main() { |
kthlee | 0:cd3cae45447e | 16 | // |
kthlee | 0:cd3cae45447e | 17 | |
kthlee | 0:cd3cae45447e | 18 | srand(time(NULL)); |
kthlee | 0:cd3cae45447e | 19 | float hole_x = rand() % 98 + 31; |
kthlee | 0:cd3cae45447e | 20 | float hole_y = rand() % 98 + 31; |
kthlee | 0:cd3cae45447e | 21 | float ball_x = 64; |
kthlee | 0:cd3cae45447e | 22 | float ball_y = 64; |
kthlee | 0:cd3cae45447e | 23 | |
kthlee | 0:cd3cae45447e | 24 | LSM9DS1 imu(p28, p27, 0xD6, 0x3C); |
kthlee | 0:cd3cae45447e | 25 | imu.begin(); |
kthlee | 0:cd3cae45447e | 26 | imu.calibrate(); |
kthlee | 0:cd3cae45447e | 27 | int score = 0; |
kthlee | 0:cd3cae45447e | 28 | int timer = 100; |
kthlee | 0:cd3cae45447e | 29 | bool win = false; |
kthlee | 0:cd3cae45447e | 30 | bool lose = false; |
kthlee | 0:cd3cae45447e | 31 | int starttime = 10; |
kthlee | 0:cd3cae45447e | 32 | uLCD.baudrate(BAUD_3000000); |
kthlee | 0:cd3cae45447e | 33 | FILE *wave_file2; |
kthlee | 0:cd3cae45447e | 34 | FILE *wave_file3; |
kthlee | 0:cd3cae45447e | 35 | volatile bool playStop = false; |
kthlee | 0:cd3cae45447e | 36 | volatile unsigned short volume = 0; |
kthlee | 0:cd3cae45447e | 37 | // |
kthlee | 0:cd3cae45447e | 38 | |
kthlee | 0:cd3cae45447e | 39 | uLCD.locate(1,0); |
kthlee | 0:cd3cae45447e | 40 | uLCD.printf("Ball In the Hole"); |
kthlee | 0:cd3cae45447e | 41 | uLCD.locate(2,4); |
kthlee | 0:cd3cae45447e | 42 | uLCD.printf("Roll ball into"); |
kthlee | 0:cd3cae45447e | 43 | uLCD.locate(3,5); |
kthlee | 0:cd3cae45447e | 44 | uLCD.printf("hole 10 times"); |
kthlee | 0:cd3cae45447e | 45 | uLCD.locate(4,6); |
kthlee | 0:cd3cae45447e | 46 | uLCD.printf("before time"); |
kthlee | 0:cd3cae45447e | 47 | uLCD.locate(5,7); |
kthlee | 0:cd3cae45447e | 48 | uLCD.printf("runs out!"); |
kthlee | 0:cd3cae45447e | 49 | while (1) { |
kthlee | 0:cd3cae45447e | 50 | uLCD.locate(1,10); |
kthlee | 0:cd3cae45447e | 51 | uLCD.printf("Game starts in:%d ", starttime); |
kthlee | 0:cd3cae45447e | 52 | starttime--; |
kthlee | 0:cd3cae45447e | 53 | wait(0.5); |
kthlee | 0:cd3cae45447e | 54 | if (starttime == 0) break; |
kthlee | 0:cd3cae45447e | 55 | } |
kthlee | 0:cd3cae45447e | 56 | |
kthlee | 0:cd3cae45447e | 57 | uLCD.cls(); |
kthlee | 0:cd3cae45447e | 58 | while (1) { |
kthlee | 0:cd3cae45447e | 59 | srand(time(NULL)); |
kthlee | 0:cd3cae45447e | 60 | uLCD.circle(hole_x, hole_y, 15, BLUE); |
kthlee | 0:cd3cae45447e | 61 | uLCD.filled_circle(ball_x, ball_y, 9, BLACK); |
kthlee | 0:cd3cae45447e | 62 | imu.readAccel(); |
kthlee | 0:cd3cae45447e | 63 | ball_x = -imu.calcAccel(imu.ax) * 64 + 64; |
kthlee | 0:cd3cae45447e | 64 | ball_y = -imu.calcAccel(imu.ay) * 64 + 64; |
kthlee | 0:cd3cae45447e | 65 | uLCD.filled_circle(ball_x, ball_y, 9, RED); |
kthlee | 0:cd3cae45447e | 66 | uLCD.locate(0,0); |
kthlee | 0:cd3cae45447e | 67 | uLCD.printf("Score:%d ", score); |
kthlee | 0:cd3cae45447e | 68 | uLCD.locate(9,0); |
kthlee | 0:cd3cae45447e | 69 | uLCD.printf("Time:%d ", timer); |
kthlee | 0:cd3cae45447e | 70 | timer--; |
kthlee | 0:cd3cae45447e | 71 | |
kthlee | 0:cd3cae45447e | 72 | if (ball_x > hole_x - 6 && ball_x < hole_x + 6 && ball_y > hole_y - 6 && ball_y < hole_y + 6) { |
kthlee | 0:cd3cae45447e | 73 | score++; |
kthlee | 0:cd3cae45447e | 74 | uLCD.circle(hole_x, hole_y, 15, BLACK); |
kthlee | 0:cd3cae45447e | 75 | hole_x = rand() % 98 + 1; |
kthlee | 0:cd3cae45447e | 76 | hole_y = rand() % 98 + 1; |
kthlee | 0:cd3cae45447e | 77 | } |
kthlee | 0:cd3cae45447e | 78 | |
kthlee | 1:2acd36169ca1 | 79 | if (score == 10) { |
kthlee | 0:cd3cae45447e | 80 | win = true; |
kthlee | 0:cd3cae45447e | 81 | break; |
kthlee | 0:cd3cae45447e | 82 | } |
kthlee | 0:cd3cae45447e | 83 | |
kthlee | 0:cd3cae45447e | 84 | if (timer <= 0) { |
kthlee | 0:cd3cae45447e | 85 | lose = true; |
kthlee | 0:cd3cae45447e | 86 | break; |
kthlee | 0:cd3cae45447e | 87 | } |
kthlee | 0:cd3cae45447e | 88 | |
kthlee | 0:cd3cae45447e | 89 | } |
kthlee | 0:cd3cae45447e | 90 | |
kthlee | 0:cd3cae45447e | 91 | if (win) { |
kthlee | 0:cd3cae45447e | 92 | uLCD.cls(); |
kthlee | 0:cd3cae45447e | 93 | uLCD.media_init(); |
kthlee | 0:cd3cae45447e | 94 | uLCD.set_sector_address(0x0000, 0x0235); |
kthlee | 0:cd3cae45447e | 95 | uLCD.display_video(0,0); |
kthlee | 0:cd3cae45447e | 96 | wave_file2 = fopen("/sd/wavfiles/win.wav","r"); |
kthlee | 0:cd3cae45447e | 97 | player.play(wave_file2); |
kthlee | 0:cd3cae45447e | 98 | fclose(wave_file2); |
kthlee | 0:cd3cae45447e | 99 | } |
kthlee | 0:cd3cae45447e | 100 | |
kthlee | 0:cd3cae45447e | 101 | if (lose) { |
kthlee | 0:cd3cae45447e | 102 | uLCD.cls(); |
kthlee | 0:cd3cae45447e | 103 | uLCD.media_init(); |
kthlee | 0:cd3cae45447e | 104 | uLCD.set_sector_address(0x0000, 0x01F4); |
kthlee | 0:cd3cae45447e | 105 | uLCD.display_video(0,0); |
kthlee | 0:cd3cae45447e | 106 | //PlayGIF |
kthlee | 0:cd3cae45447e | 107 | wave_file3 = fopen("/sd/wavfiles/lose.wav","r"); |
kthlee | 0:cd3cae45447e | 108 | player.play(wave_file3); |
kthlee | 0:cd3cae45447e | 109 | fclose(wave_file3); |
kthlee | 0:cd3cae45447e | 110 | //PlaySound |
kthlee | 0:cd3cae45447e | 111 | } |
kthlee | 0:cd3cae45447e | 112 | |
kthlee | 0:cd3cae45447e | 113 | } |