Skip to content
Snippets Groups Projects
Commit 8f71094c authored by SmallIshMink's avatar SmallIshMink
Browse files

Ajout shop, résoudre bug affichage argents, pas capable d'acheter encore

parent 6fb37b67
Branches
No related tags found
No related merge requests found
Showing with 176 additions and 23 deletions
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){
textures->bmenu = load_image( "ressources/image-menu.bmp",renderer); textures->bmenu = load_image( "ressources/image-menu.bmp",renderer);
textures->bshop = load_image( "ressources/Elements/shop.png",renderer);
textures->ship = load_image( "ressources/spaceship.bmp",renderer); textures->ship = load_image( "ressources/spaceship.bmp",renderer);
textures->background = load_image( "ressources/Elements/backgrounds/1.png",renderer); textures->background = load_image( "ressources/Elements/backgrounds/1.png",renderer);
textures->background2 = load_image( "ressources/Elements/backgrounds/3.png",renderer); textures->background2 = load_image( "ressources/Elements/backgrounds/3.png",renderer);
...@@ -20,6 +21,11 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){ ...@@ -20,6 +21,11 @@ void init_ressource(SDL_Renderer *renderer, ressources_t *textures){
textures->soleilBarre = load_image( "ressources/Elements/soleil.png",renderer); textures->soleilBarre = load_image( "ressources/Elements/soleil.png",renderer);
textures->soleil = load_image( "ressources/Elements/soleil.png",renderer); textures->soleil = load_image( "ressources/Elements/soleil.png",renderer);
textures->buy = load_image( "ressources/Elements/shop/buy.png",renderer);
textures->nomoney = load_image( "ressources/Elements/shop/nomoney.png",renderer);
textures->sell = load_image( "ressources/Elements/shop/sell.png",renderer);
textures->nb_init = 9; textures->nb_init = 9;
init_ressource_element(renderer, textures); init_ressource_element(renderer, textures);
...@@ -102,12 +108,12 @@ void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world, ...@@ -102,12 +108,12 @@ void apply_walls(SDL_Renderer * renderer, SDL_Texture *texture, world_t *world,
void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textures){
//on vide le renderer //on vide le renderer
clear_renderer(renderer); clear_renderer(renderer);
if (world->isMenu == 0){ if (world->gamestate == 0){
ingame(renderer,world,textures); ingame(renderer,world,textures);
}else if(world -> isMenu==1){ }else if(world ->gamestate==1){
inmenu(renderer,world,textures); inmenu(renderer,world,textures);
}else if(world ->gamestate==2){
inshop(renderer,world,textures);
} }
update_screen(renderer); update_screen(renderer);
...@@ -115,7 +121,30 @@ void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textu ...@@ -115,7 +121,30 @@ void refresh_graphics(SDL_Renderer *renderer, world_t *world,ressources_t *textu
} }
void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures){
apply_background(renderer, textures->bmenu, world); apply_background(renderer, textures->bmenu, world);
printf("%d\n", number_of_numbers(world->money));
apply_text(renderer, (SCREEN_WIDTH-90)-(25*number_of_numbers(world->money)), 20, 25*number_of_numbers(world->money), 50, world->coin_menu_str, textures->font, textures->color);
} }
void inshop(SDL_Renderer *renderer, world_t *world, ressources_t *textures){
apply_background(renderer, textures->bshop, world);
apply_text(renderer, 150, 150, 50, 25, "200$", textures->font, textures->color);
apply_text(renderer, 360, 145, 50, 25, "500$", textures->font, textures->color);
apply_text(renderer, 560, 140, 50, 25, "1000$", textures->font, textures->color);
apply_text(renderer, 370, 315, 50, 25, "2000$", textures->font, textures->color);
for (int i = 0; i<4; i++){
if (world->shopPrice[i] <= world->money){
apply_sprite_fixed(renderer, textures->buy, world->Spr_ship[i], world);
}else{
apply_sprite_fixed(renderer, textures->nomoney, world->Spr_ship[i], world);
}
}
apply_text(renderer, (SCREEN_WIDTH-90)-(25*number_of_numbers(world->money)), 20, 25*number_of_numbers(world->money), 50, world->coin_menu_str, textures->font, textures->color);
}
void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures){ void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures){
//application des textures dans le renderer //application des textures dans le renderer
... ...
......
...@@ -35,6 +35,7 @@ ...@@ -35,6 +35,7 @@
struct ressources_s{ struct ressources_s{
SDL_Texture* background; /*!< Texture liée à l'image du fond de l'écran. */ SDL_Texture* background; /*!< Texture liée à l'image du fond de l'écran. */
SDL_Texture* bmenu;/*!< Texture liée à l'image du fond du menu. */ SDL_Texture* bmenu;/*!< Texture liée à l'image du fond du menu. */
SDL_Texture* bshop; /*!< Texture liée à l'image du fond du gameover. */
SDL_Texture* background2; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */ SDL_Texture* background2; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */
SDL_Texture* background3; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */ SDL_Texture* background3; /*!< Texture liée à l'image du fond de l'écran. (effet parallax) */
SDL_Texture* ship; /*!< Texture liée à l'image du vaisseau. */ SDL_Texture* ship; /*!< Texture liée à l'image du vaisseau. */
...@@ -49,6 +50,10 @@ struct ressources_s{ ...@@ -49,6 +50,10 @@ struct ressources_s{
SDL_Texture* soleilBarre; /*!< Texture liée à l'image du soleil. */ SDL_Texture* soleilBarre; /*!< Texture liée à l'image du soleil. */
SDL_Texture* soleil; /*!< Texture liée à l'image du soleil. */ SDL_Texture* soleil; /*!< Texture liée à l'image du soleil. */
SDL_Texture* buy; /*!< Texture liée à l'image du bouton buy. */
SDL_Texture* nomoney; /*!< Texture liée à l'image du bouton nomoney. */
SDL_Texture* sell; /*!< Texture liée à l'image du bouton sell. */
TTF_Font *font; // Font TTF_Font *font; // Font
SDL_Color color; // Color SDL_Color color; // Color
long double angle; /*!< Angle de rotation de l'image. */ long double angle; /*!< Angle de rotation de l'image. */
...@@ -149,8 +154,30 @@ void clean_textures(ressources_t *textures); ...@@ -149,8 +154,30 @@ void clean_textures(ressources_t *textures);
*/ */
void clean(SDL_Window *window, SDL_Renderer * renderer, ressources_t *textures, world_t * world); void clean(SDL_Window *window, SDL_Renderer * renderer, ressources_t *textures, world_t * world);
/**
* @brief La fonction qui affiche le jeu (en jeu)
*
* @param renderer
* @param world
* @param textures
*/
void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures); void ingame(SDL_Renderer *renderer, world_t *world,ressources_t *textures);
/**
* @brief La fonction qui affiche le jeu (en menu)
*
* @param renderer
* @param world
* @param textures
*/
void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures); void inmenu(SDL_Renderer *renderer, world_t *world,ressources_t *textures);
/**
* @brief La fonction qui affiche le jeu (en magasin)
*
* @param renderer
* @param world
* @param textures
*/
void inshop(SDL_Renderer *renderer, world_t *world, ressources_t *textures);
#endif #endif
\ No newline at end of file
No preview for this file type
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
#include "../utility/utility.h" #include "../utility/utility.h"
void update_data(world_t *world){ void update_data(world_t *world){
if (world->isMenu==0){ if (world->gamestate==0){
world->ligneArriver->y += (int)world->speed_h; world->ligneArriver->y += (int)world->speed_h;
world->soleil->y += (int)world->speed_h; world->soleil->y += (int)world->speed_h;
...@@ -27,8 +27,10 @@ void update_data(world_t *world){ ...@@ -27,8 +27,10 @@ void update_data(world_t *world){
} }
allEvents(world); allEvents(world);
world->timer = SDL_GetTicks() - world->startTimer;; world->timer = SDL_GetTicks() - world->startTimer;;
}else if(world->isMenu == 1){ }else if(world->gamestate == 1){
world->startTimer = SDL_GetTicks(); world->startTimer = SDL_GetTicks();
}else if(world->gamestate == 2){
printf("%d\n", world->money);
} }
} }
...@@ -38,8 +40,14 @@ void init_data(world_t * world){ ...@@ -38,8 +40,14 @@ void init_data(world_t * world){
world->speed_h = (float)INITIAL_SPEED; world->speed_h = (float)INITIAL_SPEED;
init_walls(world); init_walls(world);
world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0); world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0);
InitMenu(world);
print_sprite(world->vaisseau); world->money = 2000; // Initialisation de l'argent ici car necessaire dans init_menu
init_menu(world);
init_shop(world);
printf("test\n");
// Initialisation du vaisseau // Initialisation du vaisseau
world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE-15, SHIP_SIZE, SHIP_SIZE, '0', 0); world->vaisseau = init_sprite(world->vaisseau, SCREEN_WIDTH/2 - SHIP_SIZE/2, SCREEN_HEIGHT - SHIP_SIZE-15, SHIP_SIZE, SHIP_SIZE, '0', 0);
world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_REAL_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0); world->ligneArriver = init_sprite(world->ligneArriver, 0, -world->nb_lines_murs*METEORITE_REAL_SIZE-30 , SCREEN_WIDTH, FINISH_LINE_HEIGHT, 'z', 0);
...@@ -54,24 +62,25 @@ void init_data(world_t * world){ ...@@ -54,24 +62,25 @@ void init_data(world_t * world){
world->vaisseauMini->dy = SCREEN_HEIGHT - (110); world->vaisseauMini->dy = SCREEN_HEIGHT - (110);
world->timer = SDL_GetTicks(); world->timer = SDL_GetTicks();
world->temps_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string temps world->temps_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string temps
world->coins_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string coins world->coins_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string coins
world->coins_str[0] = '\0'; world->coins_str[0] = '\0';
world->coins_str = strcats(world->coins_str, 1, "0"); world->coins_str = strcats(world->coins_str, 1, "0");
world->angle = 0.0; world->angle = 0.0;
world->isFlipping = 0; world->isFlipping = 0;
world->isMenu = 1; world->gamestate = 1;
world->mouseX = 0; world->mouseX = 0;
world->mouseY = 0; world->mouseY = 0;
world->money = 0;
world->parallax = 0; world->parallax = 0;
world->invicibility = false; world->invicibility = false;
}
print_sprite(world->vaisseau);
}
void outBorder(world_t *world){ void outBorder(world_t *world){
if (isOverScreen(world->vaisseau)){ if (isOverScreen(world->vaisseau)){
...@@ -93,13 +102,35 @@ int timer_update_s(world_t *world){ ...@@ -93,13 +102,35 @@ int timer_update_s(world_t *world){
return 0; return 0;
} }
void InitMenu(world_t * world){ void init_menu(world_t * world){
world->play = init_btn(203,129, 218, 97); world->play = init_btn(203,129, 218, 97);
world->exit = init_btn(202,294, 218, 97); world->exit = init_btn(202,294, 218, 97);
world->magasin = init_btn(436, 217, 218, 97); world->magasin = init_btn(436, 217, 218, 97);
world->sound = init_btn(0, 0, 100, 100); world->sound = init_btn(0, 0, 100, 100);
world->coin_menu_str = malloc(sizeof(char)*100); // Allocation de la mémoire pour le string coins
world->coin_menu_str[0] = '\0';
world->coin_menu_str = strcats(world->coin_menu_str, 1, int_to_str(world->money));
} }
void init_shop(world_t * world){
world->ship1 = init_btn(74,145, 191, 139);
world->ship2 = init_btn(287,143, 191, 139);
world->ship3 = init_btn(491,138, 191, 139);
world->ship4 = init_btn(290,308, 191, 139);
world->Spr_ship = malloc(sizeof(sprite_t*)*4);
world->Spr_ship[0] = init_sprite(world->Spr_ship[0], 74,145, 191, 139, 'x', 0);
world->Spr_ship[1] = init_sprite(world->Spr_ship[1], 287,143, 191, 139, 'x', 0);
world->Spr_ship[2] = init_sprite(world->Spr_ship[2], 491,138, 191, 139, 'x', 0);
world->Spr_ship[3] = init_sprite(world->Spr_ship[3], 290,308, 191, 139, 'x', 0);
world->shopPrice = malloc(sizeof(int)*4);
world->shopPrice[0] = 200;
world->shopPrice[1] = 500;
world->shopPrice[2] = 1000;
world->shopPrice[3] = 2000;
}
void collide(sprite_t *sp1, sprite_t *sp2, world_t *world){ void collide(sprite_t *sp1, sprite_t *sp2, world_t *world){
if (sprites_collide(sp1, sp2)){ if (sprites_collide(sp1, sp2)){
...@@ -204,6 +235,13 @@ void clean_data(world_t *world){ ...@@ -204,6 +235,13 @@ void clean_data(world_t *world){
free(world->exit); free(world->exit);
free(world->magasin); free(world->magasin);
free(world->sound); free(world->sound);
free(world->ship1);
free(world->ship2);
free(world->ship3);
free(world->ship4);
free(world->Spr_ship);
free(world->shopPrice);
free(world->coin_menu_str);
printf("clean_data"); printf("clean_data");
} }
\ No newline at end of file
...@@ -44,12 +44,20 @@ struct world_s{ ...@@ -44,12 +44,20 @@ struct world_s{
btn_t *magasin; btn_t *magasin;
btn_t *sound; btn_t *sound;
btn_t *ship1;
btn_t *ship2;
btn_t *ship3;
btn_t *ship4;
sprite_t *BarreProgression; sprite_t *BarreProgression;
sprite_t *vaisseauMini; sprite_t *vaisseauMini;
sprite_t *soleilBarre; sprite_t *soleilBarre;
sprite_t *soleil; sprite_t *soleil;
sprite_t *air; sprite_t *air;
sprite_t *coins; sprite_t *coins;
sprite_t **Spr_ship;
int *shopPrice;
int nb_murs; // Nombre de météorites int nb_murs; // Nombre de météorites
int nb_lines_murs; // Nombre de lignes de météorites int nb_lines_murs; // Nombre de lignes de météorites
sprite_t *ligneArriver; sprite_t *ligneArriver;
...@@ -59,10 +67,12 @@ struct world_s{ ...@@ -59,10 +67,12 @@ struct world_s{
unsigned int timer; /*!< Timer de jeu */ unsigned int timer; /*!< Timer de jeu */
char * temps_str; // String affichant le temps sur le jeu char * temps_str; // String affichant le temps sur le jeu
char * coins_str; char * coins_str;
char * coin_menu_str;
double angle; // Angle de rotation de la map double angle; // Angle de rotation de la map
int isFlipping; // Indique si l'on est en train de faire une rotation de l'écran et dans quelle sens (0 : non droite, 1 : vers la droite, -1 : vers la gauche, -2 : non gauche) int isFlipping; // Indique si l'on est en train de faire une rotation de l'écran et dans quelle sens (0 : non droite, 1 : vers la droite, -1 : vers la gauche, -2 : non gauche)
int isMenu; int gamestate;
int mouseX; int mouseX;
int mouseY; int mouseY;
...@@ -71,6 +81,8 @@ struct world_s{ ...@@ -71,6 +81,8 @@ struct world_s{
int parallax; int parallax;
bool invicibility; bool invicibility;
}; };
typedef struct world_s world_t; typedef struct world_s world_t;
...@@ -168,5 +180,12 @@ void allEvents(world_t *world); ...@@ -168,5 +180,12 @@ void allEvents(world_t *world);
* *
* @param world * @param world
*/ */
void InitMenu(world_t *world); void init_menu(world_t *world);
/**
* @brief La fonction qui initialise le shop
*
* @param world
*/
void init_shop(world_t * world);
#endif #endif
No preview for this file type
...@@ -44,6 +44,9 @@ char **lirefile(char *filename, int *num_lines) { ...@@ -44,6 +44,9 @@ char **lirefile(char *filename, int *num_lines) {
} }
int number_of_numbers(int n){ int number_of_numbers(int n){
if (n == 0){
return 1;
}
int count = 0; int count = 0;
while (n != 0){ while (n != 0){
n /= 10; n /= 10;
...@@ -51,3 +54,8 @@ int number_of_numbers(int n){ ...@@ -51,3 +54,8 @@ int number_of_numbers(int n){
} }
return count; return count;
} }
void modify_str(char *str, char *str2){
str[0] = '\0';
strcat(str, str2);
}
\ No newline at end of file
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
*/ */
char * strcats(char* dest, int num_args, ...); char * strcats(char* dest, int num_args, ...);
void modify_str(char *str, char *str2);
/** /**
* \brief Convertit un entier en chaine de caractère * \brief Convertit un entier en chaine de caractère
* *
...@@ -42,5 +43,11 @@ char * int_to_str(int n); ...@@ -42,5 +43,11 @@ char * int_to_str(int n);
char ** lirefile(char *filename, int *num_lines); char ** lirefile(char *filename, int *num_lines);
/**
* @brief Retourne le nombre de chiffres d'un entier
*
* @param n
* @return int
*/
int number_of_numbers(int n); int number_of_numbers(int n);
#endif #endif
\ No newline at end of file
No preview for this file type
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
void handle_events(SDL_Event *event,world_t *world){ void handle_events(SDL_Event *event,world_t *world){
const Uint8 *keystates = SDL_GetKeyboardState(NULL); const Uint8 *keystates = SDL_GetKeyboardState(NULL);
if (!world->isMenu){ if (world->gamestate == 0){
if (keystates[SDL_SCANCODE_A]){ if (keystates[SDL_SCANCODE_A]){
world->vaisseau->x -= MOVING_STEP; world->vaisseau->x -= MOVING_STEP;
} }
...@@ -46,7 +46,7 @@ void handle_events(SDL_Event *event,world_t *world){ ...@@ -46,7 +46,7 @@ void handle_events(SDL_Event *event,world_t *world){
//si une touche est appuyée //si une touche est appuyée
if(event->type == SDL_KEYDOWN){ if(event->type == SDL_KEYDOWN){
//si la touche appuyée est 'D' //si la touche appuyée est 'D'
if (!world->isMenu){ if (!world->gamestate){
switch (event->key.keysym.sym){ switch (event->key.keysym.sym){
case SDLK_z: case SDLK_z:
world->speed_h = 4; world->speed_h = 4;
...@@ -65,23 +65,46 @@ void handle_events(SDL_Event *event,world_t *world){ ...@@ -65,23 +65,46 @@ void handle_events(SDL_Event *event,world_t *world){
// print_sprite(world->vaisseau); // print_sprite(world->vaisseau);
} }
if (event->type == SDL_MOUSEBUTTONDOWN){ if (event->type == SDL_MOUSEBUTTONDOWN){
printf("aaa"); if (world->gamestate == 1){
if (world->isMenu){
if (event->button.button == SDL_BUTTON_LEFT){ if (event->button.button == SDL_BUTTON_LEFT){
printf("mouse(%d, %d) world rect (%d %d %d %d)\n", world->mouseX, world->mouseY, world->play->rect.x,world->play->rect.y,world->play->rect.w,world->play->rect.h ); printf("mouse(%d, %d) world rect (%d %d %d %d)\n", world->mouseX, world->mouseY, world->play->rect.x,world->play->rect.y,world->play->rect.w,world->play->rect.h );
if (collidePoint(world->play, world->mouseX, world->mouseY)){ if (collidePoint(world->play, world->mouseX, world->mouseY)){
printf("tesy"); printf("tesy");
world->isMenu = false; world->gamestate = 0;
} }
if (collidePoint(world->magasin, world->mouseX, world->mouseY)){ if (collidePoint(world->magasin, world->mouseX, world->mouseY)){
printf("tesy"); printf("tesy");
world->isMenu = false; world->gamestate = 2;
} }
if (collidePoint(world->exit, world->mouseX, world->mouseY)){ if (collidePoint(world->exit, world->mouseX, world->mouseY)){
printf("tesy"); printf("tesy");
world->gameover = true; world->gameover = true;
} }
} }
}else if(world->gamestate == 2){
if (event->button.button == SDL_BUTTON_LEFT){
if (collidePoint(world->ship1, world->mouseX, world->mouseY)){
printf("ship1");
world->money -= world->shopPrice[0];
modify_str(world->coins_str, int_to_str(world->money));
printf("%s\n", world->coins_str);
}
if (collidePoint(world->ship2, world->mouseX, world->mouseY)){
printf("ship2");
world->money -= world->shopPrice[1];
modify_str(world->coins_str, int_to_str(world->money));
}
if (collidePoint(world->ship3, world->mouseX, world->mouseY)){
printf("ship3");
world->money -= world->shopPrice[2];
modify_str(world->coins_str, int_to_str(world->money));
}
if (collidePoint(world->ship4, world->mouseX, world->mouseY)){
printf("ship4");
world->money -= world->shopPrice[3];
modify_str(world->coins_str, int_to_str(world->money));
}
}
} }
} }
} }
...@@ -103,9 +126,11 @@ void handle_events(SDL_Event *event,world_t *world){ ...@@ -103,9 +126,11 @@ void handle_events(SDL_Event *event,world_t *world){
void init(SDL_Window **window, SDL_Renderer ** renderer, ressources_t *textures, world_t * world){ void init(SDL_Window **window, SDL_Renderer ** renderer, ressources_t *textures, world_t * world){
init_sdl(window,renderer,SCREEN_WIDTH, SCREEN_HEIGHT); init_sdl(window,renderer,SCREEN_WIDTH, SCREEN_HEIGHT);
init_data(world); init_data(world);
// Initialisation du ttf // Initialisation du ttf
init_ttf(); init_ttf();
init_ressource(*renderer,textures); init_ressource(*renderer,textures);
} }
... ...
......
No preview for this file type
ressources/Elements/shop.png

84.5 KiB

ressources/Elements/shop/buy.png

4 KiB

ressources/Elements/shop/nomoney.png

4.21 KiB

ressources/Elements/shop/sell.png

4.24 KiB

No preview for this file type
...@@ -136,7 +136,7 @@ int test_allEvents(world_t *world){ ...@@ -136,7 +136,7 @@ int test_allEvents(world_t *world){
} }
int test_InitMenu(world_t *world){ int test_InitMenu(world_t *world){
InitMenu(world); init_menu(world);
return 1; // Pas d'erreur return 1; // Pas d'erreur
} }
... ...
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment