SAE11_2021/jeu.c

208 lines
5.0 KiB
C
Raw Normal View History

2022-11-30 17:23:59 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <graph.h>
#include <time.h>
#include "images.h"
#include "jeu.h"
#include <unistd.h>
2022-12-03 18:52:19 +01:00
#include "cheat.h"
2022-12-10 16:29:19 +01:00
#include "rejouer.h"
2022-11-30 17:23:59 +01:00
int jeu(int position[20][4], int nb_paires,int tab[40]){
int score=0,score2,repet;
int pos_x,pos_y,oui;
int i,j,g,l=0,n=0,x=0,stock_g_1,stock_g_2,sauv_pos[80];
2022-12-02 19:46:42 +01:00
int repet2, nb_images;
2022-12-10 16:29:19 +01:00
int a;
int clic_x,clic_y,im_x, im_x_1, im_x_2, im_y, im_y_1, im_y_2,xpos,ypos;
2022-12-10 17:16:06 +01:00
unsigned long temps=0, temps2=Microsecondes(),temps3=1;
2022-12-02 19:46:42 +01:00
char tempstab[50],affichage[50],scoretab[50];
2022-11-30 22:19:45 +01:00
couleur c=CouleurParComposante(0,0,0);
2022-12-10 16:29:19 +01:00
ChargerImage("./Images/score.png",760,200,0,0,224,63);
nb_images=nb_paires*2;
score=0;
2022-12-02 20:32:05 +01:00
c=CouleurParComposante(255,255,255);
ChoisirCouleurDessin(c);
2022-12-10 16:29:19 +01:00
RemplirRectangle(850,270,50,50);
snprintf(scoretab,50,"%d",score);
c=CouleurParComposante(0,0,0);
ChoisirCouleurDessin(c);
2022-12-10 16:29:19 +01:00
EcrireTexte(865,310,scoretab,2);
ChargerImage("./Images/X.png",950,15,0,0,62,63);
for (i=0;i<80;i++){
sauv_pos[i]=0;
}
while(score<nb_paires){
2022-11-30 21:06:30 +01:00
temps=(Microsecondes()-temps2)/1000000;
if(temps!=temps3){
2022-11-30 22:19:45 +01:00
snprintf(tempstab,50,"%ld secondes ecoulees",temps);
c=CouleurParComposante(0,0,0);
ChoisirCouleurDessin(c);
2022-12-10 17:16:06 +01:00
RemplirRectangle(863,100,120,20);
2022-11-30 22:19:45 +01:00
c=CouleurParComposante(255,255,255);
ChoisirCouleurDessin(c);
2022-12-10 17:16:06 +01:00
EcrireTexte(867,117,tempstab,0);
}
2022-11-30 22:19:45 +01:00
temps3=temps;
repet=0;
2022-12-10 14:53:42 +01:00
im_x=0;
2022-12-08 12:43:17 +01:00
im_x_1=0;
im_x_2=0;
2022-12-10 14:53:42 +01:00
im_y=0;
2022-12-08 12:43:17 +01:00
im_y_1=0;
im_y_2=0;
while (repet<2){
2022-12-10 16:29:19 +01:00
xpos=0;
ypos=0;
temps=(Microsecondes()-temps2)/1000000;
if(temps!=temps3){
snprintf(tempstab,50,"%ld secondes ecoulees",temps);
c=CouleurParComposante(0,0,0);
ChoisirCouleurDessin(c);
2022-12-10 17:16:06 +01:00
RemplirRectangle(863,100,120,20);
c=CouleurParComposante(255,255,255);
ChoisirCouleurDessin(c);
2022-12-10 17:16:06 +01:00
EcrireTexte(867,117,tempstab,0);
}
temps3=temps;
2022-12-02 20:09:13 +01:00
/*Triche*/
2022-12-03 16:38:44 +01:00
if(ToucheEnAttente() && Touche()==XK_t){
2022-12-10 14:53:42 +01:00
n=cheat(affichage,tab, sauv_pos, im_x, im_y);
}
2022-12-02 20:09:13 +01:00
/*Clic de la souris*/
2022-12-10 16:29:19 +01:00
if (SourisCliquee()==1){
2022-11-30 17:23:59 +01:00
clic_x=_X;
clic_y=_Y;
2022-12-10 16:29:19 +01:00
xpos=_X;
ypos=_Y;
2022-11-30 17:23:59 +01:00
im_x=0;
im_y=0;
2022-12-10 16:29:19 +01:00
/*Pour fermer le jeu avec le bouton X*/
if((xpos>=950 && xpos<=1002) && (ypos>=15 && ypos<=78)){
FermerGraphique();
return EXIT_SUCCESS;
}
2022-12-02 20:09:13 +01:00
/*Détection des coordonées x et y du clic de la souris*/
2022-11-30 17:23:59 +01:00
for (pos_y=370;pos_y>=10;pos_y=pos_y-90){
if (clic_y>=pos_y){
if (clic_y<=pos_y+80){
im_y=pos_y;
2022-12-02 20:09:13 +01:00
if (repet==0){
im_y_1=pos_y;
}
else{
im_y_2=pos_y;
}
2022-11-30 17:23:59 +01:00
break;
}
}
}
for (pos_x=640;pos_x>=10;pos_x=pos_x-90){
if (clic_x>=pos_x){
if (clic_x<=pos_x+80){
im_x=pos_x;
2022-12-02 20:09:13 +01:00
if (repet==0){
im_x_1=pos_x;
}
else{
im_x_2=pos_x;
}
2022-11-30 17:23:59 +01:00
break;
}
}
}
/*Si les coordoonées correspondent à une image, et une image différente pour le 2e cilc*/
2022-12-02 20:32:05 +01:00
if (im_x!=0 && im_y!=0 && (im_x_1!=im_x_2 || im_y_1!=im_y_2)){
2022-11-30 17:23:59 +01:00
for (i=0;i<20;i++){
for (j=0;j<2;j++){
if (position[i][(j*2)]==im_x && position[i][(j*2+1)]==im_y){
2022-12-02 20:09:13 +01:00
g=i+1;
2022-12-01 23:17:52 +01:00
if (repet==0){
stock_g_1=g;
}
else{
stock_g_2=g;
}
2022-12-02 20:09:13 +01:00
snprintf(affichage,50,"Images/image%d.png",g);
ChargerImage(affichage,im_x,im_y,0,0,80,80);
repet++;
2022-11-30 17:23:59 +01:00
}
}
}
2022-12-01 13:53:28 +01:00
}
}
}
/*Si les deux images forment une paire*/
2022-12-01 23:17:52 +01:00
if (stock_g_1==stock_g_2){
sauv_pos[l]=position[stock_g_1-1][0];
sauv_pos[l+1]=position[stock_g_1-1][1];
sauv_pos[l+2]=position[stock_g_1-1][2];
sauv_pos[l+3]=position[stock_g_1-1][3];
2022-12-02 20:09:13 +01:00
position[stock_g_1-1][0]=0;
position[stock_g_1-1][1]=0;
position[stock_g_1-1][2]=0;
position[stock_g_1-1][3]=0;
2022-12-01 22:32:16 +01:00
score++;
l=l+4;
}
/*réaffichage des images*/
repet2=0;
2022-12-01 23:17:52 +01:00
if (score!=nb_paires){
sleep(1);
for (pos_y=10;pos_y<=370;pos_y=pos_y+90){
for (pos_x=10;pos_x<=640;pos_x=pos_x+90){
oui=0;
for (i=0;i<l;i=i+2){
if (sauv_pos[i]==pos_x && sauv_pos[i+1]==pos_y){
oui=1;
}
}
2022-12-02 20:32:05 +01:00
if (oui==0){
ChargerImage("Images/image0.png",pos_x,pos_y,0,0,80,80);
}
2022-12-02 16:25:42 +01:00
repet2++;
if (repet2==nb_images){
pos_x=750;
pos_y=380;
}
}
2022-11-30 17:23:59 +01:00
}
}
if(score2!=score){
c=CouleurParComposante(255,255,255);
ChoisirCouleurDessin(c);
2022-12-10 16:29:19 +01:00
RemplirRectangle(850,270,50,50);
snprintf(scoretab,50,"%d",score);
c=CouleurParComposante(0,0,0);
ChoisirCouleurDessin(c);
2022-12-10 16:29:19 +01:00
EcrireTexte(865,310,scoretab,2);
}
score2=score;
2022-11-30 17:23:59 +01:00
}
2022-12-10 16:29:19 +01:00
ChargerImage("./Images/FIN.png",250,460,0,0,399,87);
ChargerImage("./Images/rejouer.png",760,460,0,0,226,44);
while(1){
if(SourisCliquee()==1){
xpos=_X;
ypos=_Y;
if((xpos>=760 && xpos<=986) && (ypos>=460 && ypos<=504)){
a=rejouer();
}
if((xpos>=950 && xpos<=1002) && (ypos>=15 && ypos<=78)){
FermerGraphique();
return EXIT_SUCCESS;
}
2022-12-10 16:29:19 +01:00
}
}
FermerGraphique();
2022-12-10 16:29:19 +01:00
return EXIT_SUCCESS;
2022-11-30 17:23:59 +01:00
}