test2.c 3.36 KB
#include<SDL/SDL.h>
#include<SDL/SDL_ttf.h>
#include"Graphique/libgraph.h"
#include<stdbool.h>
#include "test2.h"
#include <stdio.h>
#include <stdlib.h>
#define  TAILLE_X     700
#define  TAILLE_DESC  40
#define  LARGEUR_MONS 30
#define  HAUTEUR_MONS 30
#define  LARGEUR_VAIS 35
#define  SPEED        1
#define  MISS_SPEED   5
#define  LARGEUR_MISS 5
#define  HAUTEUR_MISS 10




l_entite* create_l_entite(int x,int y,int speed,int image)
{ 
  l_entite* monster_1 =(l_entite*)malloc(sizeof(l_entite));
  (monster_1->ent).image=image;
  (monster_1->ent).x    =x;
  (monster_1->ent).y    =y;
  (monster_1->ent).speed=speed;
  monster_1->next       = NULL;
  return monster_1;
}

entite create_entite(entite ent,int x,int y, int speed, int image)
{
  (ent).image=image;
  (ent).x    =x;
  (ent).y    =y;
  (ent).speed=speed;
  return ent;
}


  
void add_entite (l_entite** head,int x,int y,int speed,int image)
{
 
  l_entite* new_monster = create_l_entite( x, y,speed,image);
  if (head==NULL)
     {
     *head=new_monster;
    }
  else
    {
     l_entite* current =*head;
      while (current->next != NULL)
	{ current=current->next;
	}
      current->next=new_monster;
    }
}


void display_l_entite(l_entite* mons)
{
 l_entite* ptr= mons;
  while(ptr != NULL)
    {
      afficherLutin((ptr->ent).image,(ptr->ent).x,(ptr->ent).y);
      ptr=ptr->next;
    }
}

void display_entite(entite ent)
 {
    afficherLutin((ent).image,(ent).x,(ent).y);
 }



void moveMonster(l_entite* monstre)
{
  int max =  TAILLE_X - LARGEUR_MONS;
  l_entite* currentMonster = monstre;
  while (currentMonster != NULL)
    {
      (currentMonster->ent).x += (currentMonster->ent).speed;

      if ( (currentMonster->ent).x < 0 || (currentMonster->ent).x > max)
	{
	  (currentMonster->ent).speed = -(currentMonster->ent).speed;
	  (currentMonster->ent).y += TAILLE_DESC ;
	}
      
      currentMonster = currentMonster->next;
    }
}
int main()
{

  l_entite* W_monster = NULL;
  l_entite* B_monster = NULL;
  l_entite* l_missile = NULL;
  entite    vaisseau;
  entite    missile;
  entite    missile_ref;
  creerSurface(TAILLE_X,TAILLE_Y,"Space Invaders");


  
  int ZB=lutinTexte("Space invaders", COULEUR_BLEU);
  int M1=chargerLutin("Graphique/invader_monstre1_2.bmp", COULEUR_BLEU);
  int M2=chargerLutin("Graphique/invader_monstre2_1.bmp", COULEUR_BLEU);
  int V =chargerLutin("Graphique/invader_canon.bmp", COULEUR_BLEU);
  int MI =chargerLutin("Graphique/invader_missile.bmp", COULEUR_BLEU);



  W_monster   =create_l_entite(0,0,1,M1);
  B_monster   =create_l_entite(0,100,1,M2);
  vaisseau    =create_entite(vaisseau,0.5*TAILLE_X,0.95*TAILLE_Y,1,V);
  missile_ref =create_entite(missile_ref,vaisseau.x + (LARGEUR_VAIS/2),400,1,MI);
  l_missile   =create_l_entite( vaisseau.x + (LARGEUR_VAIS/2),(vaisseau).y,1,MI);

  int i=0;int a =60;
  for (i=0;i<4;i++)
     {  
       add_entite(&W_monster,((W_monster->ent).x)+a,(W_monster->ent).y,1,M1);
       add_entite(&B_monster,((B_monster->ent).x)+a,(B_monster->ent).y,1,M2);
       a+=60;
     }


    
  while(1)
    {
      evenement ev;
      char      touche;
       l_missile = NULL;
      lireEvenement(&ev,&touche,NULL);
      if(ev==quitter)
	{break;
	}
      rectanglePlein (0,0,TAILLE_X,TAILLE_Y,COULEUR_NOIR);
      display_l_entite(W_monster);
      display_l_entite(B_monster);
      display_entite(vaisseau);
      moveMonster( W_monster);
      moveMonster( B_monster);
      }
     }