Skip to content
Snippets Groups Projects
Commit 68575b6e authored by IKHRICHI Soumaya's avatar IKHRICHI Soumaya
Browse files

modification dans le trap.c fait avec Rawan

parent 6313ccdd
Branches
Tags
No related merge requests found
No preview for this file type
...@@ -3,10 +3,10 @@ ...@@ -3,10 +3,10 @@
int main() int main()
{ {
trap_t trap1= {"the capital of france","paris"}; trap_t trap1= {"The capital of Yemen","Sanaa","Riyadh","Aden"};
trap_t trap2= {"light bulb inventor","tesla"}; //trap_t trap2= {"light bulb inventor","tesla"};
trap_t trap3={"the origine of the frensh language","latin"}; //trap_t trap3={"the origine of the frensh language","latin"};
trap_t trap4={"Who won the world cup in 2006","italy"}; //trap_t trap4={"Who won the world cup in 2006","italy"};
int nbl = 0,nbc = 0,i=0; int nbl = 0,nbc = 0,i=0;
char** map =lire_map("ressources/maze_map.txt");//lire le fichier char** map =lire_map("ressources/maze_map.txt");//lire le fichier
taille_map("ressources/maze_map.txt",&nbl,&nbc); taille_map("ressources/maze_map.txt",&nbl,&nbc);
...@@ -18,6 +18,7 @@ int main() ...@@ -18,6 +18,7 @@ int main()
init_sdl(&fenetre,&ecran,SCREENW,SCREENH); init_sdl(&fenetre,&ecran,SCREENW,SCREENH);
// Charger l’images // Charger l’images
apply_texture(walls,ecran); apply_texture(walls,ecran);
FontPosition_t fontPos=initFontPosition();
SDL_Event event; SDL_Event event;
// Boucle principale // Boucle principale
while(!player->gameover){ while(!player->gameover){
......
#include "trap.h" #include "trap.h"
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4){ FontPosition_t initFontPosition(){
FontPosition_t fontPos;
for(int i=0;i<4;i++){
fontPos.position[i].h=TRAPHT;
fontPos.position[i].w=TRAPWT;
if(i==0){
fontPos.position[i].x =60;
fontPos.position[i].y =30;
}else{
fontPos.position[i].x =60;
fontPos.position[i].y=30+fontPos.position[i].y;
}
}
//fontPos.font= TTF_OpenFont("ressources/arial.ttf",20);
return fontPos;
}
SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font, const char* text, SDL_Color color,SDL_Rect* position){
SDL_Surface *text_img = TTF_RenderText_Solid(font, text, color);
SDL_Texture *text_tex = SDL_CreateTextureFromSurface(renderer, text_img);
position->x=60;
position->y=30;
position->w=text_img->w;
position->h=text_img->h;
SDL_FreeSurface(text_img);
return text_tex;
}
/*
void initialiser_text_surf(FontPosition_t fontPos,SDL_Renderer* renderer,trap_t trap){
SDL_Color color_question={255,255,255,255};
SDL_Color color_answer={255,0,255,0};
fontPos.textSurfQ = TTF_RenderText_Solid(fontPos.font,trap.question,color_question);
fontPos.textTextureQ= SDL_CreateTextureFromSurface(renderer,fontPos.textSurfQ);
for (int i = 0; i < 3; i++)
{
fontPos.textSurfR[i] = TTF_RenderText_Solid(fontPos.font,trap.answers[i],color_answer);
fontPos.textTextureR[i]= SDL_CreateTextureFromSurface(renderer,fontPos.textSurfR[i]);
}
}*/
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3){
trap_t* traps=malloc(4*sizeof(trap_t)); trap_t* traps=malloc(4*sizeof(trap_t));
traps[0]=trap1; traps[0]=trap1;
traps[1]=trap2; traps[1]=trap2;
traps[2]=trap3; traps[2]=trap3;
traps[3]=trap4;
return traps; return traps;
} }
void showTrap(trap_t trap,player_t* player,SDL_Event* event){ void showTrap(trap_t trap,player_t* player,SDL_Event* event){
int px=0,py=0;
//int px=fontPos.position[j+1].x,py=fontPos.position[j+1].y;
//SDL_Surface* textSurfQ;
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
SDL_Window *fenetre1=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,600,SDL_WINDOW_OPENGL); SDL_Window *fenetre=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,600,SDL_WINDOW_OPENGL);
SDL_Renderer *renderer1=SDL_CreateRenderer(fenetre1,-1,0); SDL_Renderer *renderer=SDL_CreateRenderer(fenetre,-1,0);
TTF_Init(); TTF_Init();
TTF_Font* font= TTF_OpenFont("ressources/arial.ttf",20); TTF_Font* font= TTF_OpenFont("ressources/arial.ttf",24);
SDL_Color color={255,255,255,255}; SDL_Color color={255,255,255,255};
SDL_Surface* textSurf; SDL_Rect* position;
textSurf = TTF_RenderText_Solid(font,trap.question,color); SDL_Rect*position1;
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer1,textSurf); SDL_Rect*position2;
SDL_Rect textRect ={30,30,textSurf->w,textSurf->h}; //textSurfQ = TTF_RenderText_Solid(font,trap.question,color);
SDL_FreeSurface(textSurf); SDL_Texture* textTextureQ= createText(renderer,font,trap.question,color,position);
TTF_CloseFont(font); SDL_Texture* textTextureR=createText(renderer,font,trap.answers,color,position1);
SDL_Texture* textTextureR1=createText(renderer,font,trap.answers,color,position2);
//SDL_FreeSurface(textSurfQ);
//textSurfQ = TTF_RenderText_Solid(font,trap.answers,color);
//SDL_Texture* textTextureR= SDL_CreateTextureFromSurface(renderer,textSurfQ);
//SDL_FreeSurface(textSurfQ);
TTF_CloseFont(font);
bool end=false; bool end=false;
const char* reponse_j;
const char* r;
//SDL_SetRenderDrawColor(renderer1,0,0,225,225);
while(!end){ while(!end){
// apply_text(renderer1,0,0,100,200,trap.question,font);
while (SDL_PollEvent(event)) while (SDL_PollEvent(event))
{ {
if (event->type ==SDL_QUIT){ if (event->type ==SDL_QUIT){
end=true; end=true;
} }
if (event->type == SDL_KEYDOWN){ if (event->key.keysym.sym==SDLK_ESCAPE){ //si on appui sur la touche Echap, on quitte le jeu.
if (event->key.keysym.sym==SDLK_ESCAPE){ //si on appui sur la touche Echap, on quitte le jeu. end=true;
end=true; }
} if (event->type== SDL_MOUSEBUTTONDOWN){
if(event->key.keysym.sym==SDLK_KP_ENTER){ if (event->button.state==SDL_PRESSED){
if(strcmp(reponse_j,trap.answer)==0){ if(((event->button.x>=px) && (event->button.x<=( px +TRAPWT)))&&((event->button.y>=py)&&(event->button.y<=(py+TRAPHT)))){
player->score +=10; player->score += 50;
end=true; end=true;
}else{ }else{
player->lives--; player->lives--;
} }
} }
else{
r= SDL_GetKeyName(event->key.keysym.sym);
reponse_j +=*r;
textSurf = TTF_RenderText_Solid(font,reponse_j,color);
}
} }
} }
SDL_RenderCopy(renderer,textTextureQ,NULL,position);
SDL_RenderCopy(renderer1,textTexture,NULL, &textRect); SDL_RenderCopy(renderer,textTextureR,NULL,position1);
SDL_RenderPresent(renderer1); SDL_RenderCopy(renderer,textTextureR,NULL,position2);
SDL_UpdateWindowSurface(fenetre1); SDL_RenderPresent(renderer);
SDL_UpdateWindowSurface(fenetre);
} }
SDL_DestroyTexture(textTextureQ);
SDL_DestroyTexture(textTextureR);
SDL_DestroyTexture(textTextureR1);
// Quitter SDL // Quitter SDL
SDL_DestroyRenderer(renderer1); SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(fenetre1); SDL_DestroyWindow(fenetre);
SDL_Quit(); SDL_Quit();
TTF_Quit(); TTF_Quit();
} }
\ No newline at end of file
void clean_trap(trap_t* trap){
free(trap);
}
/*
void clean_FontPosition(FontPosition_t fontPos){
SDL_FreeSurface(fontPos.textSurfQ);
SDL_DestroyTexture(fontPos.textTextureQ);
SDL_FreeSurface(fontPos.textSurfR);
SDL_DestroyTexture(fontPos.textTextureR[i]);
TTF_CloseFont(fontPos.font);
}*/
...@@ -3,13 +3,33 @@ ...@@ -3,13 +3,33 @@
#include "graphics.h" #include "graphics.h"
#include "sdl2-ttf-light.h" #include "sdl2-ttf-light.h"
#define TRAPHT 60
#define TRAPWT 120
typedef struct trap_s{ typedef struct trap_s{
const char* question; const char* question;
const char* answer; const char* answers;
const char* answers1;
const char* answers2;
}trap_t; }trap_t;
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4); typedef struct FontPosition_s{
SDL_Rect position[4];
/*
SDL_Surface* textSurfQ;
SDL_Surface* textSurfR[3];
SDL_Texture* textTextureQ;
SDL_Texture* textTextureR[3];
TTF_Font* font;*/
}FontPosition_t;
FontPosition_t initFontPosition();
void initialiser_text_surf(FontPosition_t fontPos,SDL_Renderer* renderer,trap_t trap);
SDL_Texture* createText(SDL_Renderer *renderer, TTF_Font *font, const char* text, SDL_Color color,SDL_Rect* position);
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3);
void showTrap(trap_t trap,player_t* player,SDL_Event* event); void showTrap(trap_t trap,player_t* player,SDL_Event* event);
void clean_trap(trap_t* trap);
void clean_FontPosition(FontPosition_t fontPos);
//void check_Trap(player_t* player,trap_t trap,char** map,int* i,walls_t* walls); //void check_Trap(player_t* player,trap_t trap,char** map,int* i,walls_t* walls);
#endif #endif
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment