SAE11_2023/SAE_semestre1/src/game.c

247 lines
5.7 KiB
C
Raw Normal View History

2023-12-07 14:35:39 +01:00
#include <stdlib.h>
#include <stdio.h>
#include <graph.h>
#include <time.h>
#define CYCLE 10000L
int seconde=0;
int minute=0;
int seconde_actuel=0;
int old_seconde=0;
char timer[6];
unsigned long int suivant;
/*fin de jeu*/
int go_on=1;
/*serpent*/
int serpent;
int x = 600;
int y = 400;
int direction = 4;
int t;
int segment=10;
int i=0;
int pos_x[60];
int pos_y[60];
int old_x[60];
int old_y[60];
int last_direction = 0;
int p=0;
int pp=0;
int pomme, pommex[5], pommey[5];
int fond;
int nombre;
char score[4];
2023-12-11 16:21:27 +01:00
int murx[50];
int mury[50];
int mur;
2023-12-07 14:35:39 +01:00
void DessinerScene(){
snprintf(timer,6,"%02d:%02d", minute ,seconde);
ChoisirCouleurDessin(CouleurParComposante(91,222,122));
RemplirRectangle(20,20,1160,700);
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
serpent=ChargerSprite("../img/serpent.png");
fond = ChargerSprite("../img/fond.png");
2023-12-11 16:21:27 +01:00
mur = ChargerSprite("../img/mur.png");
2023-12-07 14:35:39 +01:00
for (i = 0; i < segment; i++){
AfficherSprite(serpent, x-(i*20), y);
pos_x[i]=x-(i*20);
pos_y[i]=y;
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
srand(time(NULL));
pomme=ChargerSprite("../img/pomme.png");
for (p = 0; p < 5; p++) {
pommex[p] = ((rand() % (55)+1)*20);
pommey[p] = ((rand() % (35)+1)*20);
AfficherSprite(pomme, pommex[p],pommey[p]);
}
2023-12-11 17:22:35 +01:00
for(i=0; i<30; i++){
2023-12-11 16:21:27 +01:00
murx[i] = ((rand() % (55)+1)*20);
mury[i] = ((rand() % (35)+1)*20);
AfficherSprite(mur, murx[i], mury[i]);
}
}
void Score(){
nombre= (segment-10)*10;
snprintf(score,4,"%04d0", nombre);
ChoisirCouleurDessin(CouleurParNom("black"));
RemplirRectangle(1100,700,1200,800);
ChoisirCouleurDessin(CouleurParNom("white"));
EcrireTexte(1000,760,"Score: ",2);
EcrireTexte(1100,760,score,2);
2023-12-07 14:35:39 +01:00
}
void Update_Timer(){
snprintf(timer,6,"%02d:%02d", minute, seconde);
ChoisirCouleurDessin(CouleurParComposante(0,0,0));
2023-12-11 17:22:35 +01:00
RemplirRectangle(10,700,12000,800);
2023-12-07 14:35:39 +01:00
ChoisirCouleurDessin(CouleurParComposante(255,255,255));
2023-12-11 16:21:27 +01:00
EcrireTexte(50,760,"time: ",2);
EcrireTexte(120,760,timer,2);
2023-12-07 14:35:39 +01:00
}
void Update_Serpent(){
AfficherSprite(fond, pos_x[segment-1], pos_y[segment-1]);
AfficherSprite(serpent, pos_x[0], pos_y[0]);
for (i=1 ; i<segment ; i++){
pos_x[i]=old_x[i-1];
pos_y[i]=old_y[i-1];
AfficherSprite(serpent, pos_x[i], pos_y[i]);
}
old_x[0]=pos_x[0];
old_y[0]=pos_y[0];
for (i=1 ; i<segment ; i++){
old_x[i]=pos_x[i];
old_y[i]=pos_y[i];
}
}
2023-12-11 17:22:35 +01:00
void Collision_Serpent(){
int bombe;
bombe = ChargerSprite("../img/bombe.png");
/*Serpent contre coté*/
if (pos_x[0] >1160 || pos_x[0]<=20){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirRectangle(pos_x[0],pos_x[0], 20,20);
go_on=0;
}
/*Serpent contre coté*/
if (pos_y[0]<40 || pos_y[0] >=700){
ChoisirCouleurDessin(CouleurParNom("red"));
RemplirRectangle(pos_y[0],pos_y[0], 20,20);
2023-12-07 14:35:39 +01:00
go_on=0;
2023-12-11 17:22:35 +01:00
}
/*Serpent contre Serpent*/
2023-12-07 14:35:39 +01:00
for (i = 1; i < segment; i++) {
if (pos_x[0] == pos_x[i] && pos_y[0] == pos_y[i])
go_on = 0;
}
2023-12-11 17:22:35 +01:00
/*Serpent contre mur*/
for(i=0; i<30;i++){
if(pos_x[0] == murx[i] && pos_y[0] == mury[i]){
AfficherSprite(bombe, murx[i], mury[i]);
go_on=0;
}
}
2023-12-07 14:35:39 +01:00
}
void Timer(){
if(Microsecondes()> suivant){
suivant = Microsecondes()+CYCLE;
seconde_actuel = (suivant/1000000)%10;
if(seconde_actuel !=old_seconde){
old_seconde = seconde_actuel;
if(seconde == 59){
minute=minute+1;
seconde=0;
Update_Timer();
}else;{
seconde = seconde+1;
Update_Timer();
}
}
}
}
void Controle() {
while(ToucheEnAttente()) {
t = Touche();
switch(t) {
case XK_Left:
if (last_direction != 4) {
direction = 3;
}
break;
case XK_Right:
if (last_direction != 3) {
direction = 4;
}
break;
case XK_Up:
if (last_direction != 2) {
direction = 1;
}
break;
case XK_Down:
if (last_direction != 1) {
direction = 2;
}
break;
case XK_Escape:
direction = 0;
go_on = 0;
break;
case XK_p:
direction = 0;
}
}
}
void Serpent() {
if (direction == 1 && direction != 2 ){
pos_y[0]=old_y[0]-20;
}
if (direction == 2 && direction != 1) {
pos_y[0]=old_y[0]+20;
}
if (direction == 3 & direction != 4) {
pos_x[0]=old_x[0]-20;
}
if (direction == 4 && direction != 3) {
pos_x[0]=old_x[0]+20;
}
for(p=0; p<6; p++){
if(pommex[p]==pos_x[0] && pommey[p]==pos_y[0]){
segment+=2;
pommex[p] = ((rand() % (60)+1)*20);
pommey[p] = ((rand() % (27)+1)*20);
}
}
Update_Serpent();
2023-12-11 17:22:35 +01:00
Collision_Serpent();
2023-12-07 14:35:39 +01:00
usleep(100000);
}
void Pomme(){
for(pp = 0; pp < 5; ++pp){
AfficherSprite(pomme, pommex[pp], pommey[pp]);
}
}
2023-12-11 17:22:35 +01:00
2023-12-07 14:35:39 +01:00
2023-12-11 16:21:27 +01:00
2023-12-07 14:35:39 +01:00
int main(){
InitialiserGraphique();
2023-12-11 16:21:27 +01:00
CreerFenetre(350,100,1200,900);
2023-12-07 14:35:39 +01:00
EffacerEcran(CouleurParComposante(0,0,0));
suivant = Microsecondes()+CYCLE;
old_seconde=(suivant/1000000)%10;
DessinerScene();
while(go_on){
Timer();
Score();
Controle();
Serpent();
Pomme();
}
FermerGraphique();
return EXIT_SUCCESS;
}