Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit 8256f7d

Browse files
authored
Beta2 to main (#53)
2 parents d00c5c5 + 8a4795a commit 8256f7d

33 files changed

+292
-16
lines changed

assets/audio/congratulation.mp3

59.7 KB
Binary file not shown.

assets/audio/wrong.mp3

32 KB
Binary file not shown.

assets/font/Skatchboard.ttf

94.1 KB
Binary file not shown.

assets/img/animation/Layer 1.png

612 Bytes
Loading

assets/img/animation/Layer 10.png

603 Bytes
Loading

assets/img/animation/Layer 11.png

591 Bytes
Loading

assets/img/animation/Layer 12.png

604 Bytes
Loading

assets/img/animation/Layer 13.png

611 Bytes
Loading

assets/img/animation/Layer 14.png

608 Bytes
Loading

assets/img/animation/Layer 15.png

592 Bytes
Loading

assets/img/animation/Layer 16.png

610 Bytes
Loading

assets/img/animation/Layer 2.png

607 Bytes
Loading

assets/img/animation/Layer 3.png

596 Bytes
Loading

assets/img/animation/Layer 4.png

608 Bytes
Loading

assets/img/animation/Layer 5.png

615 Bytes
Loading

assets/img/animation/Layer 6.png

608 Bytes
Loading

assets/img/animation/Layer 7.png

593 Bytes
Loading

assets/img/animation/Layer 8.png

611 Bytes
Loading

assets/img/animation/Layer 9.png

615 Bytes
Loading

assets/img/background_enigme.png

169 KB
Loading
178 KB
Loading

assets/img/bg.jpg

124 KB
Loading

assets/img/sand_watch.png

7.52 KB
Loading

assets/text/enigme.txt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
1_What_was_the_source_of_wealth_in_Carthage?_:
2+
.Agriculture_;
3+
.Trade_;
4+
.Mining_;
5+
.no_idea_;
6+
2_Who_was_the_most_famous_Ottoman_Sultan_?_:
7+
.Suleiman_;
8+
.Mehmed_;
9+
.Osman_I_;
10+
.no_idea_;
11+
3_When_did_Tunisia_Gain_the_independence_?_:
12+
.1952_;
13+
.1956_;
14+
.1962_;
15+
.no_idea_;
16+
4_What_was_the_language_of_Islamic_Empire_?_:
17+
.Turkish_;
18+
.Persian_;
19+
.Arabic_;
20+
.no_idea_;
21+
5_Who_was_the_founder_of_Carthage_?_:
22+
._Hannibal_;
23+
._Dido_;
24+
._Mago_;
25+
.no_idea_;
26+

assets/text/last_score.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
140

build/makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CC = gcc
22
CFLAGS = -Wall -I../include
33
LDFLAGS = -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf
4-
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o enemy.o
4+
OBJS = main.o menu.o music.o text.o stars.o settings.o start.o enemy.o enigme_fichier.o bounding_box.o
55

66
game: $(OBJS)
77
$(CC) $(OBJS) $(LDFLAGS) -o game

include/bounding_box.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef _BOUNDING_BOX_H_
2+
#define _BOUNDING_BOX_H_
3+
#include <SDL/SDL.h>
4+
5+
int in_bounds(SDL_Rect x1,SDL_Rect x2);
6+
int collision(SDL_Rect x1,SDL_Rect x2);
7+
8+
#endif

include/enigme.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef ENIGME_H_INCLUDED
2+
3+
#define ENIGME_H_INCLUDED
4+
5+
#include <SDL/SDL_ttf.h>
6+
7+
#include <SDL/SDL.h>
8+
9+
10+
11+
typedef struct {
12+
13+
char question[100];
14+
15+
char reponse[100];
16+
17+
char reponse2[100];
18+
19+
char reponse3[100];
20+
21+
SDL_Rect questionLocation;
22+
23+
SDL_Rect reponseLocation;
24+
25+
SDL_Rect textLocation;
26+
27+
TTF_Font *font;
28+
29+
SDL_Surface* screen;
30+
31+
SDL_Color textColor;
32+
33+
SDL_Color textColor_GREEN;
34+
35+
SDL_Color textColor_YELLOW ;
36+
37+
SDL_Color textColor_RED;
38+
39+
} Enigme;
40+
41+
42+
43+
void afficherEnigme(Enigme e, SDL_Surface* screen, TTF_Font* font, SDL_Color textColor);
44+
45+
void genererEnigme(Enigme* p, char* nomfichier);
46+
47+
//void animerEnigme(Enigme * e);
48+
49+
50+
51+
#endif // ENIGME_H_INCLUDED
52+

include/enigme_fichier.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef _ENIGME_FICHIER_H_
2+
#define _ENIGME_FICHIER_H_
3+
#include <SDL/SDL_image.h>
4+
#include <SDL/SDL.h>
5+
#include <SDL/SDL_ttf.h>
6+
#include "../include/bounding_box.h"
7+
#include <time.h>
8+
9+
typedef struct{
10+
SDL_Surface *bg;
11+
SDL_Surface *question;
12+
SDL_Surface *reponses[4];
13+
SDL_Rect pos[4];
14+
int corr;
15+
SDL_Surface *won;
16+
SDL_Surface *lost;
17+
SDL_Surface *clock[16];
18+
int anim;
19+
}Enigme;
20+
21+
22+
void genererEnigme(Enigme *e,char *nomfichier);
23+
void animerenigme(Enigme * e);
24+
void afficherEnigme(Enigme e, SDL_Surface * screen, int x);
25+
int resolution(Enigme e);
26+
27+
28+
#endif

src/bounding_box.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "../include/bounding_box.h"
2+
3+
int in_bounds(SDL_Rect x1,SDL_Rect x2)//louken x1 f west l x2
4+
{
5+
return(x1.x>=x2.x)&&(x1.x<=x2.x+x2.w)&&((x1.y>=x2.y)&&(x1.y<=x2.y+x2.h)||((x1.y<=x2.y)&&(x1.y+x2.h>=x2.y)));
6+
}
7+
int collision(SDL_Rect x1,SDL_Rect x2)//ken fama collision bin x1 w x2
8+
{
9+
return in_bounds(x1,x2)||in_bounds(x2,x1);
10+
}

src/enemy.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <stdlib.h>
22
#include <SDL/SDL.h>
33
#include <SDL/SDL_image.h>
4-
#include "constants.h"
5-
#include "enemy.h"
4+
#include "../include/constants.h"
5+
#include "../include/enemy.h"
66

77
void initEnemy(enemy *e)
88
{
@@ -13,9 +13,9 @@ void initEnemy(enemy *e)
1313
}
1414
e->direction = 1;
1515
e->speed = 1;
16-
e->max_steps = 100;
16+
e->max_steps = 300;
1717
e->idle_time = 2000;
18-
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) + 100; // screen width - (width of img / sprite size) - (width of sprite / 2)
18+
e->x = (SCREEN_W / 2 - (e->img->w / 6) - 96 / 2) - 100; // screen width - (width of img / sprite size) - (width of sprite / 2)
1919
e->y = (SCREEN_H / 2 - (e->img->h / 3) - 96 / 2) + 100; // screen height - (height of img / sprite size) - (height of sprite / 2)
2020

2121
e->img_size.x = 0;

src/enigme_fichier.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "../include/enigme_fichier.h"
2+
#define QUESTION 3
3+
4+
void genererEnigme(Enigme *e, char *nomfichier)
5+
{
6+
char reponses[4][16];
7+
char question[32];
8+
char text[8];
9+
FILE *fp;
10+
char buff[255];
11+
fp = fopen(nomfichier, "r");
12+
13+
int l = rand() % 3;
14+
l *= 5;
15+
16+
for (int i = 0; i < l; i++)
17+
fscanf(fp, "%s\n", buff);
18+
fscanf(fp, "%s", question);
19+
for (int i = 0; i < 4; i++)
20+
fscanf(fp, "%s", reponses[i]);
21+
SDL_Delay(100);
22+
23+
e->bg = IMG_Load("../assets/img/bg.jpg");
24+
TTF_Font *font = TTF_OpenFont("../assets/font/Skatchboard.ttf", 50);
25+
SDL_Color color = {125, 0, 125};
26+
e->question = TTF_RenderText_Blended(font, question, color);
27+
e->corr = l;
28+
29+
sprintf(question, "%s", reponses[e->corr]);
30+
sprintf(reponses[e->corr], "%s", reponses[0]);
31+
sprintf(reponses[0], "%s", question);
32+
33+
for (int i = 0; i < 4; i++)
34+
{
35+
sprintf(text, "%c %s", 65 + i, reponses[i]);
36+
e->reponses[i] = TTF_RenderText_Solid(font, text, color);
37+
e->pos[i].x = 200;
38+
e->pos[i].y = 200 + 100 * i;
39+
e->pos[i].w = e->reponses[i]->w;
40+
e->pos[i].h = 60;
41+
}
42+
43+
sprintf(text, "** congratulation **");
44+
e->won = TTF_RenderText_Solid(font, text, color);
45+
46+
sprintf(text, "** wrong **");
47+
e->lost = TTF_RenderText_Solid(font, text, color);
48+
49+
e->anim = 0;
50+
for (int i = 0; i < 16; i++)
51+
{
52+
sprintf(buff, "../assets/img/animation/Layer %d.png", i + 1);
53+
e->clock[i] = IMG_Load(buff);
54+
}
55+
}
56+
57+
void animerenigme(Enigme *e)
58+
{
59+
e->anim++;
60+
if (e->anim == 160)
61+
e->anim = 0;
62+
}
63+
64+
void afficherEnigme(Enigme e, SDL_Surface *screen, int x)
65+
{
66+
SDL_Rect pos;
67+
pos.x = 200;
68+
pos.y = 90;
69+
int i;
70+
SDL_BlitSurface(e.bg, NULL, screen, NULL);
71+
SDL_BlitSurface(e.question, NULL, screen, &pos);
72+
for (i = 0; i < 4; i++)
73+
{
74+
SDL_BlitSurface(e.reponses[i], NULL, screen, &e.pos[i]);
75+
}
76+
e.pos[3].y += 100;
77+
if (x == 1)
78+
SDL_BlitSurface(e.won, NULL, screen, &e.pos[3]);
79+
if (x == 0)
80+
SDL_BlitSurface(e.lost, NULL, screen, &e.pos[3]);
81+
pos.x = (screen->w - e.clock[e.anim / 10]->w);
82+
pos.y = screen->h - e.clock[e.anim / 10]->h;
83+
SDL_BlitSurface(e.clock[e.anim / 10], NULL, screen, NULL);
84+
SDL_Flip(screen);
85+
}
86+
87+
int resolution(Enigme e)
88+
{
89+
Uint8 *keystate = SDL_GetKeyState(NULL);
90+
if (keystate[SDLK_ESCAPE])
91+
return 4;
92+
if (keystate[SDLK_a])
93+
return 0;
94+
if (keystate[SDLK_b])
95+
return 1;
96+
if (keystate[SDLK_c])
97+
return 2;
98+
if (keystate[SDLK_d])
99+
return 3;
100+
int x, y;
101+
Uint32 buttons = SDL_GetMouseState(&x, &y);
102+
SDL_Rect pos;
103+
pos.w = 5;
104+
pos.h = 5;
105+
pos.x = x;
106+
pos.y = y;
107+
for (int i = 0; i < 4; i++)
108+
{
109+
if ((buttons & SDL_BUTTON_LMASK) != 0)
110+
if (collision(pos, e.pos[i]))
111+
return i;
112+
}
113+
return -1;
114+
}

src/main.c

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "../include/stars.h" //stars header
1818
#include "../include/settings.h" //settings header
1919
#include "../include/enemy.h"
20+
#include "../include/enigme_fichier.h"
2021

2122
// screen
2223
SDL_Surface *screen;
@@ -64,18 +65,22 @@ text author;
6465
enemy enemy1;
6566
enemy enemy2;
6667

68+
// enigme
69+
Enigme e;
70+
6771
// logic
6872
SDL_Event event;
69-
int loop = 1; // game loop
70-
int anim_B = 0; // animation for the buttons
71-
int isButtonAnimated = 0; // check if the button animated (to prevent FX spam)
72-
int selectedButton = 0; // selected button (keyboard)
73-
int lastHoveredButton = -1; // last hovered button (keyboard and mouse) but made for keyboard
74-
int StopTheGame = 0; // stop the game, if StopTheGame = 1 -> loop = 0
75-
int UI = 0; // UI = 0 -> main menu, UI = 1 -> settings menu, UI = 2 -> game
76-
int volume = 64; // volume of the music (0 - 128)
77-
bool muteButtonOn = false; // mute button state
78-
73+
int loop = 1; // game loop
74+
int anim_B = 0; // animation for the buttons
75+
int isButtonAnimated = 0; // check if the button animated (to prevent FX spam)
76+
int selectedButton = 0; // selected button (keyboard)
77+
int lastHoveredButton = -1; // last hovered button (keyboard and mouse) but made for keyboard
78+
int StopTheGame = 0; // stop the game, if StopTheGame = 1 -> loop = 0
79+
int UI = 0; // UI = 0 -> main menu, UI = 1 -> settings menu, UI = 2 -> game
80+
int volume = 64; // volume of the music (0 - 128)
81+
bool muteButtonOn = false; // mute button state
82+
static int collisionDetected = 0; // checks if collision detected
83+
static int enigmeDone = 0; // checks if enigme is done
7984
/*
8085
********************
8186
*****GAME BEGIN*****
@@ -160,6 +165,9 @@ int main()
160165
imageLoad_lvlmenutitle(&startMenuTitle);
161166
imageLoad_lvl1(&lvl1);
162167

168+
// loading enigme
169+
genererEnigme(&e, "../assets/text/enigme.txt");
170+
163171
// uint32 return time in milliseconds
164172
Uint32 last_time = SDL_GetTicks();
165173

@@ -226,6 +234,8 @@ int main()
226234
imageDrawClicked_playbutton(screen, playButton_C);
227235
FXLoad(clickFX);
228236
UI = 2;
237+
collisionDetected = 0;
238+
enigmeDone = 0;
229239
break;
230240
case 1:
231241
// settings button pressed
@@ -449,9 +459,36 @@ int main()
449459
drawEnemytest(screen, enemy2);
450460
moveEnemytest(&enemy2); //* moveEnemy will call animateEnemy
451461
//************
452-
if (collisionBB(enemy1.img_pos, enemy2.img_pos) == 1)
462+
if (collisionBB(enemy1.img_pos, enemy2.img_pos) == 1 && collisionDetected == 0)
453463
{
454464
printf(" collision detected \t");
465+
collisionDetected = 1;
466+
}
467+
if (collisionDetected == 1 && enigmeDone == 0)
468+
{
469+
SDL_PumpEvents();
470+
if (resolution(e) == -1)
471+
{
472+
afficherEnigme(e, screen, -1);
473+
animerenigme(&e);
474+
}
475+
else
476+
{
477+
if (resolution(e) == e.corr)
478+
{
479+
afficherEnigme(e, screen, 1);
480+
SDL_Delay(5000); //! remove SDL delay
481+
UI = 0;
482+
enigmeDone = 1;
483+
}
484+
else
485+
{
486+
afficherEnigme(e, screen, 0);
487+
SDL_Delay(5000); //! remove SDL delay
488+
UI = 0;
489+
enigmeDone = 1;
490+
}
491+
}
455492
}
456493

457494
while (SDL_PollEvent(&event))

0 commit comments

Comments
 (0)