Un clone de pacman en OpenGL avec la librairie GLUT. Il n'y a pas encore de sons mais ca vas venir ...
Source / Exemple :
//------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------
#include <windows.h>
#include "glut.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <malloc.h>
//------------------------------------------------------------------------------
// Constantes & Variables
//------------------------------------------------------------------------------
#define WIDTH 1024
#define HEIGHT 768
#define EXIT {fclose(fichier); return -1;}
#define taille_pacman 32;
GLuint Name[200];
int CurrentW;
int CurrentH;
struct pacman_struct
{
float posX;
float posY;
int direction;
int taille;
}pacman;
struct jeu_struct
{
int ferme;
int next_direction;
int niveau_lu;
int start;
int bloque;
int chopes;
int monstres;
int points;
int niveau;
int vies;
char level[50];
int perdu;
}jeu;
struct monstres_struct
{
float posX;
float posY;
int direction;
}monstres[50];
struct menu_struct
{
float color;
}menu_data;
struct high_struct
{
char nom[50];
int points;
}high[10],temp_high;
char high_nom[50];
int niveau[24][24];
struct // Create A Structure For The Timer Information
{
__int64 frequency; // Timer Frequency
float resolution; // Timer Resolution
unsigned long mm_timer_start; // Multimedia Timer Start Value
unsigned long mm_timer_elapsed; // Multimedia Timer Elapsed Time
bool performance_timer; // Using The Performance Timer?
__int64 performance_timer_start; // Performance Timer Start Value
__int64 performance_timer_elapsed; // Performance Timer Elapsed Time
} timer;
int adjust = 4;
int steps[6]={ 1, 2, 4, 5, 10, 20 };
void charge_level (int level);
void menu_touche (int key, int x, int y);
void menu (void);
void charge_partie (void);
void clavier (int touche, int X, int Y);
//------------------------------------------------------------------------------
// texte
//------------------------------------------------------------------------------
void texte(
float x,
float y,
float z,
void *font,
char *string) {
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
//------------------------------------------------------------------------------
// Charge une image TGA 32 bits non compressée
//------------------------------------------------------------------------------
int LoadTGA(char *filename, int nb) // Loads A TGA File Into Memory
{
register GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0};// Uncompressed TGA Header
register GLubyte TGAcompare[12]; // Used To Compare TGA Header
register GLubyte header[6]; // First 6 Useful Bytes From The Header
register int imageSize; // Used To Store The Image Size When Setting Aside Ram
register int type; // Set The Default GL Mode To RBGA (32 BPP)
register GLubyte *imageData; // Données de l'image, jusqu'à 32 bits
register int bpp; // Bits Par Pixel de l'image
register int Width, Height; // Taille de l'image
// Lit le fichier et son header
FILE *fichier = fopen(filename, "rb"); // Open The TGA File
if (fread(TGAcompare,1,sizeof(TGAcompare),fichier)!=sizeof(TGAcompare)) EXIT; // Are There 12 Bytes To Read?
if (memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0) EXIT; // Does The Header Match What We Want?
if (fread(header,1,sizeof(header),fichier)!=sizeof(header)) EXIT; // If So Read Next 6 Header Bytes
// Récupère les infos de l'image
Width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
Height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
if (bpp==24) type=GL_RGB; // If So Set The 'type' To GL_RGB
else type=GL_RGBA; // If So Set The 'type' To GL_RGBA
imageSize = Width*Height*bpp/8; // Calculate The Memory Required For The TGA Data
// Charge l'image
imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data
if (fread(imageData, 1, imageSize, fichier)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
delete imageData;
EXIT;
}
fclose (fichier); // Close The File
// Inverse R et B
int t;
for(int i=0; i<imageSize;i+=bpp/8) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
t=imageData[i]; // Temporarily Store The Value At Image Data 'i'
imageData[i]=imageData[i+2]; // Set The 1st Byte To The Value Of The 3rd Byte
imageData[i+2]=t; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}
// Build A Texture From The Data
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(nb, &Name[nb]); // Generate OpenGL texture IDs
glBindTexture(GL_TEXTURE_2D, Name[nb]); // Bind Our Texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // Linear Filtered (ca lisser les textures mais moins ien que le TriLinear Filtering)
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // Linear Filtered
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, type, Width, Height, 0, type, GL_UNSIGNED_BYTE, imageData);
return Name[nb]; // Texture Building Went Ok, Return True
}
//------------------------------------------------------------------------------
// ReSize Function
//------------------------------------------------------------------------------
void ReshapeGL ( int Width, int Height )
{
if (Height==0) Height=1;
CurrentW = Width;
CurrentH = Height;
glViewport ( 0, 0, Width, Height );
glMatrixMode ( GL_PROJECTION );
glLoadIdentity ( );
glOrtho ( 0, WIDTH, HEIGHT, 0, 0, 0.2 ); // Fait un écran Ortho
}
//------------------------------------------------------------------------------
// Initialisation Function
//------------------------------------------------------------------------------
void InitGL ( )
{
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ); // Black Background
glClearDepth(1.0f); // Configuration de la profondeur du buffer
glDepthFunc ( GL_LEQUAL );
glEnable ( GL_DEPTH_TEST );
glAlphaFunc ( GL_GREATER, 0.5f );
glEnable ( GL_ALPHA_TEST );
glEnable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR ); // Colors Enabled
}
//------------------------------------------------------------------------------
// A propos
//------------------------------------------------------------------------------
void about_affiche (void)
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, Name[18] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,384,0,GLUT_BITMAP_HELVETICA_18,"A propos de Pac-Maniak");
glColor3f ( 0.0f,0.0f, 1.0f );
texte(50,420,0,GLUT_BITMAP_HELVETICA_12,"Pac-Maniak a ete programme par : ");
texte(100,440,0,GLUT_BITMAP_HELVETICA_12,"Funcky (funcky@magellan.fpms.ac.be)");
texte(100,460,0,GLUT_BITMAP_HELVETICA_12,"Xoriak (xoriak@hotmail.com)");
texte(100,480,0,GLUT_BITMAP_HELVETICA_12,"Mihov (mihov@magellan.fpms.ac.be)");
texte(50,500,0,GLUT_BITMAP_HELVETICA_12,"Debut de la programmation en avril 2003");
texte(50,520,0,GLUT_BITMAP_HELVETICA_12,"Programme en C/C++ avec la librairie GLUT");
texte(50,570,0,GLUT_BITMAP_HELVETICA_18,"LISTE DES TOUCHES");
texte(50,590,0,GLUT_BITMAP_HELVETICA_12,"F1 : Pause");
texte(50,610,0,GLUT_BITMAP_HELVETICA_12,"F5 : Quitter");
texte(50,630,0,GLUT_BITMAP_HELVETICA_12,"Fleches : deplacement du PacMan");
glColor3f ( 0.2f, 0.2f, 0.2f );
texte(50,700,0,GLUT_BITMAP_HELVETICA_12,"Appuyez sur F1 pour revenir au menu ...");
glColor3f ( 1.0f, 1.0f, 1.0f );
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void about_key (int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
glutSpecialFunc (menu_touche);
glutDisplayFunc ( menu );
}
}
//------------------------------------------------------------------------------
// high scores
//------------------------------------------------------------------------------
void high_lire (void)
{
/*strcpy(high[0].nom,"Funcky");
high[0].points = 100;
strcpy(high[1].nom,"Xoriak");
high[1].points = 100;
strcpy(high[2].nom,"Mihov");
high[2].points = 100;
strcpy(high[3].nom,"Funcky");
high[3].points = 100;
strcpy(high[4].nom,"Xoriak");
high[4].points = 100;
strcpy(high[5].nom,"Mihov");
high[5].points = 100;
strcpy(high[6].nom,"Funcky");
high[6].points = 100;
strcpy(high[7].nom,"Xoriak");
high[7].points = 100;
strcpy(high[8].nom,"Mihov");
high[8].points = 100;
strcpy(high[9].nom,"Funcky");
high[9].points = 100;*/
FILE * fichier;
fichier = fopen("highscores","r");
fread(high,sizeof(high),1,fichier);
fclose(fichier);
}
void high_affiche (void)
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, Name[18] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,384,0,GLUT_BITMAP_HELVETICA_18,"Pac-Maniak : High-Scores");
glColor3f ( 0.0f, 0.0f, 1.0f );
int i = 0;
char chaine[50];
char score[100];
for (i = 0; i < 10; i++)
{
itoa((i +1 ),chaine,10);
strcpy (score,chaine);
strcat (score,". ");
itoa(high[i].points,chaine,10);
strcat (score,high[i].nom);
strcat(score," ( ");
strcat(score,chaine);
strcat(score," )");
texte(50,420 + 20*i,0,GLUT_BITMAP_HELVETICA_12,score);
}
glColor3f ( 0.2f, 0.2f, 0.2f );
texte(50,700,0,GLUT_BITMAP_HELVETICA_12,"Appuyez sur F1 pour revenir au menu ...");
glColor3f ( 1.0f, 1.0f, 1.0f );
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void high_key (int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
glutSpecialFunc (menu_touche);
glutDisplayFunc ( menu );
}
}
void high_inscrire (void)
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
if (menu_data.color < 1)
{
menu_data.color += 0.01;
}
glColor3f ( menu_data.color, menu_data.color, menu_data.color );
glBindTexture(GL_TEXTURE_2D, Name[18] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,384,0,GLUT_BITMAP_HELVETICA_18,"HIGH SCORE !!! FELICITATIONS !!!");
glColor3f (0.0f,0.0f,1.0f);
texte(50,420,0,GLUT_BITMAP_HELVETICA_12,"Veuillez entrer votre nom : ");
texte(50,440,0,GLUT_BITMAP_HELVETICA_12,high_nom);
glColor3f (0.2f,0.2f,0.2f);
texte(50,500,0,GLUT_BITMAP_HELVETICA_12,"Appuyez sur F1 pour valider");
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void high_valider (void)
{
high[9].points = jeu.points;
strcpy (high[9].nom,high_nom);
int i,chgt = 1;
while (chgt == 1)
{
chgt = 0;
for (i = 0; i < 9; i++)
{
if (high[i].points < high[i+1].points)
{
temp_high = high[i];
high[i] = high[i+1];
high[i+1] = temp_high;
chgt = 1;
}
}
}
}
void high_inscrire_key (int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
high_valider();
glutSpecialFunc (menu_touche);
glutDisplayFunc ( menu );
glutKeyboardFunc (NULL);
}
}
void high_inscrire_nom (unsigned char key,int x, int y)
{
high_nom[strlen(high_nom)] = key;
}
void high_ecrire (void)
{
FILE * fichier;
fichier = fopen("highscores","w");
fwrite(high,sizeof(high),1,fichier);
fclose(fichier);
}
//------------------------------------------------------------------------------
// Position entier
//------------------------------------------------------------------------------
void test_collision (void)
{
int i,j;
i= (int)pacman.posX;
j= (int)pacman.posY;
if (pacman.direction == 4)
{
if (niveau[i-1][j] > 100 && niveau[i-1][j] <= 115)
{
jeu.bloque = 1;
}
}
if (pacman.direction == 8)
{
if (niveau[i][j-1] > 100 && niveau[i][j-1] <= 115)
{
jeu.bloque = 1;
}
}
if (pacman.direction == 6)
{
if (niveau[i+1][j] > 100 && niveau[i+1][j] <= 115)
{
jeu.bloque = 1;
}
}
if (pacman.direction == 2)
{
if (niveau[i][j+1] > 100 && niveau[i][j+1] <= 115)
{
jeu.bloque = 1;
}
}
}
void change_direction(void)
{
int i,j;
i= (int)pacman.posX;
j= (int)pacman.posY;
int bloque = 0;
if (jeu.next_direction == 4)
{
if (niveau[i-1][j] > 100 && niveau[i-1][j] <= 115)
{
bloque = 1;
}
}
if (jeu.next_direction == 8)
{
if (niveau[i][j-1] > 100 && niveau[i][j-1] <= 115)
{
bloque = 1;
}
}
if (jeu.next_direction == 6)
{
if (niveau[i+1][j] > 100 && niveau[i+1][j] <= 115)
{
bloque = 1;
}
}
if (jeu.next_direction == 2)
{
if (niveau[i][j+1] > 100 && niveau[i][j+1] <= 115)
{
bloque = 1;
}
}
if (bloque != 1)
{
pacman.direction = jeu.next_direction;
jeu.next_direction = 0;
// printf ("Change\n");
}
}
void affone_chope (void)
{
int i,j;
i = pacman.posX;
j = pacman.posY;
if (niveau[i][j] == 116)
{
niveau[i][j] = 0;
jeu.chopes--;
jeu.points += jeu.niveau;
}
}
//------------------------------------------------------------------------------
// gestion niveau
//------------------------------------------------------------------------------
void lire_niveau(void)
{
FILE * fichier;
fichier = fopen (jeu.level,"r");
int i = 0, j = 0;
for (i = 0; i < 24; i++)
{
for (j = 0; j < 24; j++)
{
fscanf(fichier,"%d",&niveau[j][i]);
if (niveau[j][i] == 116)
{
jeu.chopes++;
}
}
}
fclose (fichier);
jeu.niveau_lu = 1;
}
void affiche_niveau (void)
{
int i,j;
if (jeu.niveau_lu == 0)
{
lire_niveau();
}
for (i = 0; i < 24; i++)
{
for (j = 0; j < 24; j++)
{
if (niveau[i][j] != 0)
{
glBindTexture(GL_TEXTURE_2D, Name[niveau[i][j] - 50] );
glBegin(GL_QUADS);
glTexCoord2d ( 0, 1 ); glVertex2d(i * 32,j * 32);
glTexCoord2d ( 0, 0 ); glVertex2d(i * 32 , j * 32 + 32 );
glTexCoord2d ( 1, 0 ); glVertex2d( i * 32 + 32, j * 32 + 32 );
glTexCoord2d ( 1, 1 ); glVertex2d( i * 32 + 32 ,j * 32 );
glEnd();
}
}
}
}
//------------------------------------------------------------------------------
// Perdu
//------------------------------------------------------------------------------
void perdu (void)
{
jeu.vies--;
if (jeu.vies <= 0)
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(400,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Vous avez perdu ...");
if (jeu.points > high[9].points)
{
glColor3f ( 1.0f, 0.0f, 0.0f );
texte(400,420,0,GLUT_BITMAP_TIMES_ROMAN_24,"HIGH SCORE !!!");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep (5000);
menu_data.color = 0;
strcpy (high_nom,"");
glutSpecialFunc (high_inscrire_key);
glutDisplayFunc ( high_inscrire );
glutKeyboardFunc (high_inscrire_nom);
} else
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep (5000);
menu_data.color = 0;
strcpy (high_nom,"");
glutSpecialFunc (menu_touche);
glutDisplayFunc ( menu );
}
}
else
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(400,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Vous avez ete mange !!!");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep(3000);
jeu.start = 1;
}
}
void perdu_high (void)
{
jeu.vies--;
if (jeu.vies <= 0)
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(400,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Vous avez perdu ...");
glColor3f ( 1.0f, 0.0f, 0.0f );
texte(400,420,0,GLUT_BITMAP_TIMES_ROMAN_24,"HIGH SCORE !!!");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep (5000);
menu_data.color = 0;
strcpy (high_nom,"");
glutSpecialFunc (high_inscrire_key);
glutDisplayFunc ( high_inscrire );
glutKeyboardFunc (high_inscrire_nom);
}
else
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(400,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Vous avez ete mange !!!");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep(3000);
jeu.start = 1;
}
}
//------------------------------------------------------------------------------
// gestion des monstres
//------------------------------------------------------------------------------
void monstres_change (int nbr)
{
int a;
a=(rand()%4)+1;
if (a == 1)
{
monstres[nbr].direction = 8;
}
if (a == 2)
{
monstres[nbr].direction = 2;
}
if (a == 3)
{
monstres[nbr].direction = 6;
}
if (a == 4)
{
monstres[nbr].direction = 4;
}
int k,l;
k= (int)monstres[nbr].posX;
l= (int)monstres[nbr].posY;
int change = 0;
if (monstres[nbr].direction == 4)
{
if (niveau[k-1][l] > 100 && niveau[k-1][l] <= 115)
{
change = 1;
}
}
if (monstres[nbr].direction == 8)
{
if (niveau[k][l-1] > 100 && niveau[k][l-1] <= 115)
{
change = 1;
}
}
if (monstres[nbr].direction == 6)
{
// printf ("monstre test col %f %f\n",monstres[nbr].posX,monstres[nbr].posY);
if (niveau[k+1][l] > 100 && niveau[k+1][l] <= 115)
{
change = 1;
// printf ("monstre col\n");
}
}
if (monstres[nbr].direction == 2)
{
if (niveau[k][l+1] > 100 && niveau[k][l+1] <= 115)
{
change = 1;
}
}
if (change == 1)
{
monstres_change(nbr);
}
}
void gestion_monstres(void)
{
int i,j;
int bloque = 0;
int z,test;
int change;
int vert,bleu,rouge;
vert = bleu = 0;
rouge = 1;
for (z=0; z < jeu.monstres; z++)
{
if (rouge == 1)
{
bleu = 0;
rouge = 0;
vert = 1;
}
else if (bleu == 1)
{
rouge = 1;
bleu = 0;
vert = 0;
}
else if (vert == 1)
{
rouge = 0;
vert = 0;
bleu = 1;
}
glColor3f ( vert, bleu, rouge );
glBindTexture(GL_TEXTURE_2D, Name[9]);
glBegin(GL_QUADS);
glTexCoord2d ( 0, 1 ); glVertex2d(monstres[z].posX * 32,monstres[z].posY * 32);
glTexCoord2d ( 0, 0 ); glVertex2d(monstres[z].posX * 32 , monstres[z].posY * 32 + 32 );
glTexCoord2d ( 1, 0 ); glVertex2d( monstres[z].posX * 32 + 32, monstres[z].posY * 32 + 32 );
glTexCoord2d ( 1, 1 ); glVertex2d( monstres[z].posX * 32 + 32 ,monstres[z].posY * 32 );
glEnd();
glColor3f ( 1.0f, 1.0f, 1.0f );
if (monstres[z].direction == 6)
{
monstres[z].posX+=0.1;
}
if (monstres[z].direction == 4)
{
monstres[z].posX-=0.1;
}
if (monstres[z].direction == 8)
{
monstres[z].posY-=0.1;
}
if (monstres[z].direction == 2)
{
monstres[z].posY+=0.1;
}
test = 0;
if (monstres[z].posX == ceil (monstres[z].posX))
{
test++;
}
else if ( (monstres[z].posX > ceil(monstres[z].posX) - 1 - 0.01 ) && (monstres[z].posX < ceil(monstres[z].posX) -1 + 0.01) )
{
test++;
monstres[z].posX = ceil (monstres[z].posX) - 1;
}
else if ( (monstres[z].posX > ceil(monstres[z].posX) - 0.01 ) && (monstres[z].posX < ceil(monstres[z].posX) + 0.01) )
{
test++;
monstres[z].posX = ceil (monstres[z].posX);
}
if (monstres[z].posY == ceil (monstres[z].posY))
{
test++;
}
else if ( (monstres[z].posY > ceil(monstres[z].posY) -1 - 0.01 ) && (monstres[z].posY < ceil(monstres[z].posY) -1 + 0.01) )
{
test++;
monstres[z].posY = ceil (monstres[z].posY) - 1;
}
else if ( (monstres[z].posY > ceil(monstres[z].posY) - 0.01 ) && (monstres[z].posY < ceil(monstres[z].posY) + 0.01) )
{
test++;
monstres[z].posY = ceil (monstres[z].posY) ;
}
if (test == 2)
{
i= (int)monstres[z].posX;
j= (int)monstres[z].posY;
change=0;
if (monstres[z].direction == 4)
{
if (niveau[i-1][j] > 100 && niveau[i-1][j] <= 115)
{
change = 1;
}
}
if (monstres[z].direction == 8)
{
if (niveau[i][j-1] > 100 && niveau[i][j-1] <= 115)
{
change = 1;
}
}
if (monstres[z].direction == 6)
{
if (niveau[i+1][j] > 100 && niveau[i+1][j] <= 115)
{
change = 1;
}
}
if (monstres[z].direction == 2)
{
if (niveau[i][j+1] > 100 && niveau[i][j+1] <= 115)
{
change = 1;
}
}
if (change == 1)
{
monstres_change(z);
// printf ("Change\n");
}
else
{
change = 4;
if (niveau[i][j+1] > 100 && niveau[i][j+1] <= 115)
{
change--;
}
if (niveau[i+1][j] > 100 && niveau[i+1][j] <= 115)
{
change--;
}
if (niveau[i][j-1] > 100 && niveau[i][j-1] <= 115)
{
change--;
}
if (niveau[i-1][j] > 100 && niveau[i-1][j] <= 115)
{
change--;
}
if (change >= 3)
{
monstres_change(z);
}
}
}
if (monstres[z].posX - 0.5 < pacman.posX && monstres[z].posX + 0.5 > pacman.posX)
{
if (monstres[z].posY - 0.5 < pacman.posY && monstres[z].posY + 0.5 > pacman.posY)
{
jeu.perdu = 1;
}
}
}
}
//------------------------------------------------------------------------------
// afficher les scores
//------------------------------------------------------------------------------
void affiche_score (void)
{
glColor3f ( 1.0f, 1.0f, 1.0f );
glBindTexture(GL_TEXTURE_2D, Name[10] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 832, 32 );
glTexCoord2d ( 0, 0 ); glVertex2d( 832, 96 );
glTexCoord2d ( 1, 0 ); glVertex2d( 960, 96 );
glTexCoord2d ( 1, 1 ); glVertex2d( 960, 32 );
glEnd ( );
glBindTexture(GL_TEXTURE_2D, Name[11] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 760, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 760, 768 );
glTexCoord2d ( 1, 0 ); glVertex2d( 768, 768 );
glTexCoord2d ( 1, 1 ); glVertex2d( 768, 0 );
glEnd ( );
glBindTexture(GL_TEXTURE_2D, Name[12] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 768, 256 );
glTexCoord2d ( 0, 0 ); glVertex2d( 768, 264 );
glTexCoord2d ( 1, 0 ); glVertex2d( 1024, 264 );
glTexCoord2d ( 1, 1 ); glVertex2d( 1024, 256 );
glEnd ( );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 768, 512 );
glTexCoord2d ( 0, 0 ); glVertex2d( 768, 520 );
glTexCoord2d ( 1, 0 ); glVertex2d( 1024, 520 );
glTexCoord2d ( 1, 1 ); glVertex2d( 1024, 512 );
glEnd ( );
glBindTexture(GL_TEXTURE_2D, Name[13] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 832, 288 );
glTexCoord2d ( 0, 0 ); glVertex2d( 832, 352 );
glTexCoord2d ( 1, 0 ); glVertex2d( 960, 352 );
glTexCoord2d ( 1, 1 ); glVertex2d( 960, 288 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 1.0f, 0.0f, 0.0f );
char chaine[50];
itoa(jeu.points,chaine,10);
texte(832,128,0,GLUT_BITMAP_TIMES_ROMAN_24,chaine);
glDisable ( GL_COLOR);
glEnable ( GL_TEXTURE_2D );
glColor3f ( 1.0f, 1.0f, 1.0f );
glBindTexture(GL_TEXTURE_2D, Name[jeu.vies + 13] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 864, 384 );
glTexCoord2d ( 0, 0 ); glVertex2d( 864, 448 );
glTexCoord2d ( 1, 0 ); glVertex2d( 928, 448 );
glTexCoord2d ( 1, 1 ); glVertex2d( 928, 384 );
glEnd ( );
}
//------------------------------------------------------------------------------
// afficher
//------------------------------------------------------------------------------
void afficher (void)
{
float start = glutGet(GLUT_ELAPSED_TIME);
while(glutGet(GLUT_ELAPSED_TIME) < start+float(steps[adjust]*2.0f)) {}// Waste Cycles On Fast Systems
int i,j;
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 1.0f, 1.0f, 1.0f );
glBegin ( GL_QUADS );
glVertex2d( 0, 0 );
glVertex2d( 0, HEIGHT );
glVertex2d( WIDTH, HEIGHT );
glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, Name[0] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
// printf ("%f ",pacman.posY);
// printf ("%f ",ceil (pacman.posY));
// printf ("%f ",pacman.posX);
// printf ("%f\n",ceil (pacman.posX));
int test = 0;
if (pacman.posX == ceil (pacman.posX))
{
test++;
}
else if ( (pacman.posX > ceil(pacman.posX) - 1 - 0.01 ) && (pacman.posX < ceil(pacman.posX) -1 + 0.01) )
{
test++;
pacman.posX = ceil (pacman.posX) - 1;
}
else if ( (pacman.posX > ceil(pacman.posX) - 0.01 ) && (pacman.posX < ceil(pacman.posX) + 0.01) )
{
test++;
pacman.posX = ceil (pacman.posX);
}
if (pacman.posY == ceil (pacman.posY))
{
test++;
}
else if ( (pacman.posY > ceil(pacman.posY) -1 - 0.01 ) && (pacman.posY < ceil(pacman.posY) -1 + 0.01) )
{
test++;
pacman.posY = ceil (pacman.posY) - 1;
}
else if ( (pacman.posY > ceil(pacman.posY) - 0.01 ) && (pacman.posY < ceil(pacman.posY) + 0.01) )
{
test++;
pacman.posY = ceil (pacman.posY) ;
}
if (test == 2)
{
// printf ("Test OK %d\n",jeu.next_direction);
if (jeu.next_direction != 0 && jeu.bloque != 1)
{
change_direction();
}
test_collision();
affone_chope();
}
int text;
if (jeu.bloque != 1)
{
if (pacman.direction == 6)
{
text = 1;
pacman.posX+=0.1;
}
if (pacman.direction == 4)
{
text = 2;
pacman.posX-=0.1;
}
if (pacman.direction == 8)
{
text = 3;
pacman.posY-=0.1;
}
if (pacman.direction == 2)
{
text = 4;
pacman.posY+=0.1;
}
if (jeu.ferme < 16)
{
jeu.ferme++;
}
else
{
if (jeu.ferme < 32)
{
jeu.ferme++;
text+=4;
}
else
{
jeu.ferme = 0;
}
}
}
else
{
if (pacman.direction == 6)
{
text = 1;
}
if (pacman.direction == 4)
{
text = 2;
}
if (pacman.direction == 8)
{
text = 3;
}
if (pacman.direction == 2)
{
text = 4;
}
}
affiche_niveau();
int k=0;
if (jeu.start == 1)
{
for (i = 0; i < 24; i++)
{
for (j = 0; j < 24; j++)
{
if (niveau[i][j] == 1)
{
pacman.posX = i;
pacman.posY = j;
jeu.start = 0;
}
if (niveau[i][j] == 2)
{
monstres[k].posX = i;
monstres[k].posY = j;
monstres[k].direction = 6;
k++;
}
}
}
}
gestion_monstres();
glBindTexture(GL_TEXTURE_2D, Name[text]);
glBegin(GL_QUADS);
glTexCoord2d ( 0, 1 ); glVertex2d(pacman.posX * 32,pacman.posY * 32);
glTexCoord2d ( 0, 0 ); glVertex2d(pacman.posX * 32 , pacman.posY * 32 + 32 );
glTexCoord2d ( 1, 0 ); glVertex2d( pacman.posX * 32 + 32, pacman.posY * 32 + 32 );
glTexCoord2d ( 1, 1 ); glVertex2d( pacman.posX * 32 + 32 ,pacman.posY * 32 );
glEnd();
affiche_score();
if (jeu.perdu == 1)
{
if (jeu.points <= high[9].points)
{
perdu();
jeu.perdu = 0;
}
else
{
perdu_high();
jeu.perdu = 0;
}
}
if (jeu.chopes <= 0)
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(400,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Vous avez gagne ...");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glColor3f ( 1.0f, 1.0f, 1.0f );
glutSwapBuffers ( );
glutPostRedisplay ( );
Sleep(5000);
jeu.niveau++;
charge_level(jeu.niveau);
}
glutSwapBuffers ( );
glutPostRedisplay ( );
}
//------------------------------------------------------------------------------
// deplacement du pac
//------------------------------------------------------------------------------
void bouge_haut (void)
{
jeu.next_direction = 8;
// printf ("touche appuyee %d\n",jeu.next_direction);
jeu.bloque = 0;
}
void bouge_bas (void)
{
jeu.next_direction = 2;
// printf ("touche appuyee %d\n",jeu.next_direction);
jeu.bloque = 0;
}
void bouge_droit (void)
{
jeu.next_direction = 6;
// printf ("touche appuyee %d\n",jeu.next_direction);
jeu.bloque = 0;
}
void bouge_gauche (void)
{
jeu.next_direction = 4;
// printf ("touche appuyee %d\n",jeu.next_direction);
jeu.bloque = 0;
}
//------------------------------------------------------------------------------
// pause
//------------------------------------------------------------------------------
void pause_touche (int touche, int X, int Y)
{
if (touche == GLUT_KEY_F1)
{
glutSpecialFunc(clavier);
glutDisplayFunc ( afficher );
}
}
void pause_affiche (void)
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
if (menu_data.color < 1)
{
menu_data.color += 0.01;
}
glColor3f ( menu_data.color, menu_data.color, menu_data.color );
glBindTexture(GL_TEXTURE_2D, Name[18] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,384,0,GLUT_BITMAP_TIMES_ROMAN_24,"Pause, appuyez sur F1 pour continuer ...");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glColor3f ( 1.0f, 1.0f, 1.0f );
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void pause (void)
{
menu_data.color = 0;
glutSpecialFunc (pause_touche);
glutReshapeFunc ( ReshapeGL );
glutDisplayFunc ( pause_affiche );
}
void backtomenu (void)
{
menu_data.color = 0;
glutSpecialFunc (menu_touche);
glutDisplayFunc ( menu );
}
//------------------------------------------------------------------------------
// clavier
//------------------------------------------------------------------------------
void clavier (int touche, int X, int Y)
{
switch (touche)
{
case GLUT_KEY_F1 : pause();
break;
case GLUT_KEY_UP : bouge_haut();
break;
case GLUT_KEY_LEFT : bouge_gauche();
break;
case GLUT_KEY_RIGHT : bouge_droit();
break;
case GLUT_KEY_DOWN : bouge_bas();
break;
case GLUT_KEY_F5 : backtomenu();
break;
}
}
//------------------------------------------------------------------------------
// initialisation de la partie
//------------------------------------------------------------------------------
void init_game (void)
{
pacman.direction = 6;
jeu.ferme = 0;
jeu.niveau_lu = 0;
jeu.start = 1;
jeu.bloque = 0;
jeu.next_direction = 6;
jeu.chopes = 0;
jeu.monstres = 4;
}
//------------------------------------------------------------------------------
// debut niveau
//------------------------------------------------------------------------------
void charge_level (int level)
{
init_game();
char chaine[50];
strcpy(jeu.level,"level");
itoa(level,chaine,10);
strcat(jeu.level,chaine);
jeu.niveau = level;
pacman.taille = 32;
}
//------------------------------------------------------------------------------
// debut partie
//------------------------------------------------------------------------------
void charge_partie (void)
{
jeu.vies = 3;
charge_level(1);
jeu.points = 0;
jeu.perdu = 0;
}
//------------------------------------------------------------------------------
// quitte
//------------------------------------------------------------------------------
void quit (void)
{
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, Name[18] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,384,0,GLUT_BITMAP_HELVETICA_12,"Nous vous remercions d'avoir joue a Pac-Maniak.");
texte(50,398,0,GLUT_BITMAP_HELVETICA_12,"Ce jeu a ete cree par :");
glColor3f ( 1.0f, 0.0f, 0.0f );
texte(100,420,0,GLUT_BITMAP_HELVETICA_18,"Funcky (funcky@magellan.fpms.ac.be)");
glColor3f ( 1.0f, 0.0f, 0.0f );
texte(100,440,0,GLUT_BITMAP_HELVETICA_18,"Xoriak (xoriak@hotmail.com)");
glColor3f ( 1.0f, 0.0f, 0.0f );
texte(100,460,0,GLUT_BITMAP_HELVETICA_18,"Mihov (mihov@magellan.fpms.ac.be)");
glColor3f ( 0.0f, 1.0f, 0.0f );
texte(50,480,0,GLUT_BITMAP_HELVETICA_12,"Si vous avez des suggestions n'hestez pas a nous les envoyer.");
glColor3f ( 0.5f, 0.5f, 0.5f );
texte(50,550,0,GLUT_BITMAP_HELVETICA_12,"PAC-Maniak Beta");
texte(50,564,0,GLUT_BITMAP_HELVETICA_12,"Appuyez sur F1 pour quitter.");
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glColor3f ( 1.0f, 1.0f, 1.0f );
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void quit_touche (int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
high_ecrire();
exit(0);
}
}
//------------------------------------------------------------------------------
// gestion menu
//------------------------------------------------------------------------------
void menu (void)
{
glDisable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR );
if (menu_data.color < 1)
{
menu_data.color += 0.01;
}
glColor3f ( menu_data.color, menu_data.color, menu_data.color );
/*glBegin ( GL_QUADS );
glVertex2d( 0, 0 );
glVertex2d( 0, HEIGHT );
glVertex2d( WIDTH, HEIGHT );
glVertex2d( WIDTH, 0 );
glEnd ( );*/
glDisable ( GL_COLOR );
glEnable ( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, Name[17] );
glBegin ( GL_QUADS );
glTexCoord2d ( 0, 1 ); glVertex2d( 0, 0 );
glTexCoord2d ( 0, 0 ); glVertex2d( 0, HEIGHT );
glTexCoord2d ( 1, 0 ); glVertex2d( WIDTH, HEIGHT );
glTexCoord2d ( 1, 1 ); glVertex2d( WIDTH, 0 );
glEnd ( );
glutSwapBuffers ( );
glutPostRedisplay ( );
}
void menu_touche (int key, int x, int y)
{
if (key == GLUT_KEY_F1)
{
charge_partie();
glutSpecialFunc(clavier);
glutDisplayFunc ( afficher );
}
if (key == GLUT_KEY_F4)
{
menu_data.color = 0;
glutSpecialFunc(quit_touche);
glutDisplayFunc ( quit );
}
if (key == GLUT_KEY_F2)
{
glutSpecialFunc(high_key);
glutDisplayFunc ( high_affiche);
}
if (key == GLUT_KEY_F3)
{
glutSpecialFunc(about_key);
glutDisplayFunc ( about_affiche);
}
}
//------------------------------------------------------------------------------
// main
//------------------------------------------------------------------------------
void main( int argc, char *argv[ ], char *envp[ ] )
{
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize ( WIDTH, HEIGHT );
glutInitWindowPosition ( 50, 50 );
glutCreateWindow ( "Pac-Maniak Beta" );
glutSetCursor(GLUT_CURSOR_NONE );
glutFullScreen();
InitGL ( );
LoadTGA ("img/wall.tga",0);
LoadTGA ("img/pacou_d.tga",1);
LoadTGA ("img/pacou_g.tga",2);
LoadTGA ("img/pacou_h.tga",3);
LoadTGA ("img/pacou_b.tga",4);
LoadTGA ("img/pacfer_d.tga",5);
LoadTGA ("img/pacfer_g.tga",6);
LoadTGA ("img/pacfer_h.tga",7);
LoadTGA ("img/pacfer_b.tga",8);
LoadTGA ("img/monstre.tga",9);
LoadTGA ("img/points.tga",10);
LoadTGA ("img/score_v.tga",11);
LoadTGA ("img/score_h.tga",12);
LoadTGA ("img/vies.tga",13);
LoadTGA ("img/1.tga",14);
LoadTGA ("img/2.tga",15);
LoadTGA ("img/3.tga",16);
LoadTGA ("img/menu.tga",17);
LoadTGA ("img/menu_r.tga",18);
LoadTGA ("img/mur_h.tga",51);
LoadTGA ("img/mur_v.tga",52);
LoadTGA ("img/coin_h_d.tga",53);
LoadTGA ("img/coin_b_g.tga",54);
LoadTGA ("img/coin_b_d.tga",55);
LoadTGA ("img/coin_h_g.tga",56);
LoadTGA ("img/bout_b.tga",57);
LoadTGA ("img/bout_h.tga",58);
LoadTGA ("img/bout_d.tga",59);
LoadTGA ("img/bout_g.tga",60);
LoadTGA ("img/t_h.tga",61);
LoadTGA ("img/t_d.tga",62);
LoadTGA ("img/t_b.tga",63);
LoadTGA ("img/t_g.tga",64);
LoadTGA ("img/croix.tga",65);
LoadTGA ("img/biere.tga",66);
high_lire();
charge_partie();
menu_data.color = 0;
glutSpecialFunc (menu_touche);
glutReshapeFunc ( ReshapeGL );
glutDisplayFunc ( menu );
//glutMouseFunc ( Souris );
//glutPassiveMotionFunc (Souris_mvt);
glutMainLoop ( );
}
Conclusion :
C'est un peu (même beaucoup) programmé à la barbare, mais il a été fait pour un concours, nous avions une nuit pour y arriver, alors l'optimisation du code, on laisse tomber ...
Je mettrais plus tard une version améliorée puisqu'on travaille encore dessus ...
PS : vous pouvez trouvez la version compilée ici :
http://users.magellan.fpms.ac.be/funcky/PacManiak.zip (pour les niveaux et les images ...)
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.