Ransom Conant / Mbed 2 deprecated MbedPacman

Dependencies:   4DGL-uLCD-SE mbed wave_player

Fork of PacMan_Skeleton_unlock by ECE 2035 TA

Committer:
rconant6
Date:
Thu Mar 29 05:05:51 2018 +0000
Revision:
1:fbda247b843b
Parent:
0:be33a1fad8c0
Adding files;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
conantina 0:be33a1fad8c0 1 /* Gatech ECE2035 2015 SPRING PAC MAN
conantina 0:be33a1fad8c0 2 * Copyright (c) 2015 Gatech ECE2035
conantina 0:be33a1fad8c0 3 *
conantina 0:be33a1fad8c0 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
conantina 0:be33a1fad8c0 5 * of this software and associated documentation files (the "Software"), to deal
conantina 0:be33a1fad8c0 6 * in the Software without restriction, including without limitation the rights
conantina 0:be33a1fad8c0 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
conantina 0:be33a1fad8c0 8 * copies of the Software, and to permit persons to whom the Software is
conantina 0:be33a1fad8c0 9 * furnished to do so, subject to the following conditions:
conantina 0:be33a1fad8c0 10 *
conantina 0:be33a1fad8c0 11 * The above copyright notice and this permission notice shall be included in
conantina 0:be33a1fad8c0 12 * all copies or substantial portions of the Software.
conantina 0:be33a1fad8c0 13 *
conantina 0:be33a1fad8c0 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
conantina 0:be33a1fad8c0 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
conantina 0:be33a1fad8c0 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
conantina 0:be33a1fad8c0 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
conantina 0:be33a1fad8c0 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
conantina 0:be33a1fad8c0 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
conantina 0:be33a1fad8c0 20 * SOFTWARE.
conantina 0:be33a1fad8c0 21 */
conantina 0:be33a1fad8c0 22 /** @file main.cpp */
conantina 0:be33a1fad8c0 23 // Include header files for platform
conantina 0:be33a1fad8c0 24 #include "mbed.h"
conantina 0:be33a1fad8c0 25 #include "wave_player.h"
conantina 0:be33a1fad8c0 26 #include "SDFileSystem.h"
conantina 0:be33a1fad8c0 27
conantina 0:be33a1fad8c0 28 // Include header files for pacman project
conantina 0:be33a1fad8c0 29 #include "globals.h"
conantina 0:be33a1fad8c0 30 #include "map_public.h"
conantina 0:be33a1fad8c0 31 #include "pacman.h"
conantina 0:be33a1fad8c0 32 #include "ghost.h"
conantina 0:be33a1fad8c0 33 #include "MMA8452.h"
conantina 0:be33a1fad8c0 34 #include "doubly_linked_list.h"
conantina 0:be33a1fad8c0 35
conantina 0:be33a1fad8c0 36 // Platform initialization
conantina 0:be33a1fad8c0 37 DigitalIn left_pb(p21); // push bottem
conantina 0:be33a1fad8c0 38 DigitalIn right_pb(p22); // push bottem
conantina 0:be33a1fad8c0 39 DigitalIn up_pb(p23); // push bottem
conantina 0:be33a1fad8c0 40 DigitalIn down_pb(p24); // push bottem
rconant6 1:fbda247b843b 41 DigitalOut myled(LED1); // LED
conantina 0:be33a1fad8c0 42 uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;)
conantina 0:be33a1fad8c0 43 Serial pc(USBTX,USBRX); // used by Accelerometer
conantina 0:be33a1fad8c0 44 MMA8452 acc(p28, p27, 100000); // Accelerometer
conantina 0:be33a1fad8c0 45 AnalogOut DACout(p18); // speaker
conantina 0:be33a1fad8c0 46 wave_player waver(&DACout); // wav player
conantina 0:be33a1fad8c0 47 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs)
conantina 0:be33a1fad8c0 48
conantina 0:be33a1fad8c0 49 // Example of the decleration of your implementation
conantina 0:be33a1fad8c0 50 void playSound(char * wav);
conantina 0:be33a1fad8c0 51
conantina 0:be33a1fad8c0 52
conantina 0:be33a1fad8c0 53 /** Main() is where you start your implementation
conantina 0:be33a1fad8c0 54 @brief The hints of implementation are in the comments. <br>
conantina 0:be33a1fad8c0 55 @brief You are expected to implement your code in main.cpp and pacman.cpp. But you could modify any code if you want to make the game work better.
conantina 0:be33a1fad8c0 56 */
conantina 0:be33a1fad8c0 57 int main()
conantina 0:be33a1fad8c0 58 {
conantina 0:be33a1fad8c0 59 // Initialize the timer
conantina 0:be33a1fad8c0 60 /// [Example of time control implementation]
conantina 0:be33a1fad8c0 61 /// Here is a rough example to implement the timer control <br><br>
conantina 0:be33a1fad8c0 62 int tick, pre_tick;
conantina 0:be33a1fad8c0 63 srand (time(NULL));
conantina 0:be33a1fad8c0 64 Timer timer;
conantina 0:be33a1fad8c0 65 timer.start();
conantina 0:be33a1fad8c0 66 tick = timer.read_ms();
conantina 0:be33a1fad8c0 67 pre_tick = tick;
rconant6 1:fbda247b843b 68 int lives = 3, level = 0, invuln = 0, diff = 0;
conantina 0:be33a1fad8c0 69 // Initialize the buttons
conantina 0:be33a1fad8c0 70 left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed
conantina 0:be33a1fad8c0 71 right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed
conantina 0:be33a1fad8c0 72 up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed
conantina 0:be33a1fad8c0 73 down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed
rconant6 1:fbda247b843b 74
rconant6 1:fbda247b843b 75 uLCD.cls();
rconant6 1:fbda247b843b 76 uLCD.locate(0,0);
rconant6 1:fbda247b843b 77 uLCD.printf("SELECT GAME MODE");
rconant6 1:fbda247b843b 78 uLCD.locate(0,4);
rconant6 1:fbda247b843b 79 uLCD.printf("1) EASY");
rconant6 1:fbda247b843b 80 uLCD.locate(0,8);
rconant6 1:fbda247b843b 81 uLCD.printf("2) MEDIUM");
rconant6 1:fbda247b843b 82 uLCD.locate(0,12);
rconant6 1:fbda247b843b 83 uLCD.printf("3) HARD");
rconant6 1:fbda247b843b 84 while(1){
rconant6 1:fbda247b843b 85 if(down_pb == 0){
rconant6 1:fbda247b843b 86 diff = 1;
rconant6 1:fbda247b843b 87 break;
rconant6 1:fbda247b843b 88 }
rconant6 1:fbda247b843b 89 if(up_pb == 0){
rconant6 1:fbda247b843b 90 diff = 2;
rconant6 1:fbda247b843b 91 break;
rconant6 1:fbda247b843b 92 }
rconant6 1:fbda247b843b 93 if(right_pb == 0){
rconant6 1:fbda247b843b 94 diff = 3;
rconant6 1:fbda247b843b 95 break;
rconant6 1:fbda247b843b 96 }
rconant6 1:fbda247b843b 97 }
conantina 0:be33a1fad8c0 98 while(1)
conantina 0:be33a1fad8c0 99 {
conantina 0:be33a1fad8c0 100 /// [Example of the game control implementation]
conantina 0:be33a1fad8c0 101 /// Here is the example to initialize the game <br><br>
conantina 0:be33a1fad8c0 102 uLCD.cls();
conantina 0:be33a1fad8c0 103 map_init();
conantina 0:be33a1fad8c0 104 pacman_init(8,9); // Center of the map
rconant6 1:fbda247b843b 105
conantina 0:be33a1fad8c0 106
conantina 0:be33a1fad8c0 107 //Your code here
conantina 0:be33a1fad8c0 108 //Initiate & create & show the ghosts
rconant6 1:fbda247b843b 109 ghost_init();
rconant6 1:fbda247b843b 110 ghost_create(8,7,0xFF0000);
rconant6 1:fbda247b843b 111 if(diff > 0)
rconant6 1:fbda247b843b 112 ghost_create(7,5,0x00FF00);
rconant6 1:fbda247b843b 113 if(diff > 1)
rconant6 1:fbda247b843b 114 ghost_create(9,5,0xFFFF00);
rconant6 1:fbda247b843b 115 if(diff > 2)
rconant6 1:fbda247b843b 116 ghost_create(8,5,0x00FFFF);
rconant6 1:fbda247b843b 117 DLinkedList* ghostL = get_ghost_list();
rconant6 1:fbda247b843b 118 ghost_show(ghostL);
conantina 0:be33a1fad8c0 119
conantina 0:be33a1fad8c0 120 //[Demo of play sound file]
conantina 0:be33a1fad8c0 121 //playSound("/sd/wavfiles/BUZZER.wav");
conantina 0:be33a1fad8c0 122
conantina 0:be33a1fad8c0 123 /// 1. Begin the game loop
conantina 0:be33a1fad8c0 124 while(1){
conantina 0:be33a1fad8c0 125 tick = timer.read_ms(); // Read current time
conantina 0:be33a1fad8c0 126
conantina 0:be33a1fad8c0 127 /// 2. Implement the code to get user input and update the Pacman
conantina 0:be33a1fad8c0 128 /// -[Hint] Implement the code to move Pacman. You could use either push-button or accelerometer. <br>
conantina 0:be33a1fad8c0 129 /// The accelerometer's function readXYZGravity() might be useful.
rconant6 1:fbda247b843b 130 if(left_pb == 0 && right_pb == 0){
rconant6 1:fbda247b843b 131 lives++;
rconant6 1:fbda247b843b 132 level++;
rconant6 1:fbda247b843b 133 break;
rconant6 1:fbda247b843b 134 }
rconant6 1:fbda247b843b 135
rconant6 1:fbda247b843b 136 if(up_pb == 0 && down_pb == 0){
rconant6 1:fbda247b843b 137 lives = 0;
rconant6 1:fbda247b843b 138 }
rconant6 1:fbda247b843b 139
rconant6 1:fbda247b843b 140 double x,y,z;
rconant6 1:fbda247b843b 141 acc.readXYZGravity(&x,&y,&z);
rconant6 1:fbda247b843b 142 if(x > .4 || left_pb == 0)
rconant6 1:fbda247b843b 143 pacman_set_action(PACMAN_HEADING_LEFT);
rconant6 1:fbda247b843b 144 if(x < -.4 || right_pb == 0)
rconant6 1:fbda247b843b 145 pacman_set_action(PACMAN_HEADING_RIGHT);
rconant6 1:fbda247b843b 146 if(y > .4 || down_pb == 0)
rconant6 1:fbda247b843b 147 pacman_set_action(PACMAN_HEADING_DOWN);
rconant6 1:fbda247b843b 148 if(y < -.4 || up_pb == 0)
rconant6 1:fbda247b843b 149 pacman_set_action(PACMAN_HEADING_UP);
rconant6 1:fbda247b843b 150
rconant6 1:fbda247b843b 151 //uLCD.locate(0,1);
rconant6 1:fbda247b843b 152 //uLCD.printf("lives:%d",lives);
rconant6 1:fbda247b843b 153
rconant6 1:fbda247b843b 154
rconant6 1:fbda247b843b 155 GRID grid_info = map_get_grid_status(0,0);
rconant6 1:fbda247b843b 156 uLCD.filled_rectangle(grid_info.x,grid_info.y-GRID_SIZE,grid_info.x+(8*GRID_SIZE),grid_info.y,BACKGROUND_COLOR);
rconant6 1:fbda247b843b 157 int i = 0;
rconant6 1:fbda247b843b 158 while(i < lives){
rconant6 1:fbda247b843b 159 GRID grid_info = map_get_grid_status(i,0);
rconant6 1:fbda247b843b 160 int screen_x = grid_info.x + GRID_RADIUS;
rconant6 1:fbda247b843b 161 int screen_y = grid_info.y - GRID_RADIUS;
rconant6 1:fbda247b843b 162 uLCD.filled_circle(screen_x, screen_y, GRID_RADIUS, PACMAN_COLOR);
rconant6 1:fbda247b843b 163 uLCD.filled_rectangle(screen_x,screen_y-1,screen_x+GRID_SIZE,screen_y+1, BACKGROUND_COLOR);
rconant6 1:fbda247b843b 164 i++;
rconant6 1:fbda247b843b 165 }
rconant6 1:fbda247b843b 166
conantina 0:be33a1fad8c0 167
conantina 0:be33a1fad8c0 168 if((tick-pre_tick)>500){ // Time step control
rconant6 1:fbda247b843b 169 pre_tick = tick;
rconant6 1:fbda247b843b 170
rconant6 1:fbda247b843b 171
conantina 0:be33a1fad8c0 172 /// 3. Update the Pacman on the screen
conantina 0:be33a1fad8c0 173 /// -[Hint] You could update the position of Pacman here based on the input at step 2. <br>
rconant6 1:fbda247b843b 174 //pacman_update_position();
rconant6 1:fbda247b843b 175
rconant6 1:fbda247b843b 176 invuln += pacman_update_position(level);
rconant6 1:fbda247b843b 177
rconant6 1:fbda247b843b 178 if (invuln > 0){
rconant6 1:fbda247b843b 179 pacman_add_score(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln));
rconant6 1:fbda247b843b 180 invuln -= 500;
rconant6 1:fbda247b843b 181 myled = 1;
rconant6 1:fbda247b843b 182 }
rconant6 1:fbda247b843b 183 else{
rconant6 1:fbda247b843b 184 if(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)){
rconant6 1:fbda247b843b 185 lives -= ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln);
rconant6 1:fbda247b843b 186 int j = 0;
rconant6 1:fbda247b843b 187 while(j<10){
rconant6 1:fbda247b843b 188 pacman_clear();
rconant6 1:fbda247b843b 189 pacman_init(8,9);
rconant6 1:fbda247b843b 190 j++;
rconant6 1:fbda247b843b 191 }
rconant6 1:fbda247b843b 192 }
rconant6 1:fbda247b843b 193 myled = 0;
rconant6 1:fbda247b843b 194 }
rconant6 1:fbda247b843b 195
rconant6 1:fbda247b843b 196 ghost_random_walk();
rconant6 1:fbda247b843b 197
rconant6 1:fbda247b843b 198 if (invuln > 0){
rconant6 1:fbda247b843b 199 pacman_add_score(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln));
rconant6 1:fbda247b843b 200 }
rconant6 1:fbda247b843b 201 else{
rconant6 1:fbda247b843b 202 if(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)){
rconant6 1:fbda247b843b 203 lives -= ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln);
rconant6 1:fbda247b843b 204 int j = 0;
rconant6 1:fbda247b843b 205 while(j<10){
rconant6 1:fbda247b843b 206 pacman_clear();
rconant6 1:fbda247b843b 207 pacman_init(8,9);
rconant6 1:fbda247b843b 208 j++;
rconant6 1:fbda247b843b 209 }
rconant6 1:fbda247b843b 210 }
rconant6 1:fbda247b843b 211 }
rconant6 1:fbda247b843b 212
rconant6 1:fbda247b843b 213 ghost_show(ghostL);
rconant6 1:fbda247b843b 214
conantina 0:be33a1fad8c0 215 }
conantina 0:be33a1fad8c0 216
conantina 0:be33a1fad8c0 217 /// 4. Implement the code to check the end of game.
conantina 0:be33a1fad8c0 218 /// -[Hint] Check whether the ghost catch the Pacman. Make sure you could always detect that the ghost and Pacman meet on the screen.
conantina 0:be33a1fad8c0 219 /// One tricky scenario is that: Pacman is at grid (3,3) and is heading to (3,4), while the ghost is at grid (3,4) and is heading to (3,3).
conantina 0:be33a1fad8c0 220 /// Either at time t or t+1, you will see that the Pacman and the ghost are not on the same grid.
conantina 0:be33a1fad8c0 221 /// However, the Pacman should be caught by ghost with this movement.
rconant6 1:fbda247b843b 222 if (lives == 0){
rconant6 1:fbda247b843b 223 pacman_clear();
rconant6 1:fbda247b843b 224 timer.stop();
rconant6 1:fbda247b843b 225 uLCD.locate(9,0);
rconant6 1:fbda247b843b 226 uLCD.printf("GAME OVER");
rconant6 1:fbda247b843b 227
rconant6 1:fbda247b843b 228 if(up_pb == 0 && left_pb == 0){
rconant6 1:fbda247b843b 229 lives = 3;
rconant6 1:fbda247b843b 230 level = 0;
rconant6 1:fbda247b843b 231 timer.start();
rconant6 1:fbda247b843b 232 pacman_reset_score();
rconant6 1:fbda247b843b 233 break;
rconant6 1:fbda247b843b 234 }
rconant6 1:fbda247b843b 235 }
conantina 0:be33a1fad8c0 236 /// -[Hint] Check whether Pacman win the game <br>
rconant6 1:fbda247b843b 237 if (map_remaining_cookie() == 0){
rconant6 1:fbda247b843b 238 lives++;
rconant6 1:fbda247b843b 239 level++;
rconant6 1:fbda247b843b 240 break;
rconant6 1:fbda247b843b 241 }
conantina 0:be33a1fad8c0 242 }
conantina 0:be33a1fad8c0 243 }
conantina 0:be33a1fad8c0 244 }
conantina 0:be33a1fad8c0 245
conantina 0:be33a1fad8c0 246
conantina 0:be33a1fad8c0 247
conantina 0:be33a1fad8c0 248 // Example of implementation of your functions
conantina 0:be33a1fad8c0 249 void playSound(char * wav)
conantina 0:be33a1fad8c0 250 {
conantina 0:be33a1fad8c0 251 // open wav file
conantina 0:be33a1fad8c0 252 FILE *wave_file;
conantina 0:be33a1fad8c0 253 wave_file=fopen(wav,"r");
conantina 0:be33a1fad8c0 254
conantina 0:be33a1fad8c0 255 // play wav file
conantina 0:be33a1fad8c0 256 waver.play(wave_file);
conantina 0:be33a1fad8c0 257
conantina 0:be33a1fad8c0 258 // close wav file
conantina 0:be33a1fad8c0 259 fclose(wave_file);
conantina 0:be33a1fad8c0 260 }