chang val
This commit is contained in:
parent
ef1da78c49
commit
0b0bb215bf
198
virage.c
Normal file
198
virage.c
Normal file
@ -0,0 +1,198 @@
|
||||
#include<stdlib.h>
|
||||
#include<stdio.h>
|
||||
#include<graph.h>
|
||||
#include<time.h>
|
||||
#include<unistd.h>
|
||||
|
||||
#define H 40
|
||||
#define L 60
|
||||
#define DELTA 1000000L
|
||||
#define DELTO 1000000L
|
||||
|
||||
/*recuperation des couleurs en fonction des chiffres dans le tableau*/
|
||||
void AfficheTab(int tab[H][L], int posx, int posy, int i, int j){
|
||||
couleur c;
|
||||
if(tab[i][j]==0){
|
||||
c=CouleurParNom("black");
|
||||
ChoisirCouleurDessin(c);
|
||||
RemplirRectangle(posx,posy,20,20);
|
||||
}
|
||||
/*
|
||||
6>droite
|
||||
7>gauche
|
||||
8>haut
|
||||
8>bas
|
||||
*/
|
||||
if(tab[i][j]==1||tab[i][j]==6||tab[i][j]==7||tab[i][j]==8||tab[i][j]==9){
|
||||
c=CouleurParNom("dark orange");
|
||||
ChoisirCouleurDessin(c);
|
||||
RemplirRectangle(posx,posy,20,20);
|
||||
}
|
||||
}
|
||||
|
||||
/*affichage du tableau pour rendu graphique*/
|
||||
void Affiche(int tab[H][L]){
|
||||
int i,j,posx=0,posy=0;
|
||||
for(i=0;i<H;i++){
|
||||
for(j=0;j<L;j++){
|
||||
AfficheTab(tab, posx, posy, i, j);
|
||||
posx=posx+20;
|
||||
}
|
||||
posx=0;
|
||||
posy=posy+20;
|
||||
}
|
||||
posx=0;
|
||||
posy=0;
|
||||
}
|
||||
|
||||
/*recuperation de la direction en fonction des touches*/
|
||||
int CliqueTouche(int tab[H][L],int direction){
|
||||
switch(Touche()){
|
||||
case XK_Up:
|
||||
if(direction!=0){
|
||||
printf("aaaaaaa");
|
||||
direction=2;
|
||||
return direction;
|
||||
}
|
||||
case XK_Down:
|
||||
if(direction!=2){
|
||||
printf("zzzzzzz");
|
||||
direction=0;
|
||||
return direction;
|
||||
}
|
||||
case XK_Right:
|
||||
if(direction!=1){
|
||||
printf("eeeeeeeee");
|
||||
direction=3;
|
||||
return direction;
|
||||
}
|
||||
case XK_Left:
|
||||
if(direction!=3){
|
||||
printf("rrrrrrrr");
|
||||
direction=1;
|
||||
return direction;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int DepXTete(unsigned long suivant2,int sxmax,int symax,int direction,int tab[H][L]){
|
||||
/*haut*/
|
||||
if(direction==2){
|
||||
sxmax=sxmax-1;
|
||||
tab[sxmax][symax]=1;
|
||||
return sxmax;
|
||||
}
|
||||
/*bas*/
|
||||
if(direction==0){
|
||||
sxmax=sxmax+1;
|
||||
tab[sxmax][symax]=1;
|
||||
return sxmax;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int DepYTete(unsigned long suivant2,int sxmax,int symax,int direction,int tab[H][L]){
|
||||
/*droite*/
|
||||
if(direction==3){
|
||||
symax=symax+1;
|
||||
tab[sxmax][symax]=1;
|
||||
return symax;
|
||||
}
|
||||
/*gauche*/
|
||||
if(direction==1){
|
||||
symax=symax-1;
|
||||
tab[sxmax][symax]=1;
|
||||
return symax;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*mvmt du serpent*/
|
||||
void Serpent(int tab[H][L], int* psxmax, int* psxmin, int* psymax, int*psymin, int* pdirection){
|
||||
unsigned long suivant2=Microsecondes()+DELTO;
|
||||
|
||||
pdirection = CliqueTouche(tab,pdirection);
|
||||
|
||||
/*deplacement tete*/
|
||||
if(Microsecondes()>=suivant2){
|
||||
if(pdirection==0||pdirection==2){
|
||||
psxmax=DepXTete(suivant2,psxmax,psymax,pdirection,tab);
|
||||
}
|
||||
else if(pdirection==1||pdirection==3){
|
||||
psymax=DepYTete(suivant2,psxmax,psymax,pdirection,tab);
|
||||
}
|
||||
suivant2=Microsecondes()+DELTO;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*affichage du timer*/
|
||||
void DessinerTimer(int n){
|
||||
couleur c;
|
||||
char buf[100];
|
||||
c=CouleurParNom("white");
|
||||
ChoisirCouleurDessin(c);
|
||||
RemplirRectangle(0,850,200,100);
|
||||
c=CouleurParNom("black");
|
||||
ChoisirCouleurDessin(c);
|
||||
snprintf(buf,100,"time : %d",n);
|
||||
EcrireTexte(50,900,buf,2);
|
||||
}
|
||||
|
||||
/*initialisation de la grille et de la fenetre*/
|
||||
void init(int tab[H][L]){
|
||||
/*creation page*/
|
||||
InitialiserGraphique();
|
||||
CreerFenetre(10,10,1700,1000);
|
||||
couleur c;
|
||||
c=CouleurParNom("green");
|
||||
ChoisirCouleurDessin(c);
|
||||
int i,j;
|
||||
/*initialisation de la grille a 0(vert)*/
|
||||
for(i=0;i<H;i++){
|
||||
for(j=0;j<L;j++){
|
||||
tab[i][j]=0;
|
||||
}
|
||||
}
|
||||
/*initialisation du serpent*/
|
||||
tab[0][1]=0;
|
||||
tab[10][1]=1;
|
||||
for(i=0;i<10;i++){
|
||||
tab[i][1]=1;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void){
|
||||
int tab[H][L];
|
||||
int n=0,fin=0;
|
||||
int sxmax=10,sxmin=1,symin=1,symax=1,direction=0;
|
||||
int* psxmax = &sxmax, psxmin = &sxmin, psymin = &symin, psymax = &symax, pdirection = &direction;
|
||||
unsigned long suivant;
|
||||
suivant=Microsecondes()+DELTA;
|
||||
|
||||
init(tab);
|
||||
|
||||
while(1){
|
||||
Affiche(tab);
|
||||
Serpent(tab);
|
||||
|
||||
/*timer*/
|
||||
if(Microsecondes()>suivant){
|
||||
n++;
|
||||
DessinerTimer(n);
|
||||
suivant=Microsecondes()+DELTA;
|
||||
}
|
||||
|
||||
if(fin==1){
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user