A note hitting game for the mbed LPC 1768. User uses a joystick to hit notes as they come down the screen in 3 different lanes.
Dependencies: 4DGL-uLCD-SE SDFileSystem mbed-rtos mbed wave_player
note.cpp@1:94b1ec213686, 2016-03-17 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
jmpin | 1:94b1ec213686 | 1 | #include "note_private.h" |
jmpin | 1:94b1ec213686 | 2 | extern Serial pc; |
jmpin | 1:94b1ec213686 | 3 | |
jmpin | 1:94b1ec213686 | 4 | NOTE note_record[MAX_NUM_NOTE]; |
jmpin | 1:94b1ec213686 | 5 | int note_tick=0; // Internal note tick, used to make the note move at the appropriate speed |
jmpin | 1:94b1ec213686 | 6 | int note_interval = 25; // The interval on which notes are created. Higher number is more frequent |
jmpin | 1:94b1ec213686 | 7 | int note_speed = 4; // The speed at which the notes travel |
jmpin | 1:94b1ec213686 | 8 | |
jmpin | 1:94b1ec213686 | 9 | // See the comments in note_public.h |
jmpin | 1:94b1ec213686 | 10 | void note_generator(void){ |
jmpin | 1:94b1ec213686 | 11 | note_tick++; |
jmpin | 1:94b1ec213686 | 12 | // only create the notes at certain ticks |
jmpin | 1:94b1ec213686 | 13 | if((note_tick % note_interval)==0 || note_tick==0){ |
jmpin | 1:94b1ec213686 | 14 | note_create(); |
jmpin | 1:94b1ec213686 | 15 | } |
jmpin | 1:94b1ec213686 | 16 | |
jmpin | 1:94b1ec213686 | 17 | // update the notes and draw them |
jmpin | 1:94b1ec213686 | 18 | note_update_position(); |
jmpin | 1:94b1ec213686 | 19 | } |
jmpin | 1:94b1ec213686 | 20 | |
jmpin | 1:94b1ec213686 | 21 | // set note speed (default speed is 4) |
jmpin | 1:94b1ec213686 | 22 | void set_note_speed(int speed){ |
jmpin | 1:94b1ec213686 | 23 | if(speed>=1 && speed<=8){ |
jmpin | 1:94b1ec213686 | 24 | note_speed = speed; |
jmpin | 1:94b1ec213686 | 25 | } |
jmpin | 1:94b1ec213686 | 26 | } |
jmpin | 1:94b1ec213686 | 27 | |
jmpin | 1:94b1ec213686 | 28 | // set note interval (default interval is 25) |
jmpin | 1:94b1ec213686 | 29 | void set_note_interval(int interval){ |
jmpin | 1:94b1ec213686 | 30 | if(interval>=1 && interval<=100){ |
jmpin | 1:94b1ec213686 | 31 | note_interval = interval; |
jmpin | 1:94b1ec213686 | 32 | } |
jmpin | 1:94b1ec213686 | 33 | } |
jmpin | 1:94b1ec213686 | 34 | |
jmpin | 1:94b1ec213686 | 35 | // See the comments in note_public.h |
jmpin | 1:94b1ec213686 | 36 | NOTE note_get_info(int index){ |
jmpin | 1:94b1ec213686 | 37 | return note_record[index]; |
jmpin | 1:94b1ec213686 | 38 | } |
jmpin | 1:94b1ec213686 | 39 | |
jmpin | 1:94b1ec213686 | 40 | // See the comments in note_public.h |
jmpin | 1:94b1ec213686 | 41 | void note_set_played(int index){ |
jmpin | 1:94b1ec213686 | 42 | note_record[index].status = NOTE_PLAYED; |
jmpin | 1:94b1ec213686 | 43 | //pc.printf("Note %i has been changed to played",index); FOR DEBUGGING PURPOSES |
jmpin | 1:94b1ec213686 | 44 | } |
jmpin | 1:94b1ec213686 | 45 | |
jmpin | 1:94b1ec213686 | 46 | // See the comments in note_public.h |
jmpin | 1:94b1ec213686 | 47 | void note_set_missed(int index){ |
jmpin | 1:94b1ec213686 | 48 | note_record[index].status = NOTE_MISSED; |
jmpin | 1:94b1ec213686 | 49 | //pc.printf("Note %i has been changed to missed",index); FOR DEBUGGING PURPOSES |
jmpin | 1:94b1ec213686 | 50 | } |
jmpin | 1:94b1ec213686 | 51 | |
jmpin | 1:94b1ec213686 | 52 | // See the comments in hote_public.h |
jmpin | 1:94b1ec213686 | 53 | void set_note_color(int index, int color){ |
jmpin | 1:94b1ec213686 | 54 | note_record[index].color = color; |
jmpin | 1:94b1ec213686 | 55 | } |
jmpin | 1:94b1ec213686 | 56 | |
jmpin | 1:94b1ec213686 | 57 | /** This function finds an empty slot of note_record, and actives it. |
jmpin | 1:94b1ec213686 | 58 | */ |
jmpin | 1:94b1ec213686 | 59 | void note_create(void){ |
jmpin | 1:94b1ec213686 | 60 | int i,laneIndex; |
jmpin | 1:94b1ec213686 | 61 | int startingPoints[3] = {22,65,108}; // The three possibilities for the X coordinate of the center of the note |
jmpin | 1:94b1ec213686 | 62 | int colors[3] = {0xFFFF00,RED,GREEN}; // The three possibilities for the color of the note |
jmpin | 1:94b1ec213686 | 63 | for(i=0;i<MAX_NUM_NOTE;i++){ |
jmpin | 1:94b1ec213686 | 64 | if(note_record[i].status == NOTE_DEACTIVE){ // Only create new notes in a space where there is a deactive note |
jmpin | 1:94b1ec213686 | 65 | laneIndex = (rand() % 3); // Randomly choose which lane to put the note in |
jmpin | 1:94b1ec213686 | 66 | note_record[i].y = 0; // Initialize the note's Y value |
jmpin | 1:94b1ec213686 | 67 | //each note has its own tick |
jmpin | 1:94b1ec213686 | 68 | note_record[i].tick = 0; |
jmpin | 1:94b1ec213686 | 69 | //set a random source for the note |
jmpin | 1:94b1ec213686 | 70 | note_record[i].source_x = startingPoints[laneIndex]; |
jmpin | 1:94b1ec213686 | 71 | note_record[i].lane = laneIndex; |
jmpin | 1:94b1ec213686 | 72 | //pc.printf("Note %i is in lane %i \n\r",i,laneIndex); FOR DEBUGGING PURPOSES |
jmpin | 1:94b1ec213686 | 73 | //set appropriate color for note |
jmpin | 1:94b1ec213686 | 74 | note_record[i].color = colors[laneIndex]; |
jmpin | 1:94b1ec213686 | 75 | //the note starts at its source |
jmpin | 1:94b1ec213686 | 76 | note_record[i].x = note_record[i].source_x; |
jmpin | 1:94b1ec213686 | 77 | //the note must be set to active |
jmpin | 1:94b1ec213686 | 78 | note_record[i].status = NOTE_ACTIVE; |
jmpin | 1:94b1ec213686 | 79 | break; |
jmpin | 1:94b1ec213686 | 80 | } |
jmpin | 1:94b1ec213686 | 81 | } |
jmpin | 1:94b1ec213686 | 82 | } |
jmpin | 1:94b1ec213686 | 83 | |
jmpin | 1:94b1ec213686 | 84 | /** This function update the position of all notes and draws them |
jmpin | 1:94b1ec213686 | 85 | */ |
jmpin | 1:94b1ec213686 | 86 | void note_update_position(void){ |
jmpin | 1:94b1ec213686 | 87 | int i; |
jmpin | 1:94b1ec213686 | 88 | double delta_y; |
jmpin | 1:94b1ec213686 | 89 | //controls how fast the note will move |
jmpin | 1:94b1ec213686 | 90 | int rate = note_speed * 25; |
jmpin | 1:94b1ec213686 | 91 | for(i=0;i<MAX_NUM_NOTE;i++){ |
jmpin | 1:94b1ec213686 | 92 | if(note_record[i].status == NOTE_ACTIVE){ |
jmpin | 1:94b1ec213686 | 93 | // update note position |
jmpin | 1:94b1ec213686 | 94 | delta_y = 200/rate; |
jmpin | 1:94b1ec213686 | 95 | note_draw(note_record[i], BLACK); // Draw over old location of note |
jmpin | 1:94b1ec213686 | 96 | note_record[i].y = (int)((delta_y*(note_record[i].tick%rate))); // Update the Y value of the note |
jmpin | 1:94b1ec213686 | 97 | //pc.printf("Note %i has a y value of %i \n\r",i,note_record[i].y); FOR DEBUGGING PURPOSES |
jmpin | 1:94b1ec213686 | 98 | note_draw(note_record[i], note_record[i].color); // Draw note in its new location |
jmpin | 1:94b1ec213686 | 99 | //update note's internal tick |
jmpin | 1:94b1ec213686 | 100 | note_record[i].tick++; |
jmpin | 1:94b1ec213686 | 101 | } |
jmpin | 1:94b1ec213686 | 102 | else if((note_record[i].status == NOTE_PLAYED) || (note_record[i].status == NOTE_MISSED)){ // If the status of the note is not active |
jmpin | 1:94b1ec213686 | 103 | // clear the note off the screen |
jmpin | 1:94b1ec213686 | 104 | note_draw(note_record[i], BLACK); |
jmpin | 1:94b1ec213686 | 105 | // we are done with this note, mark as deactive |
jmpin | 1:94b1ec213686 | 106 | note_record[i].status = NOTE_DEACTIVE; |
jmpin | 1:94b1ec213686 | 107 | //resets the note's internal tick |
jmpin | 1:94b1ec213686 | 108 | note_record[i].tick = 0; |
jmpin | 1:94b1ec213686 | 109 | //resets the note's y position |
jmpin | 1:94b1ec213686 | 110 | note_record[i].y = 128; |
jmpin | 1:94b1ec213686 | 111 | } |
jmpin | 1:94b1ec213686 | 112 | |
jmpin | 1:94b1ec213686 | 113 | } |
jmpin | 1:94b1ec213686 | 114 | } |
jmpin | 1:94b1ec213686 | 115 | |
jmpin | 1:94b1ec213686 | 116 | /** This function draws a note. |
jmpin | 1:94b1ec213686 | 117 | @param note The note to be drawn |
jmpin | 1:94b1ec213686 | 118 | @param color The color of the note |
jmpin | 1:94b1ec213686 | 119 | */ |
jmpin | 1:94b1ec213686 | 120 | void note_draw(NOTE note, int color){ |
jmpin | 1:94b1ec213686 | 121 | int current_x,current_y; |
jmpin | 1:94b1ec213686 | 122 | current_x = note.x; |
jmpin | 1:94b1ec213686 | 123 | current_y = note.y; |
jmpin | 1:94b1ec213686 | 124 | if(color != BLACK) // If we arent erasing a note |
jmpin | 1:94b1ec213686 | 125 | { |
jmpin | 1:94b1ec213686 | 126 | uLCD.filled_circle(current_x, current_y,4, WHITE); |
jmpin | 1:94b1ec213686 | 127 | uLCD.circle(current_x, current_y,10,WHITE); |
jmpin | 1:94b1ec213686 | 128 | for(int i = 5;i<10;i++) |
jmpin | 1:94b1ec213686 | 129 | { |
jmpin | 1:94b1ec213686 | 130 | uLCD.circle(current_x,current_y,i,color); |
jmpin | 1:94b1ec213686 | 131 | } |
jmpin | 1:94b1ec213686 | 132 | } |
jmpin | 1:94b1ec213686 | 133 | else |
jmpin | 1:94b1ec213686 | 134 | { |
jmpin | 1:94b1ec213686 | 135 | uLCD.filled_circle(current_x,current_y,10,BLACK); |
jmpin | 1:94b1ec213686 | 136 | |
jmpin | 1:94b1ec213686 | 137 | } |
jmpin | 1:94b1ec213686 | 138 | } |
jmpin | 1:94b1ec213686 | 139 | |
jmpin | 1:94b1ec213686 | 140 | |
jmpin | 1:94b1ec213686 | 141 |