blacktanker | Bonjour, si je viens quérir de l'aide ici c'est que je n'ai pas réussi malgré mes recherches à comprendre mon erreur.
Je programme un tetris avec la librairie GLUT de OpenGL. Le problème survient lorsque que je désire déplacer la pièce vers la gauche. Ceci viens de la manière de détecter la collision du bords gauche. Mais je ne trouve pas mon erreur.
voici mon code:
Code :
- #include <stdlib.h> /*Utilisé pour la fonction "exit"*/
- #include <math.h>
- #include <string.h>
- #include <stdio.h>
- #include <stdarg.h>
- #include <time.h>
- #ifdef __APPLE__
- #include <OpenGL/OpenGL.h>
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #define X_MIN 100 // Les positions du cadre
- #define X_MAX 350
- #define Y_MIN 050
- #define Y_MAX 550
- /*fonction update timer*/
- /*fonction décrémente*/
- /******************************************---Variables********************************************************************************************/
- int pieces[7][4][4][4]={
- {//L
- {//p1
- {0,0,0,0},
- {0,1,1,0},
- {0,1,0,0},
- {0,1,0,0}
- },
- {//p2
- {0,0,0,0},
- {0,1,1,1},
- {0,0,0,1},
- {0,0,0,0}
- },
- {//p3
- {0,0,0,0},
- {0,0,1,0},
- {0,0,1,0},
- {0,1,1,0}
- },
- {//p4
- {0,0,0,0},
- {0,1,0,0},
- {0,1,1,1},
- {0,0,0,0}
- }
- },
- {//L2
- {//p1
- {0,0,0,0},
- {0,1,1,0},
- {0,0,1,0},
- {0,0,1,0}
- },
- {//p2
- {0,0,0,0},
- {0,0,0,1},
- {0,1,1,1},
- {0,0,0,0}
- },
- {//p3
- {0,0,0,0},
- {0,1,0,0},
- {0,1,0,0},
- {0,1,1,0}
- },
- {//4
- {0,0,0,0},
- {0,1,1,1},
- {0,1,0,0},
- {0,0,0,0}
- }
- },
- {//barre
- {//bh
- {0,0,0,0},
- {1,1,1,1},
- {0,0,0,0},
- {0,0,0,0}
- },
- {//bv
- {0,1,0,0},
- {0,1,0,0},
- {0,1,0,0},
- {0,1,0,0}
- },
- {//bh
- {0,0,0,0},
- {1,1,1,1},
- {0,0,0,0},
- {0,0,0,0}
- },
- {//bv
- {0,1,0,0},
- {0,1,0,0},
- {0,1,0,0},
- {0,1,0,0}
- }
- },
- {//carre
- {//p1
- {0,0,0,0},
- {0,1,1,0},
- {0,1,1,0},
- {0,0,0,0}
- },
- {//p2
- {0,0,0,0},
- {0,1,1,0},
- {0,1,1,0},
- {0,0,0,0}
- },
- {//p3
- {0,0,0,0},
- {0,1,1,0},
- {0,1,1,0},
- {0,0,0,0}
- },
- {//p4
- {0,0,0,0},
- {0,1,1,0},
- {0,1,1,0},
- {0,0,0,0}
- }
- },
- {//T
- {//p1
- {0,0,0,0},
- {0,1,1,1},
- {0,0,1,0},
- {0,0,0,0}
- },
- {//p2
- {0,0,0,0},
- {0,1,0,0},
- {0,1,1,0},
- {0,1,0,0}
- },
- {//p3
- {0,0,0,0},
- {0,0,1,0},
- {0,1,1,1},
- {0,0,0,0}
- },
- {//p4
- {0,0,0,0},
- {0,0,1,0},
- {0,1,1,0},
- {0,0,1,0}
- }
- },
- {//S
- {//p1
- {0,0,0,0},
- {0,1,1,0},
- {0,0,1,1},
- {0,0,0,0}
- },
- {//p2
- {0,0,0,0},
- {0,0,1,0},
- {0,1,1,0},
- {0,1,0,0}
- },
- {//p3
- {0,0,0,0},
- {0,1,1,0},
- {0,0,1,1},
- {0,0,0,0}
- },
- {//p4
- {0,0,0,0},
- {0,0,1,0},
- {0,1,1,0},
- {0,1,0,0}
- }
- },
- {//Z
- {//p1
- {0,0,0,0},
- {0,0,1,1},
- {0,1,1,0},
- {0,0,0,0}
- },
- {//p2
- {0,0,0,0},
- {0,1,0,0},
- {0,1,1,0},
- {0,0,1,0}
- },
- {//p3
- {0,0,0,0},
- {0,0,1,1},
- {0,1,1,0},
- {0,0,0,0}
- },
- {//p4
- {0,0,0,0},
- {0,1,0,0},
- {0,1,1,0},
- {0,0,1,0}
- }
- }
- };
- int terrain[22][12]={{0,0,0,0,0,0,0,0,0,0,0,0},
- {0,0,0,0,0,0,0,0,0,0,0,0},
- {0,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0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,1},
- {1,0,0,0,0,0,0,0,0,0,0,1},
- {1,1,1,1,1,1,1,1,1,1,1,1}};
- int piece_choisie[4][4]={{0}};
- int score=0;
- int x,y;
- int i;int d=1;
- int my=450, mx=100;
- int choix; int orientation=0 ;
- /***choix*/
- void determination()
- {
- srand(time(NULL));
- choix= rand()%6;
- printf (" choix = %d \n", choix);
- }
- /******************************************---Base/Carre---****************************************************************************************/
- void base(int x,int y) // creation de la base qui servira pour toutes les figures (ensemble de carre) float r,float v,float b
- {
- // float colr = (rand()%255)/255.0;
- // float colv = (rand()%255)/255.0;
- // float colb = (rand()%255)/255.0;
- int colr=0;
- int colv=1;
- int colb=1;
- glBegin(GL_POLYGON);
- glColor3f(colr,colv,colb);
- glVertex2f(x*25,y*25);
- glColor3f(colr,colv,0);
- glVertex2f(25+x*25,y*25);
- glColor3f(colr,colv,colb);
- glVertex2f(25+x*25,25+y*25);
- glColor3f(1,1,1);
- glVertex2f(x*25,25+y*25);
- glEnd();
- glFlush();
- glutSwapBuffers() ;
- }
- /************************/
- int Collision(int terrain[22][12],int terrainfix[22][12],int piece_choisie[4][4],int my,int mx)
- {
- int i,j;
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- if(piece_choisie[i][j]==1 && terrainfix[i+my][j+mx]==1) // permet de garder les contours lorsque la piece les approches
- {
- return 1;
- }
- if(piece_choisie[i][j]==1 && terrain[i+my][j+mx]==1) // permet de garder les contours lorsque la piece les approches
- {
- return 1;
- }
- }
- }
- return 0;
- }
- /******************************************---Chute---**********************************************************************************************/
- int Chute(int y, int x)
- {
- if(my>0)
- {
- my=my-1;
- printf (" chute my = %d \n", my);
- printf (" chute mx = %d \n", mx);
- glutPostRedisplay();
- }
- else
- {
- my=my;
- printf (" chute my = %d \n", my);
- printf (" chute mx = %d \n", mx);
- int i,j;
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- {
- terrain[i+my][j+mx]=terrain[i+my][j+mx]+piece_choisie[i][j];
- }
- }
- for( x=0;x<12;x++)
- {
- for(y=0;y<22;y++)
- {
- printf (" terrain[%d][%d] = %d \n", y, x, terrain[y][x]);
- }
- }
- }
- return 0;
- }
- /******************************************---Dessine les pieces---********************************************************************************/
- void dessin_piece(int pieces[7][4][4][4])
- {
-
- for( x=0;x<4;x++)
- {
- for(y=0;y<4;y++)
- {
- piece_choisie[y][x]=pieces[choix][orientation][y][x];
- printf (" piece[%d][%d][%d][%d] = %d \n", choix, orientation, y, x, pieces[choix][orientation][y][x]);
- if(piece_choisie[y][x]==1)
- {
- base(x,y);
- }
- }
- }
- }
- /******************************************---Dessine le terrain---********************************************************************************/
- void dessin_terrainfix(int terrainfix[22][12])
- {
-
- for( x=0;x<12;x++)
- {
- for(y=0;y<22;y++)
- {
- if(terrainfix[y][x]==1)
- {
- base(x,y);
- }
- }
- }
- }
- void dessin_terrain(int terrain[22][12])
- {
-
- for( x=0;x<12;x++)
- {
- for(y=0;y<22;y++)
- {
- if(terrain[y][x]==1)
- {
- base(x,y);
- }
- }
- }
- }
- /******************************************---Fonction pour utiliser du texte---*******************************************************************/
- void bitmap_output(int x,int y,char *string,void *font) {
- int len,i;
- glRasterPos2f(x,y);
- len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutBitmapCharacter(font,string[i]); }
- }
- /******************************************---Fenetre---*******************************************************************************************/
- void dessin(void) //fonction de dessin
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glColor3f(1, 0, 0);
- bitmap_output(470,350,"Score:",GLUT_BITMAP_HELVETICA_18);
- glTranslatef(100,25,0);
- dessin_terrainfix(terrainfix);
- dessin_terrain(terrain);
- printf (" dessin mx = %d \n ", mx);
- glTranslatef(mx,my,0);
- Chute(my,mx);
- dessin_piece(pieces);
- }
- /******************************************---Restructuration de la fenetre et changement de repere---*********************************************/
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 600, 0, 600, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- /******************************************---Fonctions de Déplacements---*************************************************************************/
- void deplacement_gauche()
- {
- if(Collision(terrain,terrainfix,piece_choisie,my,mx-1)==0)
- {
- mx=mx-5;
- printf (" key mx = %d \n", mx);
- glutPostRedisplay();
- }
- else
- {
- mx=mx;
- }
- }
- /******************************************---Touches Special du Clavier---*************************************************************************/
- void GestionSpecial(int key, int x, int y)
- {
- switch (key)
- {
- case GLUT_KEY_RIGHT:
-
- if(mx<200)
- {mx=mx+5;
- printf (" key mx = %d \n", mx);
- glutPostRedisplay();
- break;}
- else
- {mx=mx;
- break;}
- case GLUT_KEY_LEFT:
- deplacement_gauche();break;
- case GLUT_KEY_UP:
-
- if(my<450)
- {my=my+1;
- printf (" key my = %d \n", my);
- glutPostRedisplay();
- break;}
- else
- {my=my;
- break;}
- case GLUT_KEY_DOWN:
-
- if(my>0)
- {my=my-5;
- printf (" key my = %d \n", my);
- glutPostRedisplay();
- break;}
- else
- {my=my;
- break;}
- case GLUT_KEY_F1 :
- glutPostRedisplay() ;
- break;
- }
- }
- /******************************************---Touches du Clavier---*********************************************************************************/
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case ' ':
- if(orientation!=3)
- {
- orientation=orientation+1 ;
- glutPostRedisplay();
- break;
- }
- else
- {
- orientation=0;
- glutPostRedisplay();
- break;}
-
-
- case 99 /*c*/:
- if(choix!=6)
- {
- choix=choix+1 ;
- glutPostRedisplay() ;
- break;
- }
- else
- {
- choix=0;
- glutPostRedisplay();
- break;}
-
- case 120/*x*/:
-
- printf (" dessin \n" );
- srand(time(NULL));
- choix= rand()%6;
- glutPostRedisplay() ;
- break;
- case 27 /* Esc */:
- exit(1) ;
- }
- }
- /******************************************---Fonction Principal---*********************************************************************************/
- int main(int argc, char **argv)
- {
-
- glutInit(&argc, argv) ;
- glutInitWindowSize(600,600);
- glutInitWindowPosition(300,150);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE) ;
- glutCreateWindow("Tetris" ) ;
- determination();
- glutDisplayFunc(dessin) ;
- glutReshapeFunc(reshape);
- //glutMotionFunc(motion) ;
- glutKeyboardFunc(keyboard) ;
- glutSpecialFunc(GestionSpecial);
- glutMainLoop() ;
- return 0 ;
- }
|
J'espère que vous pourrez m'aider.
Merci. |