mbed Ball In the Hole Game

Dependencies:   4DGL-uLCD-SE LSM9DS1_Library SDFileSystem mbed wave_player

Committer:
kthlee
Date:
Thu Mar 17 16:31:33 2016 +0000
Revision:
1:2acd36169ca1
Parent:
0:cd3cae45447e
Child:
2:04fe2cb3b815
Winning score changed to 10 from 5;

Who changed what in which revision?

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