Project Thesis Main .C Et Game Controller
Project Thesis Main .C Et Game Controller
Background Coding
Player Coding
Enemy Coding
Minimap Code
Enigma Coding
Tic Tac Toe
3. Game Controller Idea and Code
Main .c integration
#include <stdio.h>
#include <stdlib.h>
#include "menu.h" // Including the header file for the menu
#include <SDL/SDL.h> // Including the SDL library
#include <SDL/SDL_image.h> // Including the SDL image library for loading
image files
#include <SDL/SDL_mixer.h> // Including the SDL mixer library for sound
#include <SDL/SDL_ttf.h> // Including the SDL TrueType Fonts library for
rendering text
#include "joueur.h" // Including the header file for the player
#include "background.h" // Including the header file for the background
#include "minimap.h" // Including the header file for the minimap
#include "xo.h" // Including the header file for Tic Tac Toe
#include "enemie.h" // Including the header file for the enemy
#include "enig_fichier.h" // Including the header file for file-based puzzles
//////////////////tictactoe/////////////////////////////
void tictactoe(SDL_Surface *screen); // Function prototype for Tic Tac Toe
void tictactoe(SDL_Surface *screen){ // Definition of the Tic Tac Toe function
int contin=1; // Variable to control the main loop of the game
tic t; // Structure variable for Tic Tac Toe game state
int coup; // Variable to store the move made by the player
TTF_Init(); // Initialize SDL_TTF for text rendering
SDL_Event event; // Event variable for handling SDL events
int joueur = 1; // Variable to track which player's turn it is, assuming player 1
starts first
// Check if there are remaining turns to play and if nobody has won yet
if (t.tour < 9 && atilganer(t.tabsuivi) == 0)
{
// Check whose turn it is
if ((t.tour + joueur) % 2 == 0) // Computer's turn
{
// Calculate the computer's move
calcul_coup(t.tabsuivi);
t.tour++; // Increment the number of turns
}
else // Player's turn
{
// Wait for an SDL event
SDL_WaitEvent(&event);
// Handle events
switch (event.type)
{
case SDL_QUIT: // If the user closes the window
contin = 0; // Exit the loop
break;
case SDL_MOUSEBUTTONUP: // If the user clicks the mouse
// Calculate the click coordinates
a = event.button.x / 190;
bb = event.button.y / 190;
coup = 3 * bb + a; // Calculate the move to play
if (t.tabsuivi[coup] == 0) {
t.tabsuivi[coup] = -1; // Set the player's move in the tracking
array
t.tour++; // Increment the number of turns
}
break;
}
}
}
else
{
// Display the game result
Resultat(t, screen);
contin = 0; // Exit the loop
printf("%d", t.tour); // Display the number of turns played
}
}
/////////////////////fonction tictactoe//////////////////////////
int main()
{
// Initialize SDL and set up the screen
SDL_Surface *screen ,*ecrann ;
screen=SDL_SetVideoMode(1720,968,32,SDL_SWSURFACE|SDL_DOUBLEBUF);
SDL_Init(SDL_INIT_EVERYTHING);
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER)==-1)
{
printf("could not initialize SDL:%s\n",SDL_GetError());
return -1;
}
// Initialize SDL_ttf
if (TTF_Init() == -1) {
printf("Unable to initialize SDL_ttf: %s\n", TTF_GetError());
SDL_Quit();
return 1;
}
// Load font for timer
TTF_Font *font = TTF_OpenFont("front/pepsi.ttf", 60); // Change "font.ttf" to
your font file
if (!font) {
printf("Unable to load font: %s\n", TTF_GetError());
TTF_Quit();
SDL_Quit();
return 1;
}
// Initialize bonuses
initBonus(&bb);
initBonus(&bb1);
initBonus(&bb2);
// Main loop
while(boucle)
{
// Code for different states of the game menu and gameplay
}
/////////////////////////////////////////////////////////////////////////////
//stage 1 ///////////////////////////////////////////////////////
if(state==2)
{
// Update time
t_prev = SDL_GetTicks();
if (collisionPP_down(p2.pos_perso,mask,b.backgroundPos2)==1)
{
p2.ground=p2.pos_perso.y;
}
if (p2.pos_perso.y>=p2.ground)
{
p2.pos_perso.y=p2.ground;
p2.vitesse_saut=0;
p2.up=0;
}
if (collisionPP_down(p1.pos_perso,mask,b.backgroundPos)==1)
{
p1.ground=p1.pos_perso.y;
}
if (p1.pos_perso.y>=p1.ground)
{
p1.pos_perso.y=p1.ground;
p1.vitesse_saut=0;
p1.up=0;
}
}
/////////////////////////////////////////////////////////////////////////////
//multiplayer
if(state==3)
{
// Render various elements for the multiplayer mode
}
///////////////////////////////////////////////////////////////////////////////////
// // pause
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_QUIT:
boucle =0;
break;
case SDL_MOUSEMOTION:
if(state==0 && BB.etat==0)
{
if(event.motion.x >= 710 && event.motion.x < 710 +
301 && event.motion.y >= 800 && event.motion.y < 800+ 161)
{
bt1.etat=1;
}
else
{
bt1.etat=0;
}//play
}
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 687 && event.motion.x < 687 +
345 && event.motion.y >= 160 && event.motion.y < 160+ 160)
{
start.etat=1;
initialiser_audiobref(mus);
}
else
{
start.etat=0;
liberer_audiobref(mus);
}//start
if(event.motion.x >= 687 && event.motion.x < 687 +
345 && event.motion.y >= 330 && event.motion.y < 330+ 160)
{
settings.etat=1;
initialiser_audiobref(mus);
}
else
{
settings.etat=0;
}//settings
if(event.motion.x >= 687 && event.motion.x < 687 +
345 && event.motion.y >= 500 && event.motion.y < 500+ 160)
{
credits.etat=1;
initialiser_audiobref(mus);
}
else
{
credits.etat=0;
}//credits
if(event.motion.x >= 687 && event.motion.x < 687 +
345 && event.motion.y >= 680 && event.motion.y < 680+ 160)
{
quit.etat=1;
initialiser_audiobref(mus);
}
else
{
quit.etat=0;
}//quit
}
if(state==0 &&
BB.etat==2)///////////////////settings////////////////////////////////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 1170 && event.motion.x < 1170 +
101 && event.motion.y >= 220 && event.motion.y < 220+ 101)
{
plus1.etat=1;
//afficher_mini_texte(screen , minitxt);
}
else
{
plus1.etat=0;
}//boutons plus1
if(event.motion.x >= 400 && event.motion.x < 400 +
101 && event.motion.y >= 220 && event.motion.y < 220+ 101)
{
moins1.etat=1;
}
else
{
moins1.etat=0;
}//boutons moins1
if(event.motion.x >= 1170 && event.motion.x < 1170 +
101 && event.motion.y >= 400 && event.motion.y < 400+ 101)
{
plus2.etat=1;
}
else
{
plus2.etat=0;
}//boutons plus2
if(event.motion.x >= 400 && event.motion.x < 400 +
101 && event.motion.y >= 400 && event.motion.y < 400+ 101)
{
moins2.etat=1;
}
else
{
moins2.etat=0;
}//boutons moins2
if(event.motion.x >= 500 && event.motion.x < 500 +
356 && event.motion.y >= 600 && event.motion.y < 600+ 161)
{
btfull.etat=1;
}
else
{
btfull.etat=0;
}//boutons fullscreen
}
if(state==0 &&
BB.etat==3)//////////////////////////////////////credits//////////////////////////
////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
}
if(state==0 &&
BB.etat==6)//////////////////////////////////////load
game//////////////////////////////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 1100 && event.motion.x < 1100
+ 345 && event.motion.y >= 700 && event.motion.y < 700+ 160)
{
confi.etat=1;
}
else
{
confi.etat=0;
}//ok
}
if(state==0 &&
BB.etat==7)//////////////////////////////////////singleplayer////////////////////
//////////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 1100 && event.motion.x < 1100
+ 345 && event.motion.y >= 700 && event.motion.y < 700+ 160)
{
confi.etat=1;
}
else
{
confi.etat=0;
}//ok
}
if(state==0 &&
BB.etat==8)//////////////////////////////////////multiplayer/////////////////////
/////////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 1100 && event.motion.x < 1100
+ 345 && event.motion.y >= 700 && event.motion.y < 700+ 160)
{
confi.etat=1;
}
else
{
confi.etat=0;
}//ok
}
if(state==1 ||
state==2)//////////////////////////////////////pause/////////////////////////////
/////
{
if(event.motion.x >= 1550 && event.motion.x < 1550 +
150 && event.motion.y >= 10 && event.motion.y < 10+ 150)
{
pausebt.etat=1;
}
else
{
pausebt.etat=0;
}//pause
if(state==3)//////////////////////////////////////pause////////////////////
//////////////
{
if(event.motion.x >= 50 && event.motion.x < 50 + 151
&& event.motion.y >= 50 && event.motion.y < 50+ 127)
{
btBACK.etat=1;
}
else
{
btBACK.etat=0;
}//menu back
if(event.motion.x >= 550 && event.motion.x < 550 +
345 && event.motion.y >= 600 && event.motion.y < 600+ 160)
{
quitterbt.etat=1;
}
else
{
quitterbt.etat=0;
}//bt quitter
if(event.motion.x >= 900 && event.motion.x < 900 +
345 && event.motion.y >= 600 && event.motion.y < 600+ 160)
{
sauvbt.etat=1;
}
else
{
sauvbt.etat=0;
}//bt sauvgarde
}
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_LEFT)
{
int x = event.button.x;
int y = event.button.y;
if(state==0 && BB.etat==0)
{
if (x >= 710 && x < 710 + 301 && y >= 800 && y
< 800+ 161)
{
BB.etat=1;
}
}//play
if(state==0 && BB.etat==1)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=0;
}//menu back
if (x >= 687 && x < 687 + 345 && y >= 680 && y
< 680+ 160)
{
boucle =0;
}//quit
if (x >= 687 && x < 687 + 345 && y >= 330 && y
< 330+ 160)
{
BB.etat=2;
}//settings
if (x >= 687 && x < 687 + 345 && y >= 500 && y
< 500+ 160)
{
BB.etat=3;
}//credits
if (x >= 687 && x < 687 + 345 && y >= 160 && y
< 160+ 160)
{
BB.etat=4;
}//start
}
if(state==0 && BB.etat==4)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=1;
}//menu back
if (x >= 400 && x < 400 + 345 && y >= 160 && y
< 160+ 160)
{
BB.etat=5;
}//newgame
if (x >= 950 && x < 950 + 345 && y >= 160 && y
< 160+ 160)
{
BB.etat=6;
}//loadgame
if (stage.etat == 0 && ((x >= 450 && x < 450 + 70
&& y >= 550 && y < 550 + 44) || (x >= 1200 && x < 1200 + 70 && y >= 550 && y
< 550 + 44)))
{
stage.etat = 1;
}else if (stage.etat == 1 && ((x >= 450 && x <
450 + 70 && y >= 550 && y < 550 + 44) || (x >= 1200 && x < 1200 + 70 && y >=
550 && y < 550 + 44)))
{
stage.etat = 2;
}
else if (stage.etat == 2 && ((x >= 450 && x < 450
+ 70 && y >= 550 && y < 550 + 44) || (x >= 1200 && x < 1200 + 70 && y >= 550
&& y < 550 + 44)))
{
stage.etat = 0;
}//change stage
}
else if(state==0 && BB.etat==5)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=4;
}//menu back
if (x >= 400 && x < 400 + 345 && y >= 400 && y
< 400+ 160)
{
BB.etat=7;
}//singleplayer
if (x >= 950 && x < 950 + 345 && y >= 400 && y
< 400+ 160)
{
BB.etat=8;
}//multiplayer
}
if(state==0 && BB.etat==6)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=4;
}//menu back
}
if(state==0 && BB.etat==7)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=5;
}//menu back
if (chperso.etat == 0 && ((x >= 600 && x < 600 +
70 && y >= 400 && y < 400 + 44) || (x >= 1100 && x < 1100 + 70 && y >= 400
&& y < 400 + 44)))
{
chperso.etat = 1;
}
else if (chperso.etat == 1 && ((x >= 600 && x <
600 + 70 && y >= 400 && y < 400 + 44) || (x >= 1100 && x < 1100 + 70 && y >=
400 && y < 400 + 44)))
{
chperso.etat = 2;
}else if (chperso.etat == 2 && ((x >= 600 && x <
600 + 70 && y >= 400 && y < 400 + 44) || (x >= 1100 && x < 1100 + 70 && y >=
400 && y < 400 + 44)))
{
chperso.etat = 0;
}// choisir
if (x >= 1100 && x < 1100 + 345 && y >= 700 &&
y < 700+ 160)
{
state=1;
specifier=1;
}//play stage 1
///////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////
}
if(state==0 && BB.etat==8)
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=5;
}//menu back
if (chperso2.etat == 0 && ((x >= 300 && x < 300
+ 70 && y >= 400 && y < 400 + 44) || (x >= 600 && x < 600 + 70 && y >= 400
&& y < 400 + 44)))
{
chperso2.etat = 1;
}
else if (chperso2.etat == 1 && ((x >= 300 && x <
300 + 70 && y >= 400 && y < 400 + 44) || (x >= 600 && x < 600 + 70 && y >=
400 && y < 400 + 44)))
{
chperso2.etat = 2;
}else if (chperso2.etat == 2 && ((x >= 300 && x <
300 + 70 && y >= 400 && y < 400 + 44) || (x >= 600 && x < 600 + 70 && y >=
400 && y < 400 + 44)))
{
chperso2.etat = 0;
}// choisir first character
}
if((state==0 && BB.etat==2) || (state==3))
{
/////////////////////////////boutons moins 2
://////////////////////////////////
if(volume2.etat==0 && x >= 400 && x < 400 +
101 && y >= 400 && y < 400+ 101)
{
volume2.etat=1;
}
}
if(state==0 && BB.etat==3)/////////////////////////////
credits /////////////////////////////////
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
BB.etat=1;
}
}
if(state==1 || state== 2)/////////////////////////////
pause /////////////////////////////////
{
if (x >= 1550 && x < 1550 + 150 && y >= 10 && y
< 10+ 150)
{
state=3;
}
}
if(state==3)///////////////////////////// pause
/////////////////////////////////
{
if (x >= 50 && x < 50 + 151 && y >= 50 && y <
50+ 127)
{
if(specifier==1)
state=1;
if(specifier==2)
state=2;
}
if (x >= 500 && x < 500 + 345 && y >= 600 && y
< 600+ 160)
{
state=0;
}
}
}
break;
case SDL_KEYDOWN:
if ((state == 1 || state == 2) && event.key.keysym.sym
== SDLK_ESCAPE)
state =3;
if (event.key.keysym.sym == SDLK_u)
boucle =0;
if (event.key.keysym.sym == SDLK_p && BB.etat==1)
BB.etat=4;
if (event.key.keysym.sym == SDLK_ESCAPE &&
BB.etat==4 || BB.etat==2 || BB.etat==3 )
BB.etat=1;
if (event.key.keysym.sym == SDLK_s && BB.etat==1)
BB.etat=2;
if (event.key.keysym.sym == SDLK_c && BB.etat==1)
BB.etat=3;
if (event.key.keysym.sym == SDLK_RIGHT)
{
p.direction = 1;
p.vitesse = 2;
if (event.key.keysym.sym == SDLK_LEFT)
{
p.direction = -1;
p.vitesse = 2;
if (event.key.keysym.sym == SDLK_UP)
{
up = 1;
}
if (event.key.keysym.sym == SDLK_q)
{
p2.direction = -1;
p2.vitesse = 2;
if (event.key.keysym.sym == SDLK_s)
{
up2 = 1;
}
if (event.key.keysym.sym == SDLK_LEFT)
{
p1.direction = -1;
p1.vitesse = 2;
if (event.key.keysym.sym == SDLK_UP)
{
up1 = 1;
}
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_RIGHT)
{
p.direction = 0;
p.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_LEFT)
{
p.direction = 0;
p.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_UP)
{
up = 0;
}
if (event.key.keysym.sym == SDLK_a)
{
acceleration = 0;
p.vitesse = 2;
}
if (event.key.keysym.sym == SDLK_z)
{
deceleration = 0;
p.vitesse = 2;
}
/////////////////////////////////////////////////// PERSO multijoueur
///////////////////////////////////////////////////
if (event.key.keysym.sym == SDLK_d)
{
p2.direction = 0;
p2.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_q)
{
p2.direction = 0;
p2.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_s)
{
up2 = 0;
}
if (event.key.keysym.sym == SDLK_w)
{
acceleration2 = 0;
p2.vitesse = 2;
}
if (event.key.keysym.sym == SDLK_x)
{
deceleration2 = 0;
p2.vitesse = 2;
}
/////////////////////////////////////////////////////
if (event.key.keysym.sym == SDLK_RIGHT)
{
p1.direction = 0;
p1.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_LEFT)
{
p1.direction = 0;
p1.vitesse = 0;
}
if (event.key.keysym.sym == SDLK_UP)
{
up1 = 0;
}
if (event.key.keysym.sym == SDLK_a)
{
acceleration1 = 0;
p1.vitesse = 2;
}
if (event.key.keysym.sym == SDLK_z)
{
deceleration = 0;
p1.vitesse = 2;
}
break;
}
}
SDL_Flip(screen);
}
liberer_img(IMAGE);
liberer_img(back);
liberer_img(backtt);
liberer_img(bt1);
liberer_img(mb);
liberer_texte(txte);
liberer_texte(starttxt);
liberer_texte(settingstxt);
liberer_texte(creditstxt);
liberer_texte(quittxt);
liberer_texte(splayer);
liberer_texte(mplayer);
liberer_texte(ngame);
liberer_texte(lgame);
//liberer_texte(minitxt);
liberer_img(start);
liberer_img(settings);
liberer_img(credits);
liberer_img(quit);
liberer_img(volume1);
liberer_img(plus1);
liberer_img(moins1);
liberer_img(volume2);
liberer_img(plus2);
liberer_img(moins2);
liberer_img(sinplayer);
liberer_img(multiplayer);
liberer_img(btfull);
liberer_img(btwind);
liberer_img(quitterbt);
liberer_img(sauvbt);
liberer_img(mbcredits);
liberer_img(loadgame);
liberer_img(newgame);
liberer_img(stage);
liberer_img(chperso);
liberer_img(fleshe1);
liberer_img(fleshe2);
liberer_img(chperso2);
liberer_img(fleshe12);
liberer_img(fleshe22);
liberer_img(chperso3);
liberer_img(fleshe13);
liberer_img(fleshe23);
liberer_img(pausebt);
liberer_img(confi);
liberer_texte(ok);
liberer_texte(choisir);
liberer_texte(choisir2);
liberer_texte(choisir3);
liberer_musique(music);
Free_background(&b);
Free_perso(&p);
Free_perso(&p2);
Free_minimap(&m);
TTF_CloseFont(font);
SDL_FreeSurface(e.spritesheet);
SDL_Quit();
return 0;
}
Game Controller Code Explained
#include <Arduino.h>
void setup() {
// Initialize serial communication
Serial.begin(9600); // Start serial communication at 9600 baud
void loop() {
// Read joystick positions
int joyX = analogRead(joyXPin); // Read analog value from joystick X-axis
int joyY = analogRead(joyYPin); // Read analog value from joystick Y-axis