Advertisement
Holdener

level5 fourmi

Nov 22nd, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include <Windows.h>
  6.  
  7. #define TX  40
  8. #define TY  20
  9. #define NBFOURMI 100
  10.  
  11. typedef struct fourmi {
  12.     float x, y;     // la position
  13.     float dx, dy; // le deplacement
  14.     int color;
  15.     int lettre;
  16.  
  17. }fourmi;
  18.  
  19. // déclarations des fonctions
  20.  
  21. void create_allf(fourmi*t[], int color);
  22. fourmi* create_f(int color);
  23. void run_all(fourmi*t[]);
  24. void destroy_all(fourmi*t[]);
  25. void affiche(fourmi*f, int color);
  26. void avance(fourmi*f);
  27. void gotoxy(int x, int y);
  28. void textcolor(int color);
  29. int top(int duree);
  30.  
  31. int main()
  32. {
  33.  
  34.     fourmi*all[NBFOURMI];
  35.     srand(time(NULL));
  36.  
  37.     create_allf(all, 11);
  38.  
  39.     while (!_kbhit()) {
  40.  
  41.         if (top(75))
  42.             run_all(all);
  43.  
  44.     }
  45.    
  46.    
  47.     destroy_all(all);
  48.    
  49.  
  50.     return 0;
  51. }
  52. /**************************************************
  53. ***************************************************/
  54. void create_allf(fourmi*t[],int color)
  55. {
  56.     for (int i = 0; i < NBFOURMI; i++) {
  57.  
  58.         t[i] = create_f(color);
  59.  
  60.     }
  61. }
  62.  
  63. fourmi*create_f(int color)
  64. {
  65.     fourmi*f;
  66.     f = (fourmi*)malloc(sizeof(fourmi));
  67.     f->color = color;
  68.     f->dx = (rand() / (float)RAND_MAX) * 4 - 2;
  69.     f->dy = (rand() / (float)RAND_MAX) * 4 - 2;
  70.     f->x = rand() % TX;
  71.     f->y = rand() % TY;
  72.     f->lettre = 'A' + rand() % 26;
  73.  
  74.     return f;
  75. }
  76.  
  77.  
  78. void run_all(fourmi*t[]) {
  79.  
  80.     for (int i = 0; i < NBFOURMI; i++) {
  81.  
  82.         affiche(t[i], 0);
  83.         avance(t[i]);
  84.         affiche(t[i], t[i]->color);
  85.  
  86.     }
  87.  
  88. }
  89.  
  90. void destroy_all(fourmi*t[]) {
  91.  
  92.     for (int i = 0; i < NBFOURMI; i++) {
  93.  
  94.         free(t[i]);
  95.  
  96.     }
  97.  
  98. }
  99.  
  100. /**************************************************
  101. ***************************************************/
  102. void affiche(fourmi*f, int color)
  103. {
  104.     gotoxy(f->x, f->y);
  105.     textcolor(color);
  106.     putchar(f->lettre);
  107. }
  108.  
  109. /**************************************************
  110. ***************************************************/
  111. void  avance(fourmi*f)
  112. {
  113.     f->x += f->dx;
  114.     if (f->x < 0) {
  115.         f->x = 0;
  116.         f->dx = rand() / (float)RAND_MAX * 2;
  117.     }
  118.     if (f->x > TX) {
  119.         f->x = TX;
  120.         f->dx = rand() / (float)RAND_MAX * -2;
  121.     }
  122.  
  123.     f->y += f->dy;
  124.     if (f->y < 0) {
  125.         f->y = 0;
  126.         f->dy = rand() / (float)RAND_MAX * 2;
  127.     }
  128.     if (f->y > TY) {
  129.         f->y = TY;
  130.         f->dy = rand() / (float)RAND_MAX * -2;
  131.     }
  132.  
  133. }
  134.  
  135. /**************************************************
  136. TOOLS
  137. ***************************************************/
  138. void gotoxy(int x, int y)
  139. {
  140.     COORD c;
  141.     c.X = x;
  142.     c.Y = y;
  143.     SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
  144. }
  145. void textcolor(int color)
  146. {
  147.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
  148. }
  149. int top(int duree)
  150. {
  151.     static int start = 0;
  152.     int res = 0;
  153.     if (clock() > start + duree) {
  154.         start = clock();
  155.         res = 1;
  156.     }
  157.     return res;
  158. }
  159.  
  160.  
  161. /**************************************************
  162. ***************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement