Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <conio.h>
- #include <Windows.h>
- #define TX 40
- #define TY 20
- #define NBFOURMI 100
- typedef struct fourmi {
- float x, y; // la position
- float dx, dy; // le deplacement
- int color;
- int lettre;
- }fourmi;
- // déclarations des fonctions
- void create_allf(fourmi*t[], int color);
- fourmi* create_f(int color);
- void run_all(fourmi*t[]);
- void destroy_all(fourmi*t[]);
- void affiche(fourmi*f, int color);
- void avance(fourmi*f);
- void gotoxy(int x, int y);
- void textcolor(int color);
- int top(int duree);
- int main()
- {
- fourmi*all[NBFOURMI];
- srand(time(NULL));
- create_allf(all, 11);
- while (!_kbhit()) {
- if (top(75))
- run_all(all);
- }
- destroy_all(all);
- return 0;
- }
- /**************************************************
- ***************************************************/
- void create_allf(fourmi*t[],int color)
- {
- for (int i = 0; i < NBFOURMI; i++) {
- t[i] = create_f(color);
- }
- }
- fourmi*create_f(int color)
- {
- fourmi*f;
- f = (fourmi*)malloc(sizeof(fourmi));
- f->color = color;
- f->dx = (rand() / (float)RAND_MAX) * 4 - 2;
- f->dy = (rand() / (float)RAND_MAX) * 4 - 2;
- f->x = rand() % TX;
- f->y = rand() % TY;
- f->lettre = 'A' + rand() % 26;
- return f;
- }
- void run_all(fourmi*t[]) {
- for (int i = 0; i < NBFOURMI; i++) {
- affiche(t[i], 0);
- avance(t[i]);
- affiche(t[i], t[i]->color);
- }
- }
- void destroy_all(fourmi*t[]) {
- for (int i = 0; i < NBFOURMI; i++) {
- free(t[i]);
- }
- }
- /**************************************************
- ***************************************************/
- void affiche(fourmi*f, int color)
- {
- gotoxy(f->x, f->y);
- textcolor(color);
- putchar(f->lettre);
- }
- /**************************************************
- ***************************************************/
- void avance(fourmi*f)
- {
- f->x += f->dx;
- if (f->x < 0) {
- f->x = 0;
- f->dx = rand() / (float)RAND_MAX * 2;
- }
- if (f->x > TX) {
- f->x = TX;
- f->dx = rand() / (float)RAND_MAX * -2;
- }
- f->y += f->dy;
- if (f->y < 0) {
- f->y = 0;
- f->dy = rand() / (float)RAND_MAX * 2;
- }
- if (f->y > TY) {
- f->y = TY;
- f->dy = rand() / (float)RAND_MAX * -2;
- }
- }
- /**************************************************
- TOOLS
- ***************************************************/
- void gotoxy(int x, int y)
- {
- COORD c;
- c.X = x;
- c.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
- }
- void textcolor(int color)
- {
- SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
- }
- int top(int duree)
- {
- static int start = 0;
- int res = 0;
- if (clock() > start + duree) {
- start = clock();
- res = 1;
- }
- return res;
- }
- /**************************************************
- ***************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement