void pacmanClear(int cant) { int size=1024/4/cant; for (int j = 0; j < 760; j+=cant*size) { for (int i = 0; i < 1024; i+=size/4) { for (int k = 0; k < cant; ++k) drawPacman(i+size/2,j+size/2+k*size,size/2,6); sleep(100/cant); for (int k = 0; k < cant; ++k) drawPacman(i+size/2,j+size/2+k*size,size/2,12); sleep(100/cant); for (int k = 0; k < cant; ++k) drawClosePacman(i+size/2,j+size/2+k*size,size/2); sleep(100/cant); for (int k = 0; k < cant; ++k) drawPacman(i+size/2,j+size/2+k*size,size/2,12); sleep(100/cant); for (int k = 0; k < cant; ++k) drawCSquare(i,j+k*size,size+1,size+1,0); } } clear(); }
void animatePacman(int x,int y,int radius){ while(1){ drawPacman(x,y,radius,6); sleep(500); drawPacman(x,y,radius,12); sleep(500); drawClosePacman(x,y,radius); sleep(500); drawPacman(x,y,radius,12); sleep(500); } }
void draw() { drawPacman(pacman.row, pacman.col); drawString4(SCREENHEIGHT-TEXT_HEIGHT, 0, string, WHITEINDEX); for (int i = 0; i < NUMDOTS; i++) drawDot(i); }
void gameInitial() { int i; initMap(map); drawInfo(global.score, global.life); pacman.mapX = 14; pacman.mapY = 18; pacman.screenX = 14 * UNIT + UNIT / 2 + OFFSET; pacman.screenY = 18 * UNIT + UNIT / 2 + OFFSET; pacman.dir = UP; pacman.speed = 1.2; drawPacman((int)pacman.screenX, (int)pacman.screenY, pacman.dir); for (i = 0; i < 3; i++) { ghost[i].mapX = 12 + i * 2; ghost[i].mapY = 16; ghost[i].screenX = mapToscreen(ghost[i].mapX) + UNIT / 2; ghost[i].screenY = mapToscreen(16); ghost[i].dir = UP; ghost[i].burst = 0; ghost[i].speed = 0.9; drawGhost(i, (int)ghost[i].screenX, (int)ghost[i].screenY, ghost[i].dir, ghost[i].burst); } ghost[3].mapX = 14; ghost[3].mapY = 12; ghost[3].screenX = mapToscreen(ghost[3].mapX); ghost[3].screenY = mapToscreen(12) + UNIT / 2; ghost[3].dir = LEFT; ghost[3].burst = 0; ghost[3].speed = 0.4; drawGhost(3, (int)ghost[3].screenX, (int)ghost[3].screenY, ghost[3].dir, ghost[3].burst); }
void pacmanRound() { switch (pacman.dir) { case UP: if (map[pacman.mapY + 1][pacman.mapX] != 0) clearPerson(map, pacman.mapX, pacman.mapY + 1); clearPerson(map, pacman.mapX, pacman.mapY); handleUp(); break; case DOWN: if (map[pacman.mapY - 1][pacman.mapX] != 0) clearPerson(map, pacman.mapX, pacman.mapY - 1); clearPerson(map, pacman.mapX, pacman.mapY); handleDown(); break; case LEFT: if (map[pacman.mapY][pacman.mapX + 1] != 0) clearPerson(map, pacman.mapX + 1, pacman.mapY); clearPerson(map, pacman.mapX, pacman.mapY); handleLeft(); break; case RIGHT: if (map[pacman.mapY][pacman.mapX - 1] != 0) clearPerson(map, pacman.mapX - 1, pacman.mapY); clearPerson(map, pacman.mapX, pacman.mapY); handleRight(); break; } changeDir(); drawPacman((int)pacman.screenX, (int)pacman.screenY, pacman.dir); }
void animateMotion(int xtl, int ytl,int boardHeight,int boardWidth,Location *pacman,Location ghosts[],int height,int width, int lives,int state[],int score,int newScore[],int frightenLoop[],int loop){ /* This function is called after pacman is moved, and animates the movement of pacman, * and all the ghosts. While the ghosts' new positions are tabulated in moveGhosts, they * are not drawn until this function is called after pacman is moved */ int i,j,loops=2; int x=xtl+pacman->x*radius+radius/2; //first location to draw, will increment 5 times int y=ytl+pacman->y*radius+radius/2; Location delta; delta.x=(pacman->x-pacman->prevX)*radius/loops; delta.y=(pacman->y-pacman->prevY)*radius/loops; /* Currently, pacman only moves one space abruptly, he cannot smoothly move from cell to cell */ //for(j=1;j<=loops+1;j++){ //this loop allows the pacman movement to be smooth gfx_clear(); if(loop==1){i=3;} drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,i,score); drawPacman(x,y,pacman->orientation,0); //x+=delta.x; //y+=delta.y; //usleep(10000); //} for(i=blinky;i<=clyde;i++){ x=xtl+ghosts[i].x*radius+radius/2; y=ytl+ghosts[i].y*radius+radius/2; drawGhost(x,y,i,ghosts[i].orientation,state,frightenLoop[i]); } /* Check if there needs to be a score displayed */ if(newScore[0]){ gfx_color(120,120,120); gfx_text(xtl+newScore[1]*radius+radius/2,ytl+newScore[2]*radius+radius/2,num2str(newScore[0])); newScore[0]=0; newScore[1]=0; newScore[2]=0; } }
void titleScreen(int height,int width,Location ghosts[],Location *pacman,int state[]){ /*Draws the title screen */ int i=-1,x,y; gfx_clear(); gfx_color(255,255,255); gfx_text(width*.35+10,height*.8,"Programmed By: CORY JBARA"); gfx_text(width*.7,height*.8-32,"Use the arrow keys to move"); gfx_text(width*.7,height*.8,"Press any button to start"); /* Draw dots and point values */ gfx_color(255,200,0); gfx_fill_rectangle(width*.8-2,height/2-15,4,4); gfx_color(255,255,255); gfx_text(width*.8+10,height/2-9,"- 10 Points"); gfx_color(255,200,0); gfx_fill_circle(width*.8-4,height/2+15,8); gfx_color(255,255,255); gfx_text(width*.8+10,height/2+20,"- 50 Points"); /* Draws ghosts and their names */ x=width/3; y=height*.45+i*height/15; gfx_text(x,y,"CHARACTER ------------------------ NICKNAME"); for(i=0;i<4;i++){ state[i]=chase; x=width/4; y=height*.45+i*height/15; drawGhost(x,y,i,right,state,0); x=width/3; switch(i){ case blinky: gfx_color(255,0,0); gfx_text(x,y,"SHADOW -------------------------- \"BLINKY\""); break; case pinky: gfx_color(255,150,200); gfx_text(x,y,"SPEEDY -------------------------- \"PINKY\""); break; case inky: gfx_color(0,255,255); gfx_text(x,y,"BASHFUL -------------------------- \"INKY\""); break; case clyde: gfx_color(255,130,0); gfx_text(x,y,"POKEY -------------------------- \"CLYDE\""); break; } } /* Pacman being chased */ i++; y=height*.72; for(i=0;i<=3;i++){ x=width*.4+i*width/20; drawGhost(x,y,i,left,state,0); } drawPacman(width*.4-width/20,y,left,1); /* P */ gfx_color(255,255,0); gfx_fill_rectangle(10,height/5-50,width/15,150); gfx_fill_circle(10+width/15,height/5,50); gfx_color(0,0,0); gfx_fill_circle(10+width/15,height/5,10); /* A */ gfx_color(255,255,0); int loop=0; int x1=width*.1; int x2=width*.27; int center=(x1+x2)/2; y=height/5+100-1; while(x1<x2){//down gfx_line(x1,y,x2,y); if(loop%2){ x1++; x2--; } y--; loop++; } gfx_color(0,0,0); gfx_fill_circle(center,height*.24,10); /* C ::::: Code copied from drawPacman*/ int rad=200; x=width*.24+rad/2; y=height/5; gfx_color(255,255,0); gfx_fill_circle(x,y,rad/2-1); gfx_color(0,0,0); int r=rad/2-1; gfx_fill_triangle(x+r,y-r,x+r,y+r,right); /* M */ gfx_color(255,255,0); x=width*.47; y=height/5-50; gfx_fill_rectangle(x,y,width*.15,150); gfx_color(0,0,0); gfx_fill_triangle(x,y,x+width*.15,y,up); /* A */ gfx_color(255,255,0); loop=0; x1=width*.63; x2=width*.17 +x1; center=(x1+x2)/2; y=height/5+100-1; while(x1<x2){//down gfx_line(x1,y,x2,y); if(loop%2){ x1++; x2--; } y--; loop++; } gfx_color(0,0,0); gfx_fill_circle(center,height*.24,10); /* N */ gfx_color(255,255,0); x=width*.81; y=height/5-50; gfx_fill_rectangle(x,y,width*.15,150); gfx_color(0,0,0); x-=2; y-=height/10; gfx_fill_triangle(x+width*.1,y,x+width*.1,y+height*.2,right); }
int main() { memcpy(board2,board,sizeof(int)*rows*columns); int height=800,width=800; //height and width of screen gfx_open(width,height,"Pacman"); gfx_clear_color(0,0,0); gfx_clear(); int boardHeight=radius*rows,boardWidth=radius*columns;//Height of the board int xtopleft=width/2-boardWidth/2; //x coord of top left corner of board int ytopleft=height/2-boardHeight*9/16; //y coord of top left corner of board char movement; int i,lives=3;//start with 3 lives int win=0; int active=0; //this changes depending on number of dots left int initialDots=dotsNumber(); int remainingDots=dotsNumber(); int score=0; int loop[4]={0,0,0,0}; int frightenLoop[4]={0}; int newScore[3]={0,0,0};//values [0]:new score to display(when ghost is killed),[1]:xvalue,[2]:yvalue Location pacman; Location ghosts[4];// enumerated to blinky, pinky, inky, clyde; /*There are four states: 0: Chase, 1: Scatter, 2: Frighten, 3: Dead, and 4: Housed */ int state[4]={scatter,scatter,scatter,scatter}; while(1){ titleScreen(height,width,ghosts,&pacman,state); score=0; lives=3; win=0; for(i=0;i<=3;i++) loop[i]=0; //reset the game resetBoard(); gfx_wait(); /* This is the gameplay loop, will repeat every time a life is lost */ while(lives>0){ gfx_clear(); /* Initialize pacman's location */ pacman.x=pacman.prevX=7; pacman.y=pacman.prevY=12; pacman.orientation=right; /* Initialize ghost's locations */ for(i=blinky;i<=clyde;i++){state[i]=scatter;} ghosts[blinky].x=7; ghosts[blinky].prevX=8; ghosts[blinky].y=ghosts[blinky].prevY=6; ghosts[blinky].orientation=left; ghosts[pinky].x=7; ghosts[pinky].prevX=7; ghosts[pinky].y=7; ghosts[pinky].prevY=8; ghosts[pinky].orientation=up; ghosts[inky].x=ghosts[inky].prevX=6; ghosts[inky].y=7; ghosts[inky].prevY=8; ghosts[inky].orientation=right; ghosts[clyde].x=ghosts[clyde].prevX=8; ghosts[clyde].y=7; ghosts[clyde].prevY=8; ghosts[clyde].orientation=left; /* These two statements draw pacman and the board to begin the program */ drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,2,score); drawPacman(xtopleft+radius*pacman.x+radius/2,ytopleft+radius*pacman.y+radius/2,pacman.orientation,0); for(i=blinky;i<=clyde;i++){ drawGhost(xtopleft+radius*ghosts[i].x+radius/2,ytopleft+radius*ghosts[i].y+radius/2,i,ghosts[i].orientation,state,frightenLoop[i]); } /* This loop is the gameplay after all initialization */ while(1){ /*if(gfx_event_waiting()){prevMovement=movement;*/ movement=gfx_wait();//} /* This block updates pacman position, checks for death * then updates ghosts' positions, then checks for death again */ movePacman(&pacman,ghosts,movement,xtopleft,ytopleft,boardHeight,boardWidth,active,state,&score,frightenLoop); if(checkDeath(&pacman,ghosts,xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,state,&score,newScore,frightenLoop,loop[0])){ //pacman's death? for(i=0;i<=3;i++) loop[i]=0; //reset the game lives--; //decrease the lives printf("LIVES: %i\n",lives); break; } targetGhosts(ghosts,&pacman,xtopleft,ytopleft,boardHeight,boardWidth,active,state); if(checkDeath(&pacman,ghosts,xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,state,&score,newScore,frightenLoop,loop[0])){ //pacman's death? for(i=0;i<=3;i++) loop[i]=0; //reset the game lives--; //decrease the lives printf("LIVES: %i\n",lives); break; } ghostState(loop,state,frightenLoop); active=activeGhosts(ghosts,loop); /* The next function animates the motion of all of the objects (pacman and ghosts */ animateMotion(xtopleft,ytopleft,boardHeight,boardWidth,&pacman,ghosts,height,width,lives,state,score,newScore,frightenLoop,loop[0]); /* The case to exit the loop is winning, when there are no dots left */ if(dotsNumber()==0){ //The player got all the dots, they won the game win=1; break; } } if(lives<=0){ gfx_clear(); printf("\n\nGAME OVER\n\n"); drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,0,score); if(tolower(gfx_wait())=='n'){ return 0; }//This will start the entire game over including the title screen else break;} else if(win){ printf("\n\nWINNER!\n\n"); drawBoard(xtopleft,ytopleft,boardHeight,boardWidth,height,width,lives,win,score); if(tolower(gfx_wait())=='n'){ return 0; }//This will end the game else break;} } } }
void deathAnimation(Location *pacman,Location ghosts[],int xtl,int ytl,int boardHeight,int boardWidth,int height,int width,int lives,int state[],int score,int newScore[],int frightenLoop[],int loop){ /* Death animation is in four states, first full circle, then pacman oriented upward, then half circle, then small sliver below, then gone, then explode */ int r=radius/2-1; int timer=1000000; int x=xtl+pacman->x*radius+radius/2; //first location to draw, will increment 5 times int y=ytl+pacman->y*radius+radius/2; animateMotion(xtl,ytl,boardHeight,boardWidth,pacman,ghosts,height,width,lives,state,score,newScore,frightenLoop,loop); gfx_wait(); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,5,score); gfx_color(255,255,0); gfx_fill_circle(x,y,radius/2-1); //gfx_wait(); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,6,score); drawPacman(x,y,up,1); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,7,score); gfx_color(255,255,0); drawPacman(x,y,0,0); gfx_color(0,0,0); gfx_fill_rectangle(x-radius/2-1,y-radius/2+1,radius,radius/2-1); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,8,score); gfx_color(255,255,0); gfx_fill_circle(x,y,radius/2-1); gfx_color(0,0,0); gfx_fill_triangle(x-r,y-r,x+r,y-r,up); gfx_fill_triangle(x+r,y-r,x+r,y+r,right); gfx_fill_triangle(x-r,y-r,x-r,y+r,left); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,9,score); gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,10,score); gfx_color(255,255,0); float angle=2*M_PI/5; //partial snowflake int i,xnew[5],ynew[5]; for(i=1;i<=5;i++){ xnew[i-1]=x+r*cos(i*angle+3*M_PI/2); ynew[i-1]=y+r*sin(i*angle+3*M_PI/2); gfx_line(x,y,xnew[i-1],ynew[i-1]); } gfx_wait(); //usleep(timer); gfx_clear(); drawBoard(xtl,ytl,boardHeight,boardWidth,height,width,lives,11,score); gfx_color(255,255,0); angle=2*M_PI/5; //snowflake with short lines for(i=1;i<=5;i++){ xnew[i-1]=x+r*cos(i*angle+3*M_PI/2); ynew[i-1]=y+r*sin(i*angle+3*M_PI/2); gfx_line((x+xnew[i-1])/2,(y+ynew[i-1])/2,xnew[i-1],ynew[i-1]); } gfx_wait(); //usleep(timer); }
void drawBoard(int x,int y,int height,int width,int h,int w,int lives,int win,int score){ /* This function draws the board in blue, one pixel wide borders * x and y are the coordinates of the top left corner of the board */ gfx_color(0,0,255); /* This loop draws the rectangles (1) in blue to prevent pacman from going over them * It also draws the dots (0) if pacman has not eaten them yet * Also draws the big dots (7) if pacman has not eaten them yet */ int i,j; for(i=0;i<rows;i++){ for(j=0;j<columns;j++){ if(board[i][j]==1){ gfx_color(0,0,255); gfx_fill_rectangle(x+radius*j,y+radius*i,radius,radius); } else if(board[i][j]==0){//draws dots in place gfx_color(255,200,0); gfx_fill_rectangle(x+radius*(j+0.5)-2,y+radius*(i+0.5)-2,4,4); } else if (board[i][j]==7){//draw big dots gfx_color(255,200,0); gfx_fill_circle(x+radius*(j+.5),y+radius*(i+.5),8); } else if(board[i][j]==8){ gfx_color(0,0,255); gfx_rectangle(x+radius*j,y+radius*i,radius,radius); }//printf("%i",board[i][j]); }//printf("\n"); } /* This block draws borders for the board */ gfx_color(0,0,255); gfx_line(x,y,x+width,y); //top border gfx_line(x,y+height,x+width,y+height); //bottom border gfx_line(x,y,x,y+radius*7); //left top border (above gap) gfx_line(x,y+radius*8,x,y+height); //left bottom border (below gap) gfx_line(x+width,y,x+width,y+radius*7); //right top border (above gap) gfx_line(x+width,y+radius*8,x+width,y+height); //right bottom border (below gap) /* This block draws the score and number of lives left */ gfx_color(255,255,255); gfx_text((w-width)/4-radius/2,h*1/2-2*radius/3,"LIVES:"); gfx_text((w-width)/4-radius/2,h*.3-16,"SCORE:"); gfx_text((w-width)/4-radius/2,h*.3,num2str(score)); for(i=0;i<lives;i++){ drawPacman((w-width)/4,h*1/2+radius*i,right,1); } if(lives<=0){ gfx_color(255,255,255); gfx_text(w/2-27,h/2-8,"GAME OVER"); gfx_text(w/2-48,h/2+8,"Play Again? (y/n)"); } else if(win==1){ gfx_color(255,255,255); gfx_text(w/2-18,h/2-8,"WINNER!"); gfx_text(w/2-48,h/2+8,"Play Again? (y/n)"); } else if(win==2){ gfx_color(255,255,255); gfx_text(w/2-18,h/2-8,"READY?"); } else if(win==3){ gfx_color(255,255,255); gfx_text(w/2-8,h/2-8,"GO!"); } else if(win==5){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 7 times"); } else if(win==6){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 6 times"); } else if(win==7){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 5 times"); } else if(win==8){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 4 times"); } else if(win==9){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 3 times"); } else if(win==10){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-38,h/2+8,"Click 2 times"); } else if(win==11){ gfx_color(255,255,255); gfx_text(w/2-23,h/2-8,"YOU DIED"); gfx_text(w/2-36,h/2+8,"Click 1 time"); } }
/* Function: levelUpDisplay * Parameters: none * Display function used when the player is has completed a level. * Draws the board and pacman at the current position, and uses a * static rotation and scale varibale to spin the board around the * origin and scale it down off the screen, while scaling pacman * to the inverse of the scale variable to make him fly upward and * through the screen. Continuously redisplays. */ void levelUpDisplay() { static GLfloat scale = 1.0; static int angle = 0; static int count = 0; /* Stop movement */ gameStarted = 0; glutSpecialFunc(NULL); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Position camera */ gluLookAt(camera[0], camera[1], camera[2], boardWidth / 2.0, boardWidth / 2.0, 1.0, 0.0, 1.0, 0.0); /* Draw pacman */ glPushMatrix(); glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), pacman.size / 2.0); glScalef(1/scale, 1/scale, 1/scale); glColor3fv(pacman.colorf); drawPacman(); glPopMatrix(); /* Draw board */ glPushMatrix(); glRotatef(angle, 0.0, 0.0, -1.0); glScalef(scale, scale, scale); glCallList(walls); glPopMatrix(); angle = (angle + 10) % 360; scale -= 0.025; count++; glFlush(); glutSwapBuffers(); if (count == 40) { /* Reset count, angle and scale */ count = 0; scale = 1.0; angle = 0; /* Load next level */ level = (level + 1) % numLevels; loadLevel(levels[level]); /* Speed up players */ ghostDefaultSpeed -= 5; pacmanDefaultSpeed -= 3; /* Reduce power-up time */ if (powerupTime > 3) powerupTime--; /* Register callbacks */ glutDisplayFunc(gameDisplay); glutSpecialFunc(specialKeyInput); flashReady = 5; glutTimerFunc(500, disableFlashReady, 0); } glutPostRedisplay(); }
/* Function: killPacmanDisplay * Parameters: none * Display function used when the player is killed by a ghost. * Draws pacman at the current position and redisplays with a * static scale variable to shrink pacman off the screen. When * this function is done drawing the death sequence, it resets * the player positions (pacman & ghosts) and registers the * next display function (gameDisplay if the player has * remaining lives or gameOverDisplay if the player has run out * of remaining lives). Continually redisplays. */ void killPacmanDisplay() { static GLfloat scale = 1.0; static int count = 0; /* Stop movement */ pacman.direction = -1; pacman.nextDirection = -1; gameStarted = 0; glutSpecialFunc(NULL); /* If tilt is enabled, return angle to starting position */ if (tiltBoard) { theta[0] = -6.0; theta[1] = 0.0; theta[2] = 0.0; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Position camera */ gluLookAt(camera[0], camera[1], camera[2], boardWidth / 2.0, boardWidth / 2.0, 1.0, 0.0, 1.0, 0.0); /* Draw pacman */ glPushMatrix(); glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), pacman.size / 2.0); glColor3fv(pacman.colorf); glScalef(scale, scale, scale); drawPacman(); glPopMatrix(); glFlush(); glutSwapBuffers(); if (count == 150) { /* Reset count, scale */ count = 0; scale = 1.0; /* No more players? */ if (playersLeft == 0) { glutDisplayFunc(gameOverDisplay); glutKeyboardFunc(NULL); } else { /* Reposition players */ playerSetup(1); /* Disable point flashing */ flashPoints = 0; /* Register callbacks */ glutSpecialFunc(specialKeyInput); glutDisplayFunc(gameDisplay); flashReady = 5; glutTimerFunc(500, disableFlashReady, 1); } } else { if (scale > 0) scale -= 0.012; count++; } glutPostRedisplay(); }
/* Function: instrDisplay * Parameters: none * Display function used when the player is at the instructions * screen. Prints a lot of text and rotating player models. Sets * up right click menu with "Play", "Title Screen" and "Quit" * options. Continuously redisplays. */ void instrDisplay() { static int angle = 0; int i; GLUquadricObj *sphere = gluNewQuadric(); /* Build Menu */ glutCreateMenu(menu); glutAddMenuEntry("Play!", 1); glutAddMenuEntry("Title Screen", 3); glutAddMenuEntry("Quit", 99); glutAttachMenu(GLUT_RIGHT_BUTTON); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Position camera */ gluLookAt(camera[0], camera[1], camera[2], boardWidth / 2.0, boardWidth / 2.0, 1.0, 0.0, 1.0, 0.0); /* Turn off lighting, print text */ glColor3f(1.0, 1.0, 1.0); glDisable(GL_LIGHTING); displayText(9.0, 31.0, 0.0, "Instructions"); displayText(11.0, 27.5, 0.0, "PLAYERS"); displayText(2.0, 24.5, 0.0, "You:"); displayText(15.0, 24.5, 0.0, "The Enemy:"); displayText(11.0, 15.0, 0.0, "SCORING"); displayText(13.0, 13.0, 0.0, "10 pts"); displayText(13.0, 11.0, 0.0, "50 pts"); displayText(10.0, 8.5, 0.0, "1st: 200 pts"); displayText(10.0, 7.0, 0.0, "2nd: 400 pts"); displayText(10.0, 5.5, 0.0, "3rd: 800 pts"); displayText(10.0, 4.0, 0.0, "4th: 1600 pts"); displayText(4.0, 1.0, 0.0, "Extra player every 20000 pts"); displayText(6.0, -1.0, 0.0, "Use arrow keys to move"); glEnable(GL_LIGHTING); /* You: pacman */ glPushMatrix(); glTranslatef(7.0, 25.0, 0.0); glRotatef(angle, 0.0, 1.0, 0.0); glScalef(2.0, 2.0, 2.0); drawPacman(); glPopMatrix(); /* Enemy: ghosts */ glPushMatrix(); glTranslatef(25.0, 24.5, 0.0); glScalef(2.0, 2.0, 2.0); for (i = 0; i < 4; i++) { glPushMatrix(); glRotatef(angle, 0.0, 1.0, 0.0); glRotatef(-90, 1.0, 0.0, 0.0); drawGhost(i); glPopMatrix(); glTranslatef(0.0, -1.0, 0.0); } glPopMatrix(); /* Scoring: small pellet */ glPushMatrix(); glTranslatef(11.0, 13.5, 0.0); glRotatef(angle, 0.0, 1.0, 0.0); glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0)); gluSphere(sphere, 0.25, 10, 10); glPopMatrix(); /* Scoring: large pellet */ glPushMatrix(); glTranslatef(11.0, 11.5, 0.0); glRotatef(angle, 0.0, 1.0, 0.0); glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0)); gluSphere(sphere, 0.5, 10, 10); glPopMatrix(); /* Scoring: blue ghosts */ glPushMatrix(); glTranslatef(8.0, 8.0, 0.0); glScalef(2.0, 2.0, 2.0); glPushMatrix(); glRotatef(angle, 0.0, 1.0, 0.0); glRotatef(-90, 1.0, 0.0, 0.0); ghosts[0].colorf[0] = 0.0; ghosts[0].colorf[1] = 0.0; ghosts[0].colorf[2] = 1.0; drawGhost(0); glPopMatrix(); glTranslatef(0.0, -1.0, 0.0); colorGhosts(); glPopMatrix(); angle = (angle + 2) % 360; /* rotation angle */ gluDeleteQuadric(sphere); glFlush(); glutSwapBuffers(); glutPostRedisplay(); }
/* Function: gameDisplay * Parameters: none * Display function used when the player is playing the game. * Draws score, top score, the board, pacman and ghosts. Sets * up right click menu with "Pause", "Toggle Tilt", "End Game" * and "Quit" options. If the gameStarted flag is set, re-registers * timer functions for movement. */ void gameDisplay() { int x, y, i; int digits = 1; char scoreStr[6]; GLUquadricObj *sphere = gluNewQuadric(); /* Build Menu */ glutCreateMenu(menu); if (gameStarted) glutAddMenuEntry("Pause", 5); else glutAddMenuEntry("Un-pause", 5); glutAddMenuEntry("Toggle Tilt", 10); glutAddMenuEntry("End Game", 3); glutAddMenuEntry("Quit", 99); glutAttachMenu(GLUT_RIGHT_BUTTON); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); /* Position camera */ gluLookAt(camera[0], camera[1], camera[2], boardWidth / 2.0, boardWidth / 2.0, 1.0, 0.0, 1.0, 0.0); /* Translate center of board to origin for rotation */ glTranslatef(boardWidth / 2.0, boardHeight / 2.0, 0.0); glRotatef(theta[0], 1.0, 0.0, 0.0); glRotatef(theta[1], 0.0, 1.0, 0.0); glRotatef(theta[2], 0.0, 0.0, 1.0); glTranslatef(-(boardWidth / 2.0), -(boardHeight / 2.0), 0.0); /* Draw the floor */ glBegin(GL_QUADS); glColor3f(0.5, 0.2, 0.2); glVertex2f(0.0, 0.0); glVertex2f(boardWidth, 0.0); glVertex2f(boardWidth, boardHeight); glVertex2f(0.0, boardHeight); glEnd(); /* Draw the walls */ glCallList(walls); /* Draw everything else */ for (y = 0; y < boardHeight; y++) { for (x = 0; x < boardWidth; x++) { /* Pellet */ if (board[y][x] == '.') { glDisable(GL_LIGHTING); glBegin(GL_POINTS); glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0)); glVertex3f((GLfloat)x + 0.5, boardHeight - (GLfloat)y - 0.5, 0.5); glEnd(); glEnable(GL_LIGHTING); } /* Super-pellet */ if (board[y][x] == 'P') { glTranslatef(x + 0.5, ((boardHeight - y) - 0.5), 0.5); glColor3f((252.0 / 255.0), (182.0 / 255.0), (148.0 / 255.0)); gluSphere(sphere, 0.3, 12, 12); glTranslatef(-(x + 0.5), -((boardHeight - y) - 0.5), -0.5); } /* Ghost door */ if (board[y][x] == 'G') { glBegin(GL_QUADS); glColor3f((252.0 / 255.0), (182.0 / 255.0), (220.0 / 255.0)); glVertex3f((GLfloat)x, boardHeight - (GLfloat)y - 0.5, 0.0); glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y - 0.5, 0.0); glVertex3f((GLfloat)x + 1.0, boardHeight - (GLfloat)y - 0.5, wallHeight); glVertex3f((GLfloat)x, boardHeight - (GLfloat)y - 0.5, wallHeight); glEnd(); } } } /* Draw pacman */ glPushMatrix(); glTranslatef(pacman.x + (pacman.size / 2.0), pacman.y - (pacman.size / 2.0), (pacman.size / 2.0)); drawPacman(); glPopMatrix(); /* Draw the ghosts */ for (i = 0; i < 4; i++) { glPushMatrix(); glTranslatef(ghosts[i].x + (ghosts[i].size / 2.0), ghosts[i].y - (ghosts[i].size / 2.0), 0.0); drawGhost(i); glPopMatrix(); } /* Draw players left */ glPushMatrix(); glTranslatef(boardWidth - 1.0, boardHeight + 2.0, 0.5); for (i = 0; i < playersLeft; i++) { glColor3fv(pacman.colorf); gluSphere(sphere, (pacman.size / 2.0) - 0.1, 10, 10); glTranslatef(-1.0, 0.0, 0.0); } glPopMatrix(); /* Flash points if Pacman ate a ghost */ if (flashPoints) { glDisable(GL_LIGHTING); glColor3f(1.0, 1.0, 1.0); displayText((GLfloat)flashPointsX, (GLfloat)flashPointsY, 2.5, itoa(flashPointsScore, scoreStr, 10)); glEnable(GL_LIGHTING); } /* Flash READY! if just starting */ if (flashReady > 0) { if (flashReady == 1 || flashReady == 3 || flashReady == 5) { glDisable(GL_LIGHTING); glColor3f(1.0, 1.0, 1.0); displayText(((GLfloat)boardWidth / 2.0) - 2.0, (GLfloat)boardHeight / 2.0, 2.0, "READY!"); glEnable(GL_LIGHTING); } } /* Print score */ glDisable(GL_LIGHTING); glColor3f(1.0, 1.0, 1.0); displayText(0.5, (GLfloat)boardHeight + 3.0, 1.0, "SCORE"); displayText(0.5, (GLfloat)boardHeight + 1.5, 1.0, itoa(score, scoreStr,10)); displayText(10.0, (GLfloat)boardHeight + 3.0, 1.0, "TOP SCORE"); displayText(11.0, (GLfloat)boardHeight + 1.5, 1.0, itoa(topScore, scoreStr, 10)); glEnable(GL_LIGHTING); gluDeleteQuadric(sphere); glFlush(); glutSwapBuffers(); }