1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
#include<stdlib.h>
#include<stdio.h>
#include<graph.h>
#include<time.h>
#define LARGEUR (Maxx()*3)/4
#define HAUTEUR (Maxy()*3)/4
#define Lcase LARGEUR/60
#define Hcase HAUTEUR/40
#define L 60
#define H 40
#define Tl 19
#define Th 30
#define Ql 10
/*void fenetre(int tabx[][H]) {
int i,j;
if ( tabx[j][i] == 0 )
RemplirRectangleC(0,0,Lcase,Hcase,CouleurParComposante(131,202,34));
if ( tabx[j][i] == 1 )
RemplirRectangleC(0,0,Lcase,Hcase,CouleurParComposante(0,153,0));
if ( tabx[j][i] == 8 )
RemplirRectangleC(0,0,Lcase,Hcase,CouleurParComposante(174,255,0));
}
*/
/*int collision(int tabx[][H]) {
int i,j;
if ( tabx[j][i] == 8 ) {
FermerGraphique();
}
}
*/
/*
int droite(int tabx[][H]) {
int i,j,k=0;
while ( ToucheEnAttente() == 0 ) {
if ( tabx[Tl+k][Th] != 8 ) {
tabx[Tl+k][Th]=3;
tabx[Ql+k][Th]=0;
printf("%d\n",Tl+k);
k = k + 1;
}
/* Test collision
else {
FermerGraphique();
return EXIT_SUCCESS;
}
for ( i=0 ; i < H ; i++ ) {
for ( j=0 ; j < L ; j++ ) {
printf("%d",tabx[j][i]);
}
putchar('\n');
}
usleep(200000);
}
}
*/
void AJOUT_POMME(void) {
int tabx[L][H];
int pommex,pommey;
srand(time(NULL));
pommex=(rand()%L)+1;
pommey=(rand()%H)+1;
if ( ( tabx[pommex][pommey] != 8 ) && ( tabx[pommex][pommey] != 1 ) && ( tabx[pommex][pommey] != 3 ) ) {
tabx[pommex][pommey]=7;
ChargerImage("fruit1.xpm",Lcase*pommex,Hcase*pommey,0,0,Lcase,Lcase);
}
}
int main(void) {
int i,j,k=0;
int tabx[L][H];
int pommex,pommey;
/* Initialisation de l'interface graphique et création de la fenetre */
InitialiserGraphique();
CreerFenetre((Maxx()-LARGEUR)/2,(Maxy()-HAUTEUR)/2,LARGEUR,HAUTEUR);
/* Initialisation de la matrice */
for ( i=0 ; i < H ; i++ ) {
for ( j=0 ; j < L ; j++ ) {
tabx[j][i] = 0;
}
}
/* Creation du serpent d'origine */
/* Queue du serpent=1 || Tete du serpent=3 */
tabx[Ql][Th]=1;
for ( j=11 ; j < 19 ; j++ ) {
tabx[j][30] = 1;
}
tabx[Tl][Th]=3;
/* Creation des murs */
/* Mur= 8 */
for ( j=0 ; j < L ; j++ )
tabx[j][0]=8;
for ( j=0 ; j < L ; j++ )
tabx[j][H-1]=8;
for ( i=0 ; i < H ; i++ )
tabx[0][i]=8;
for ( i=0 ; i < H ; i++ )
tabx[L-1][i]=8;
/* Affichage en mode graphique */
for ( i=0 ; i < H ; i++ ) {
for ( j=0 ; j < L ; j++ ) {
if ( tabx[j][i] == 0 )
RemplirRectangleC(Lcase*j,Hcase*i,Lcase,Hcase,CouleurParComposante(131,202,34));
if ( ( tabx[j][i] == 1 ) || ( tabx[j][i] == 3 ) )
RemplirRectangleC(Lcase*j,Hcase*i,Lcase,Hcase,CouleurParComposante(0,153,0));
if ( tabx[j][i] == 8 )
RemplirRectangleC(Lcase*j,Hcase*i,Lcase,Hcase,CouleurParComposante(174,255,0));
}
}
if ( Touche () == XK_Right ) {
/* Placement aleatoire des fruits */
srand(time(NULL));
pommex=(rand()%L)+1;
pommey=(rand()%H)+1;
if ( ( tabx[pommex][pommey] != 8 ) && ( tabx[pommex][pommey] != 1 ) && ( tabx[pommex][pommey] != 3 ) ) {
tabx[pommex][pommey]=7;
ChargerImage("fruit1.xpm",Lcase*pommex,Hcase*pommey,0,0,Lcase,Lcase);
}
/* Deplacement du serpent */
while ( ToucheEnAttente() == 0 ) {
if ( tabx[Tl+k][Th] != 8 && tabx[Tl+k][Th] != 1 ) { /* Test collision contre mur ou serpent */
tabx[Tl+k][Th]=3;
tabx[Ql+k][Th]=0;
RemplirRectangleC(Lcase*((Ql-1)+k),Hcase*Th,Lcase,Hcase,CouleurParComposante(131,202,34));
RemplirRectangleC(Lcase*(Tl+k),Hcase*Th,Lcase,Hcase,CouleurParComposante(0,153,0));
k= k + 1;
}
else { /* Collision avec un mur ou serpent lui même, donc on sort */
FermerGraphique();
return EXIT_SUCCESS;
}
/* Affichage de la matrice */
for ( i=0 ; i < H ; i++ ) {
for ( j=0 ; j < L ; j++ ) {
printf("%d",tabx[j][i]);
}
putchar('\n');
}
usleep(80000);
}
}
Touche();
FermerGraphique();
return EXIT_SUCCESS;
} |
Partager