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

Committer:
jmpin
Date:
Thu Mar 17 21:12:59 2016 +0000
Revision:
1:94b1ec213686
Game is pretty much finished, minus the sound coming from .wav files from the SD card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmpin 1:94b1ec213686 1 #include "mbed.h"
jmpin 1:94b1ec213686 2 #include "uLCD_4DGL.h"
jmpin 1:94b1ec213686 3 #include "player.h"
jmpin 1:94b1ec213686 4 #include "note_public.h"
jmpin 1:94b1ec213686 5 #include <math.h>
jmpin 1:94b1ec213686 6
jmpin 1:94b1ec213686 7 extern int note_speed; // Note speed
jmpin 1:94b1ec213686 8 extern int note_interval; // Interval on which notes are created
jmpin 1:94b1ec213686 9 extern uLCD_4DGL uLCD; // LCD display
jmpin 1:94b1ec213686 10 extern Serial pc; // For debugging
jmpin 1:94b1ec213686 11
jmpin 1:94b1ec213686 12 PLAYER CURRENT_PLAYER; // Player object
jmpin 1:94b1ec213686 13 NOTE note_array[MAX_NUM_NOTE]; // Structure containing the Note objects
jmpin 1:94b1ec213686 14
jmpin 1:94b1ec213686 15 void player_init() { // Initializes the player object with default values
jmpin 1:94b1ec213686 16 CURRENT_PLAYER.score = 0;
jmpin 1:94b1ec213686 17 CURRENT_PLAYER.lives = 20;
jmpin 1:94b1ec213686 18 CURRENT_PLAYER.status = ALIVE;
jmpin 1:94b1ec213686 19 }
jmpin 1:94b1ec213686 20
jmpin 1:94b1ec213686 21 void gui_init() { // Draws the GUI on the LCD
jmpin 1:94b1ec213686 22 uLCD.locate(0,0);
jmpin 1:94b1ec213686 23 uLCD.line(43,0,43,128,GREEN); //Lines separating the lanes
jmpin 1:94b1ec213686 24 uLCD.line(86,0,86,128,GREEN); //Lines separating the lanes
jmpin 1:94b1ec213686 25 uLCD.circle(108,128-15,10,WHITE); //Left note
jmpin 1:94b1ec213686 26 uLCD.filled_circle(108,128-15,4,WHITE); //Left note
jmpin 1:94b1ec213686 27 uLCD.circle(65,128-15,10,WHITE); //Middle note
jmpin 1:94b1ec213686 28 uLCD.filled_circle(65,128-15,4,WHITE); //Middle note
jmpin 1:94b1ec213686 29 uLCD.circle(22,128-15,10,WHITE); //Right note
jmpin 1:94b1ec213686 30 uLCD.filled_circle(22,128-15,4,WHITE); //Right note
jmpin 1:94b1ec213686 31 for(int i = 5;i<10;i++) //Loop used to fill in the notes with appropriate color
jmpin 1:94b1ec213686 32 {
jmpin 1:94b1ec213686 33 uLCD.circle(108,128-15,i,GREEN);
jmpin 1:94b1ec213686 34 uLCD.circle(65,128-15,i,RED);
jmpin 1:94b1ec213686 35 uLCD.circle(22,128-15,i,0xFFFF00);
jmpin 1:94b1ec213686 36 }
jmpin 1:94b1ec213686 37 }
jmpin 1:94b1ec213686 38
jmpin 1:94b1ec213686 39 double get_player_lives() { // Gets number of lives player has remaining
jmpin 1:94b1ec213686 40
jmpin 1:94b1ec213686 41 return(CURRENT_PLAYER.lives);
jmpin 1:94b1ec213686 42 }
jmpin 1:94b1ec213686 43
jmpin 1:94b1ec213686 44 int get_player_score() { // Gets score of player
jmpin 1:94b1ec213686 45 pc.printf("Player's Score: %i\n\r",CURRENT_PLAYER.score);
jmpin 1:94b1ec213686 46 return(CURRENT_PLAYER.score);
jmpin 1:94b1ec213686 47 }
jmpin 1:94b1ec213686 48
jmpin 1:94b1ec213686 49 void update_values(){ // Updates the structure containing the Note object data
jmpin 1:94b1ec213686 50 for(int index=0;index<MAX_NUM_NOTE;index++)
jmpin 1:94b1ec213686 51 {
jmpin 1:94b1ec213686 52 note_array[index]=note_get_info(index);
jmpin 1:94b1ec213686 53 //pc.printf("The y value of note %i is %d\n\r",index,note_array[index].y); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 54 }
jmpin 1:94b1ec213686 55 }
jmpin 1:94b1ec213686 56
jmpin 1:94b1ec213686 57 bool detect_lose_conditions(){ // Detects when the game is over by checking the player's lives
jmpin 1:94b1ec213686 58 bool game_over = false;
jmpin 1:94b1ec213686 59 if(CURRENT_PLAYER.lives <= 0){
jmpin 1:94b1ec213686 60 game_over=true;
jmpin 1:94b1ec213686 61 }
jmpin 1:94b1ec213686 62 return(game_over);
jmpin 1:94b1ec213686 63 }
jmpin 1:94b1ec213686 64
jmpin 1:94b1ec213686 65 float get_distance(int index){ // Detects distance between notes in the lane and the note registration zone
jmpin 1:94b1ec213686 66 float distance = 0;
jmpin 1:94b1ec213686 67 int y2 = note_array[index].y;
jmpin 1:94b1ec213686 68 distance = abs(y2-(128.0-15.0)); // 113.0 is the location of the center of the registration zone in the y direction
jmpin 1:94b1ec213686 69 //pc.printf("The Y value for note %i is %i \n\r",index,y2); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 70 //pc.printf("The distance between the note and the checkpoint is: %f\n\r",distance); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 71 return (distance);
jmpin 1:94b1ec213686 72 }
jmpin 1:94b1ec213686 73
jmpin 1:94b1ec213686 74 bool detect_note_miss(){ // Detects when notes go past the checkpoint, also will check to see if player has lost all their lives
jmpin 1:94b1ec213686 75 bool gameOver = false; // Assume the player has not lost the game yet
jmpin 1:94b1ec213686 76 update_values(); // Make sure the location data for all notes is current
jmpin 1:94b1ec213686 77 for(int index=0;index<5;index++){
jmpin 1:94b1ec213686 78 if((note_array[index].y >=(128-10)) && (note_array[index].y != 128)) // If the note has passed the registration/checkpoint zone
jmpin 1:94b1ec213686 79 {
jmpin 1:94b1ec213686 80 note_set_missed(index); // Set note as missed
jmpin 1:94b1ec213686 81 note_update_position(); // Update its status
jmpin 1:94b1ec213686 82 gui_init(); // Redraw the GUI
jmpin 1:94b1ec213686 83 CURRENT_PLAYER.lives -= 2; // Lose 2 lives for a missed note
jmpin 1:94b1ec213686 84 gameOver = detect_lose_conditions(); // Determine if this has lost the player the game
jmpin 1:94b1ec213686 85 //pc.printf("Note went past registration zone!\n\r"); // FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 86 //break;
jmpin 1:94b1ec213686 87 }
jmpin 1:94b1ec213686 88 }
jmpin 1:94b1ec213686 89 return(gameOver);
jmpin 1:94b1ec213686 90 }
jmpin 1:94b1ec213686 91
jmpin 1:94b1ec213686 92 bool detect_note_hit(int lane){ // Detects when a note is hit, is called whenever a direction is input on the joystick
jmpin 1:94b1ec213686 93 bool gameOver = false; // Assume player has not lost the game yet
jmpin 1:94b1ec213686 94 update_values(); // Make sure the location data for all notes is current
jmpin 1:94b1ec213686 95 int minDistance = 128; // We only want to look at the closest note in the lane we care about
jmpin 1:94b1ec213686 96 int indexToCheck; // We only need to look at one note per function call
jmpin 1:94b1ec213686 97 for(int i=0;i<5;i++)
jmpin 1:94b1ec213686 98 {
jmpin 1:94b1ec213686 99 //pc.printf("Checking note %i, current lane: %i \n\r",i,note_array[i].lane); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 100 if(note_array[i].lane+1 == (lane+1))
jmpin 1:94b1ec213686 101 {
jmpin 1:94b1ec213686 102 if(get_distance(i) < minDistance)
jmpin 1:94b1ec213686 103 {
jmpin 1:94b1ec213686 104 minDistance = get_distance(i); // Make sure we get the note with the least distance to the checkpoint
jmpin 1:94b1ec213686 105 indexToCheck=i; // This is the note we want to check if we hit
jmpin 1:94b1ec213686 106 }
jmpin 1:94b1ec213686 107 }
jmpin 1:94b1ec213686 108 }
jmpin 1:94b1ec213686 109 //pc.printf("Found a match!"); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 110 double distance = get_distance(indexToCheck); // Check distance of selected note from checkpoint zone
jmpin 1:94b1ec213686 111 if(distance <= 4.0){ // If note is close enough, proceed
jmpin 1:94b1ec213686 112 note_set_played(indexToCheck); // Set note status to NOTE_PLAYED
jmpin 1:94b1ec213686 113 note_update_position(); // Update note position
jmpin 1:94b1ec213686 114 gui_init(); // Redraw the GUI
jmpin 1:94b1ec213686 115 CURRENT_PLAYER.lives += 1.2; // Player gets 1.2 lives back for each note hit
jmpin 1:94b1ec213686 116 CURRENT_PLAYER.score += 1.0; // Player gets 1 point for every note hit
jmpin 1:94b1ec213686 117 //pc.printf("Note hit!\n\r"); FOR DEBUGGING PURPOSES
jmpin 1:94b1ec213686 118 }
jmpin 1:94b1ec213686 119 else if(distance > 4) // If note is not close enough, proceed with miss protocol
jmpin 1:94b1ec213686 120 {
jmpin 1:94b1ec213686 121 CURRENT_PLAYER.lives -=1.0; // If a note is missed in this manner, player loses 1 life
jmpin 1:94b1ec213686 122 gameOver = detect_lose_conditions();// Check to see if this has caused the player to lose all their lives and the game
jmpin 1:94b1ec213686 123 //pc.printf("Note missed...\n\r"); DEBUGGING PURPOSES
jmpin 1:94b1ec213686 124 }
jmpin 1:94b1ec213686 125 return(gameOver);
jmpin 1:94b1ec213686 126 }
jmpin 1:94b1ec213686 127
jmpin 1:94b1ec213686 128