Ransom Conant / Mbed 2 deprecated MbedPacman

Dependencies:   4DGL-uLCD-SE mbed wave_player

Fork of PacMan_Skeleton_unlock by ECE 2035 TA

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pacman.cpp Source File

pacman.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  
00023 #include "mbed.h"
00024 #include "globals.h"
00025 #include "map_public.h "
00026 #include "pacman.h "
00027 
00028 PLAYER pacman;
00029 
00030 
00031 int score, highscore;
00032 
00033 void pacman_add_score(int add){
00034     score += add;
00035 }
00036 int pacman_get_x(void){
00037     return pacman.grid_x;
00038 }
00039 
00040 int pacman_get_y(void){
00041     return pacman.grid_y;
00042 }
00043 
00044 void pacman_reset_score(void){
00045     score = 0;
00046     uLCD.locate(0,0);
00047     uLCD.printf("score:%d",score);
00048 }
00049 void pacman_init(int grid_x, int grid_y){
00050     pacman.motion = PACMAN_HEADING_RIGHT;
00051     pacman.status = PACMAN_WAIT_COMMAND;
00052     pacman.grid_x = grid_x;
00053     pacman.grid_y = grid_y;
00054     map_eat_cookie(grid_x,grid_y); //clear the cookie on the grid.
00055     pacman_draw();
00056     uLCD.locate(0,0);
00057     uLCD.printf("score:%d",score);
00058     uLCD.locate(9,1);
00059     uLCD.printf("best:%d",highscore);
00060 }
00061 
00062 void pacman_draw(void){
00063     int x = pacman.grid_x;
00064     int y = pacman.grid_y;
00065     GRID grid_info = map_get_grid_status(x,y);
00066     int screen_x = grid_info.x + GRID_RADIUS;
00067     int screen_y = grid_info.y + GRID_RADIUS;
00068     uLCD.filled_circle(screen_x, screen_y, GRID_RADIUS, PACMAN_COLOR);
00069     //draw the mouth of Pacman
00070     switch(pacman.motion){
00071         case PACMAN_HEADING_RIGHT: 
00072             uLCD.filled_rectangle(screen_x,screen_y-1,screen_x+GRID_RADIUS,screen_y+1, BACKGROUND_COLOR);
00073             break;
00074         case PACMAN_HEADING_LEFT: 
00075             uLCD.filled_rectangle(screen_x-GRID_RADIUS,screen_y-1,screen_x,screen_y+1, BACKGROUND_COLOR);
00076             break;
00077         case PACMAN_HEADING_UP: 
00078             uLCD.filled_rectangle(screen_x-1,screen_y-GRID_RADIUS,screen_x+1,screen_y, BACKGROUND_COLOR);
00079             break;
00080         case PACMAN_HEADING_DOWN: 
00081             uLCD.filled_rectangle(screen_x-1,screen_y,screen_x+1,screen_y+GRID_RADIUS, BACKGROUND_COLOR);
00082             break;
00083         default: //head right
00084             uLCD.filled_rectangle(screen_x,screen_y-1,screen_x+GRID_RADIUS,screen_y+1, BACKGROUND_COLOR);
00085             break;
00086     }
00087 }
00088 
00089 void pacman_clear(void){
00090     int x = pacman.grid_x;
00091     int y = pacman.grid_y;
00092     GRID grid_info = map_get_grid_status(x,y);
00093     uLCD.filled_rectangle(grid_info.x, grid_info.y, grid_info.x+GRID_SIZE-1, grid_info.y+GRID_SIZE-1, BACKGROUND_COLOR);
00094 }
00095 
00096 void pacman_set_action(PACMAN_MOTION motion){
00097     if(motion == PACMAN_NO_UPDATE)
00098         return;
00099         
00100     pacman.motion = motion;
00101     pacman.status = PACMAN_RUNNING;
00102 }
00103 
00104 int pacman_update_position(int level){
00105     GRID next_grid_info;
00106     int x = pacman.grid_x;
00107     int y = pacman.grid_y;
00108     int value = 0;
00109     if(pacman.status==PACMAN_RUNNING){
00110         switch(pacman.motion){
00111             case PACMAN_HEADING_UP:
00112                 if(y==0)
00113                     y = NUM_GRID_Y-1;
00114                 else
00115                     y--;
00116                 break;
00117             case PACMAN_HEADING_DOWN:
00118                 if(y==(NUM_GRID_Y-1))
00119                     y = 0;
00120                 else
00121                     y++;
00122                 break;
00123             case PACMAN_HEADING_RIGHT:
00124                 if(x==NUM_GRID_X-1)
00125                     x = 0;
00126                 else
00127                     x++;
00128                 break;
00129             case PACMAN_HEADING_LEFT:
00130                 if(x==0)
00131                     x = NUM_GRID_X-1;
00132                 else
00133                     x--;
00134                 break;
00135         }
00136         next_grid_info = map_get_grid_status(x,y);
00137         if(next_grid_info.status==GRID_WALL){
00138             pacman.status=PACMAN_WAIT_COMMAND;
00139         }
00140         else{
00141             //clear the picture of previous
00142             pacman_clear();
00143             //update pacman position in map
00144             pacman.grid_x = x;
00145             pacman.grid_y = y;
00146             pacman_draw();
00147             if(next_grid_info.status>=GRID_COOKIE){
00148                 map_eat_cookie(x,y);
00149                 score++;
00150                 if(next_grid_info.status==GRID_SUPER_COOKIE){ //one super cookie worth 5 points
00151                     score+=4;
00152                     
00153                     if(level <= 4)
00154                         value = (5000-(1000*level));
00155                     else
00156                         value = 1000;
00157                         
00158                 }
00159                 uLCD.locate(0,0);
00160                 uLCD.printf("score:%d",score);
00161                 if(score > highscore)
00162                     highscore = score;
00163                 uLCD.locate(9,1);
00164                 uLCD.printf("best:%d",highscore);
00165                
00166             }
00167         }
00168     }
00169     return value; 
00170 }