Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE mbed wave_player
Fork of PacMan_Skeleton_unlock by
main.cpp
00001 /* Gatech ECE2035 2015 SPRING PAC MAN 00002 * Copyright (c) 2015 Gatech ECE2035 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy 00005 * of this software and associated documentation files (the "Software"), to deal 00006 * in the Software without restriction, including without limitation the rights 00007 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00008 * copies of the Software, and to permit persons to whom the Software is 00009 * furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included in 00012 * all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00015 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00016 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00017 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00018 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00019 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 00020 * SOFTWARE. 00021 */ 00022 /** @file main.cpp */ 00023 // Include header files for platform 00024 #include "mbed.h" 00025 #include "wave_player.h" 00026 #include "SDFileSystem.h" 00027 00028 // Include header files for pacman project 00029 #include "globals.h" 00030 #include "map_public.h " 00031 #include "pacman.h " 00032 #include "ghost.h " 00033 #include "MMA8452.h" 00034 #include "doubly_linked_list.h " 00035 00036 // Platform initialization 00037 DigitalIn left_pb(p21); // push bottem 00038 DigitalIn right_pb(p22); // push bottem 00039 DigitalIn up_pb(p23); // push bottem 00040 DigitalIn down_pb(p24); // push bottem 00041 DigitalOut myled(LED1); // LED 00042 uLCD_4DGL uLCD(p9,p10,p11); // LCD (serial tx, serial rx, reset pin;) 00043 Serial pc(USBTX,USBRX); // used by Accelerometer 00044 MMA8452 acc(p28, p27, 100000); // Accelerometer 00045 AnalogOut DACout(p18); // speaker 00046 wave_player waver(&DACout); // wav player 00047 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card and filesystem (mosi, miso, sck, cs) 00048 00049 // Example of the decleration of your implementation 00050 void playSound(char * wav); 00051 00052 00053 /** Main() is where you start your implementation 00054 @brief The hints of implementation are in the comments. <br> 00055 @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. 00056 */ 00057 int main() 00058 { 00059 // Initialize the timer 00060 /// [Example of time control implementation] 00061 /// Here is a rough example to implement the timer control <br><br> 00062 int tick, pre_tick; 00063 srand (time(NULL)); 00064 Timer timer; 00065 timer.start(); 00066 tick = timer.read_ms(); 00067 pre_tick = tick; 00068 int lives = 3, level = 0, invuln = 0, diff = 0; 00069 // Initialize the buttons 00070 left_pb.mode(PullUp); // The variable left_pb will be zero when the pushbutton for moving the player left is pressed 00071 right_pb.mode(PullUp); // The variable rightt_pb will be zero when the pushbutton for moving the player right is pressed 00072 up_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed 00073 down_pb.mode(PullUp); //the variable fire_pb will be zero when the pushbutton for firing a missile is pressed 00074 00075 uLCD.cls(); 00076 uLCD.locate(0,0); 00077 uLCD.printf("SELECT GAME MODE"); 00078 uLCD.locate(0,4); 00079 uLCD.printf("1) EASY"); 00080 uLCD.locate(0,8); 00081 uLCD.printf("2) MEDIUM"); 00082 uLCD.locate(0,12); 00083 uLCD.printf("3) HARD"); 00084 while(1){ 00085 if(down_pb == 0){ 00086 diff = 1; 00087 break; 00088 } 00089 if(up_pb == 0){ 00090 diff = 2; 00091 break; 00092 } 00093 if(right_pb == 0){ 00094 diff = 3; 00095 break; 00096 } 00097 } 00098 while(1) 00099 { 00100 /// [Example of the game control implementation] 00101 /// Here is the example to initialize the game <br><br> 00102 uLCD.cls(); 00103 map_init(); 00104 pacman_init(8,9); // Center of the map 00105 00106 00107 //Your code here 00108 //Initiate & create & show the ghosts 00109 ghost_init(); 00110 ghost_create(8,7,0xFF0000); 00111 if(diff > 0) 00112 ghost_create(7,5,0x00FF00); 00113 if(diff > 1) 00114 ghost_create(9,5,0xFFFF00); 00115 if(diff > 2) 00116 ghost_create(8,5,0x00FFFF); 00117 DLinkedList* ghostL = get_ghost_list(); 00118 ghost_show(ghostL); 00119 00120 //[Demo of play sound file] 00121 //playSound("/sd/wavfiles/BUZZER.wav"); 00122 00123 /// 1. Begin the game loop 00124 while(1){ 00125 tick = timer.read_ms(); // Read current time 00126 00127 /// 2. Implement the code to get user input and update the Pacman 00128 /// -[Hint] Implement the code to move Pacman. You could use either push-button or accelerometer. <br> 00129 /// The accelerometer's function readXYZGravity() might be useful. 00130 if(left_pb == 0 && right_pb == 0){ 00131 lives++; 00132 level++; 00133 break; 00134 } 00135 00136 if(up_pb == 0 && down_pb == 0){ 00137 lives = 0; 00138 } 00139 00140 double x,y,z; 00141 acc.readXYZGravity(&x,&y,&z); 00142 if(x > .4 || left_pb == 0) 00143 pacman_set_action(PACMAN_HEADING_LEFT); 00144 if(x < -.4 || right_pb == 0) 00145 pacman_set_action(PACMAN_HEADING_RIGHT); 00146 if(y > .4 || down_pb == 0) 00147 pacman_set_action(PACMAN_HEADING_DOWN); 00148 if(y < -.4 || up_pb == 0) 00149 pacman_set_action(PACMAN_HEADING_UP); 00150 00151 //uLCD.locate(0,1); 00152 //uLCD.printf("lives:%d",lives); 00153 00154 00155 GRID grid_info = map_get_grid_status(0,0); 00156 uLCD.filled_rectangle(grid_info.x,grid_info.y-GRID_SIZE,grid_info.x+(8*GRID_SIZE),grid_info.y,BACKGROUND_COLOR); 00157 int i = 0; 00158 while(i < lives){ 00159 GRID grid_info = map_get_grid_status(i,0); 00160 int screen_x = grid_info.x + GRID_RADIUS; 00161 int screen_y = grid_info.y - GRID_RADIUS; 00162 uLCD.filled_circle(screen_x, screen_y, GRID_RADIUS, PACMAN_COLOR); 00163 uLCD.filled_rectangle(screen_x,screen_y-1,screen_x+GRID_SIZE,screen_y+1, BACKGROUND_COLOR); 00164 i++; 00165 } 00166 00167 00168 if((tick-pre_tick)>500){ // Time step control 00169 pre_tick = tick; 00170 00171 00172 /// 3. Update the Pacman on the screen 00173 /// -[Hint] You could update the position of Pacman here based on the input at step 2. <br> 00174 //pacman_update_position(); 00175 00176 invuln += pacman_update_position(level); 00177 00178 if (invuln > 0){ 00179 pacman_add_score(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)); 00180 invuln -= 500; 00181 myled = 1; 00182 } 00183 else{ 00184 if(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)){ 00185 lives -= ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln); 00186 int j = 0; 00187 while(j<10){ 00188 pacman_clear(); 00189 pacman_init(8,9); 00190 j++; 00191 } 00192 } 00193 myled = 0; 00194 } 00195 00196 ghost_random_walk(); 00197 00198 if (invuln > 0){ 00199 pacman_add_score(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)); 00200 } 00201 else{ 00202 if(ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln)){ 00203 lives -= ghost_collision(ghostL, pacman_get_x(), pacman_get_y(), invuln); 00204 int j = 0; 00205 while(j<10){ 00206 pacman_clear(); 00207 pacman_init(8,9); 00208 j++; 00209 } 00210 } 00211 } 00212 00213 ghost_show(ghostL); 00214 00215 } 00216 00217 /// 4. Implement the code to check the end of game. 00218 /// -[Hint] Check whether the ghost catch the Pacman. Make sure you could always detect that the ghost and Pacman meet on the screen. 00219 /// 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). 00220 /// Either at time t or t+1, you will see that the Pacman and the ghost are not on the same grid. 00221 /// However, the Pacman should be caught by ghost with this movement. 00222 if (lives == 0){ 00223 pacman_clear(); 00224 timer.stop(); 00225 uLCD.locate(9,0); 00226 uLCD.printf("GAME OVER"); 00227 00228 if(up_pb == 0 && left_pb == 0){ 00229 lives = 3; 00230 level = 0; 00231 timer.start(); 00232 pacman_reset_score(); 00233 break; 00234 } 00235 } 00236 /// -[Hint] Check whether Pacman win the game <br> 00237 if (map_remaining_cookie() == 0){ 00238 lives++; 00239 level++; 00240 break; 00241 } 00242 } 00243 } 00244 } 00245 00246 00247 00248 // Example of implementation of your functions 00249 void playSound(char * wav) 00250 { 00251 // open wav file 00252 FILE *wave_file; 00253 wave_file=fopen(wav,"r"); 00254 00255 // play wav file 00256 waver.play(wave_file); 00257 00258 // close wav file 00259 fclose(wave_file); 00260 }
Generated on Thu Aug 4 2022 06:03:35 by
1.7.2
