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

modification dans le trap.c fait avec Rawan

parent 8d2b9775
Branches main
No related tags found
No related merge requests found
CC = gcc
CFLAGS = -W -Wall -ansi -std=c99 -g
CFLAGS = -W -Wall -ansi -std=c99 -g
LIBS = -L /SDL2_ttf/.libs -L /SDL2_image/.libs
LDFLAGS = `sdl2-config --cflags --libs`
LDFLAGS = `sdl2-config --cflags --libs` -lSDL2_ttf
INCLUDES = -I ./SDL2_ttf -I ./SDL2_image
EXEC = main
SRC = main.c player.c map.c graphics.c
SRC = main.c player.c map.c graphics.c trap.c sdl2-ttf-light.c
OBJ = $(SRC:.c=.o)
all: $(EXEC)
......
......@@ -46,10 +46,10 @@ void apply_texture(walls_t* walls,SDL_Renderer* ecran){
}
void init_wall(walls_t* walls,int px,int py,int postx,int posty){
walls->bricks.w=BRICKW;
walls->bricks.h=BRICKH;
walls->bricks.x=BRICKW*postx;
walls->bricks.y=BRICKH*posty;
walls->DestR.w=BRICKW;
walls->DestR.h=BRICKH;
walls->DestR.x=BRICKW*postx;
walls->DestR.y=BRICKH*posty;
walls->Src.x=BRICKW*px;
walls->Src.y=BRICKH*py;
walls->Src.w=BRICKW;
......@@ -62,26 +62,52 @@ void affichage_bricks(SDL_Renderer* ecran,walls_t* walls,char** map,int nbc,int
{
if(map[i][j]=='L'){
init_wall(walls,j,i,9,3);
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src));
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src));
}
else if(map[i][j]=='#'){
init_wall(walls,j,i,10,3);
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src));
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src));
}
else if (map[i][j]=='S'){
init_wall(walls,j,i,13,3);
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->bricks),&(walls->Src));
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src));
}
else if (map[i][j]=='T'){
init_wall(walls,j,i,13,4);
SDL_RenderCopy(ecran,walls->texture_brick,&(walls->DestR),&(walls->Src));
}
}
}
}
void handle_events(SDL_Event *event, player_t* player,char** map){
void affichage_joueur(player_t* player,walls_t* walls,SDL_Renderer* ecran){
walls->DestR.h=PLAYERH;
walls->DestR.w=PLAYERW;
walls->DestR.x=PLAYERW*player->posc;
walls->DestR.y=PLAYERH*player->posl;
walls->Src.w=PLAYERW;
walls->Src.h=PLAYERH;
walls->Src.x=PLAYERW*player->x;
walls->Src.y=PLAYERH*player->y;
SDL_RenderCopy(ecran,walls->texture_joueur,&(walls->DestR),&(walls->Src));
}
void change_movement_player(player_t* player,walls_t* walls,int posc,int posl){
player->posc=posc;
player->posl=posl;
walls->DestR.x=PLAYERW*player->posc;
walls->DestR.y=PLAYERH*player->posl;
walls->Src.x=PLAYERW*player->x;
walls->Src.y=PLAYERH*player->y;
}
void handle_events(SDL_Event *event,walls_t* walls,player_t* player,char** map){
while (SDL_PollEvent(event)){
if (event->type ==SDL_QUIT){
player->gameover=1;
}
//si une touche est appuyée
if(event->type == SDL_KEYDOWN){
if (event->key.keysym.sym==SDLK_ESCAPE){ //si on appui sur la touche Echap, on quitte le jeu.
......@@ -89,28 +115,40 @@ void handle_events(SDL_Event *event, player_t* player,char** map){
}
if(event->key.keysym.sym == SDLK_LEFT){
movement(map,player,'q'); // fléche vers la gauche
change_movement_player(player,0,1);
change_movement_player(player,1,1);
change_movement_player(player,walls,1,1);
}
if(event->key.keysym.sym == SDLK_RIGHT){
movement(map,player,'d'); // fléche vers la droite
change_movement_player(player,0,2);
change_movement_player(player,1,2);
change_movement_player(player,walls,1,2);
}
if(event->key.keysym.sym == SDLK_UP){
movement(map,player,'z');// fléche vers le haut
change_movement_player(player,0,3);
change_movement_player(player,1,3);
change_movement_player(player,walls,1,3);
}
if(event->key.keysym.sym == SDLK_DOWN){
movement(map,player,'s');// fléche vers le bas
change_movement_player(player,0,0);
change_movement_player(player,1,0);
change_movement_player(player,walls,1,0);
}
}
}
}
/*
void apply_text(SDL_Renderer* ecran,int x,int y,int width,int height,const char* message){
TTF_Font* font=TTF_OpenFont("ressources/angelina.TTF",14);
SDL_Color white={255,255,255,255};
SDL_Surface* surfaceText=TTF_RenderText_Solid(font,message,white);
SDL_Texture* text=SDL_CreateTextureFromSurface(ecran,surfaceText);
SDL_Rect textRect;
textRect.h=height;
textRect.w=width;
textRect.x=x;
textRect.y=y;
TTF_CloseFont(font);
SDL_RenderCopy(ecran,text,NULL,&textRect);
SDL_FreeSurface(surfaceText);
SDL_DestroyTexture(text);
}*/
void clean_walls(walls_t* walls){
SDL_DestroyTexture(walls->texture_joueur);
......
......@@ -10,7 +10,7 @@
typedef struct walls_s
{
SDL_Rect bricks;
SDL_Rect DestR;
SDL_Rect Src;
SDL_Texture* texture_joueur;
SDL_Texture* texture_brick;
......@@ -22,6 +22,9 @@ SDL_Texture* charger_image (const char* nomfichier, SDL_Renderer* renderer);
void apply_texture(walls_t* walls,SDL_Renderer* ecran);
void init_wall(walls_t* walls,int px,int py,int postx,int posty);
void affichage_bricks(SDL_Renderer* ecran,walls_t* walls,char** map,int nbc,int nbl);
void handle_events(SDL_Event *event,player_t* player,char** map);
void affichage_joueur(player_t* player,walls_t* walls,SDL_Renderer* ecran);
void change_movement_player(player_t* player,walls_t* walls,int posc,int posl);
void handle_events(SDL_Event *event,walls_t* walls,player_t* player,char** map);
//void apply_text(SDL_Renderer* ecran,int x,int y,int width,int height,const char* message);
void clean_walls(walls_t* walls);
#endif
\ No newline at end of file
No preview for this file type
#include "graphics.h"
#include "trap.h"
int main()
{
int nbl = 0,nbc = 0;
trap_t trap1= {"the capital of france","paris"};
trap_t trap2= {"light bulb inventor","tesla"};
trap_t trap3={"the origine of the frensh language","latin"};
trap_t trap4={"Who won the world cup in 2006","italy"};
int nbl = 0,nbc = 0,i=0;
char** map =lire_map("ressources/maze_map.txt");//lire le fichier
taille_map("ressources/maze_map.txt",&nbl,&nbc);
player_t*player = init_player(0,2);//initialisation du joueur
......@@ -17,11 +21,15 @@ int main()
SDL_Event event;
// Boucle principale
while(!player->gameover){
handle_events(&event,player,map);
gamrover(player,map,nbc,nbl);
if (map[player->y][player->x]=='T'){
showTrap(trap1,player,&event);
map[player->y][player->x]='L';
}
handle_events(&event,walls,player,map);
gameover(player,map,nbc,nbl);
SDL_RenderClear(ecran);
affichage_bricks(ecran,walls,map,nbc,nbl);
SDL_RenderCopy(ecran,walls->texture_joueur,&(player->DestR),&(player->Src));
affichage_joueur(player,walls,ecran);
SDL_RenderPresent(ecran);
SDL_UpdateWindowSurface(fenetre);
}
......
......@@ -5,6 +5,8 @@
#include <stdlib.h>
#include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
char** allouer_tab_2D(int n,int m);
......
......@@ -5,30 +5,15 @@ player_t* init_player(int posc,int posl){
player->x=1;
player->y=1;
player->score=0;
player->height=PLAYERH;
player->width=PLAYERW;
player->DestR.h=PLAYERH;
player->DestR.w=PLAYERW;
player->DestR.x=PLAYERW*posc;
player->DestR.y=PLAYERH*posl;
player->Src.w=PLAYERW;
player->Src.h=PLAYERH;
player->Src.x=PLAYERW*player->x;
player->Src.y=PLAYERH*player->y;
player->posc=posc;
player->posl=posl;
player->lives=3;
player->gameover=0;
return player;
}
void change_movement_player(player_t* player,int posc,int posl){
player->DestR.x=PLAYERW*posc;
player->DestR.y=PLAYERH*posl;
player->Src.x=PLAYERW*player->x;
player->Src.y=PLAYERH*player->y;
}
void gamrover(player_t* player,char** tab, int nbc,int nbl){
void gameover(player_t* player,char** tab, int nbc,int nbl){
for (int i = 0; i<nbl ; i++)
{
for (int j = 0; j<nbc; j++)
......@@ -40,11 +25,14 @@ void gamrover(player_t* player,char** tab, int nbc,int nbl){
}
}
}
if (player->lives==0){
player->gameover=1;
}
}
bool handle_movement_up(char** tab,player_t*player)
{
if (tab[player->y-1][player->x]=='L'|| tab[player->y-1][player->x]=='S'){
if (tab[player->y-1][player->x]=='L'|| tab[player->y-1][player->x]=='S' || tab[player->y-1][player->x]=='T'){
return true;
}
else{
......@@ -54,7 +42,7 @@ bool handle_movement_up(char** tab,player_t*player)
bool handle_movement_down(char** tab,player_t*player)
{
if (tab[player->y+1][player->x]=='L'|| tab[player->y+1][player->x]=='S'){
if (tab[player->y+1][player->x]=='L'|| tab[player->y+1][player->x]=='S'|| tab[player->y+1][player->x]=='T'){
return true;
}
else{
......@@ -64,7 +52,7 @@ bool handle_movement_down(char** tab,player_t*player)
}
bool handle_movement_left(char** tab,player_t*player)
{
if (tab[player->y][player->x-1]=='L'|| tab[player->y][player->x-1]=='S'){
if (tab[player->y][player->x-1]=='L'|| tab[player->y][player->x-1]=='S'||tab[player->y][player->x-1]=='T'){
return true;
}
else{
......@@ -73,7 +61,7 @@ bool handle_movement_left(char** tab,player_t*player)
}
bool handle_movement_right(char** tab,player_t*player)
{
if (tab[player->y][player->x+1]=='L'||tab[player->y][player->x+1]=='S'){
if (tab[player->y][player->x+1]=='L'||tab[player->y][player->x+1]=='S'||tab[player->y][player->x+1]=='T'){
return true;
}
else{
......
......@@ -10,24 +10,21 @@ typedef struct player_s
{
int x;
int y;
int width;
int height;
int posc;
int posl;
int score;
SDL_Rect DestR;
SDL_Rect Src;
int lives;
int gameover;
}player_t;
player_t* init_player(int posc,int posl);
void change_movement_player(player_t* player,int posc,int posl);
bool handle_movement_up(char** tab,player_t*player);
bool handle_movement_down(char** tab,player_t*player);
bool handle_movement_left(char** tab,player_t*player);
bool handle_movement_right(char** tab,player_t*player);
bool handle_mouvement_trap(char** tab,player_t* player);
void movement(char** tab,player_t *player,char deplacement);
void gamrover(player_t* player,char** tab, int nbc,int nbl);
void gameover(player_t* player,char** tab, int nbc,int nbl);
void free_player(player_t* player);
#endif
\ No newline at end of file
ressources/bg_trap.bmp

938 KiB

###########################################
#LLLLLLLLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLL#
#LTLLLLLLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLL#
#L#########################L#########L###L#
#L#LLLLLLLLLLLLLLLLLLLLLLLLL#############L#
#L#L#####################################L#
......@@ -13,11 +13,11 @@
#L##L###############################L####L#
#L##L#L######L######################LLL##L#
#L###########L#####L##L#####LLL####LL####L#
#L###########L#####L##L###########LL#####L#
#T###########L#####L##L###########LL#####L#
#L#################L##L##LLLLLLLLLLLLLL##L#
#L#################L##LLLL######L###L####L#
#L#################L##LLLL######L###T####L#
#L#################L##L#LL######L###S####L#
#L#LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL#L#
#L#LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLTLLL#L#
#L############L##########################L#
#LLLLLLLLLLLLLLLLLLL#LLLLLLLLLLLLLLLLLLLLL#
###########################################
......
ressources/ok_button.bmp

9.43 KiB

File added
#include "sdl2-ttf-light.h"
void init_ttf(){
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
}
}
TTF_Font * load_font(const char *path, int font_size){
TTF_Font *font = TTF_OpenFont(path, font_size);
if(font == NULL){
fprintf(stderr, "Erreur pendant chargement font: %s\n", SDL_GetError());
}
return font;
}
void apply_text(SDL_Renderer *renderer,int x, int y, int w, int h, const char *text, TTF_Font *font){
SDL_Color color = { 255, 0, 255 ,0};
SDL_Surface* surface = TTF_RenderText_Solid(font, text, color);
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect dstrect2 = {x, y, w, h};
SDL_RenderCopy(renderer, texture, NULL, &dstrect2);
}
void clean_font(TTF_Font * font){
TTF_CloseFont(font);
}
#ifndef __SDL2_TTF_LIGHT__H
#define __SDL2_TTF_LIGHT__H
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
/**
* \brief La fonction initialise l'environnement TTF
*/
void init_ttf();
/**
* \brief La fonction charge une police
* \param path le chemin du fichier correpondant à la police
* \param font_size la taille de la police
* \return la police chargée
*/
TTF_Font * load_font(const char* path, int font_size);
/**
* \brief La fonction applique un texte dans une certaine police sur le renderer à une certaine position et avec une certaine dimension
* \param renderer le renderer
* \param x abscisse du coin en haut à gauche du texte
* \param y son abscisse
* \param w la largeur du message
* \param h sa hauteur
* \param text le texte à afficher
* \param font la police
*/
void apply_text(SDL_Renderer *renderer,int x, int y, int w, int h, const char *text, TTF_Font *font);
/**
* \brief La fonction nettoie une police en mémoire
* \param font la police
*/
void clean_font(TTF_Font * font);
#endif
#include "trap.h"
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4){
trap_t* traps=malloc(4*sizeof(trap_t));
traps[0]=trap1;
traps[1]=trap2;
traps[2]=trap3;
traps[3]=trap4;
return traps;
}
void showTrap(trap_t trap,player_t* player,SDL_Event* event){
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* fenetre=SDL_CreateWindow("Trap",SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,400,600,SDL_WINDOW_OPENGL);
SDL_Renderer* renderer=SDL_CreateRenderer(fenetre,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
TTF_Font* font;
init_ttf();
font=load_font("ressources/only.ttf",14);
bool end=false;
const char* reponse_j;
const char* r;
SDL_SetRenderDrawColor(renderer,255,0,0,255);
while(!end){
apply_text(renderer,0,0,100,200,"trap.question",font);
while (SDL_PollEvent(event))
{
if (event->type ==SDL_QUIT){
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.
end=true;
}
if(event->key.keysym.sym==SDLK_KP_ENTER){
if(strcmp(reponse_j,trap.answer)==0){
player->score +=10;
end=true;
}else{
player->lives--;
}
}
else{
r= SDL_GetKeyName(event->key.keysym.sym);
reponse_j +=*r;
apply_text(renderer,0,201,100,200,reponse_j,font);
}
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
// Quitter SDL
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(fenetre);
clean_font(font);
SDL_Quit();
}
\ No newline at end of file
#ifndef TRAP_H
#define TRAP_H
#include "graphics.h"
#include "sdl2-ttf-light.h"
typedef struct trap_s{
const char* question;
const char* answer;
}trap_t;
trap_t* init_trap(trap_t trap1,trap_t trap2,trap_t trap3,trap_t trap4);
void showTrap(trap_t trap,player_t* player,SDL_Event* event);
//void check_Trap(player_t* player,trap_t trap,char** map,int* i,walls_t* walls);
#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