Advertisement
Holdener

Jeu Console

Oct 4th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.86 KB | None | 0 0
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<Windows.h>
  4. #include<time.h>
  5. #include<conio.h>
  6.  
  7. void gotoxy(int x, int y) {
  8.  
  9.  
  10.     COORD c;
  11.     c.X = x;
  12.     c.Y = y;
  13.  
  14.     HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
  15.  
  16.     SetConsoleCursorPosition(h, c);
  17.  
  18. }
  19.  
  20. void textcolor(int color) {
  21.  
  22.     SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
  23. }
  24. int MonRand(int min, int max) {
  25.     int res;
  26.     res = min + rand() % (max - min);
  27.     return res;
  28.  
  29. }
  30.  
  31. float frand() {
  32.     float f;
  33.     f = (float)rand() / RAND_MAX;
  34.     return f;
  35. }
  36.  
  37. int main() {
  38.  
  39.     SetConsoleOutputCP(1252); //pour les accents dans la console
  40.  
  41.     int fin = 0, touche;
  42.     const int TX = 40;
  43.     const int TY = 20;
  44.  
  45.     // déclaration du joueur
  46.     int x, y; // position
  47.     int color; //couleur
  48.     int lettre; // son apparence
  49.  
  50.     srand(time(NULL));
  51.  
  52.     //initialisation du joueur
  53.     x = rand() % TX;
  54.     y = rand() % TY;
  55.     lettre = 'A' + rand() % 26;
  56.     color = rand() % 256;
  57.  
  58.     //afficher le joueur
  59.     textcolor(color);
  60.     gotoxy(x, y);
  61.     putchar(lettre);
  62.  
  63.  
  64.  
  65.  
  66.     while (!fin) { // tant que fin == 0
  67.  
  68.         if (_kbhit()) { //si touche appuyée
  69.            
  70.             //effacer le joueur
  71.             textcolor(0);
  72.             gotoxy(x, y);
  73.             putchar(lettre);
  74.             //putchar(' ');
  75.  
  76.  
  77.  
  78.  
  79.             //recuperer la touche
  80.             touche = _getch();
  81.             switch (touche) {
  82.  
  83.                 case 72: y--;           break; //fleche haut
  84.                 case 77: x++;           break; //fleche droite
  85.                 case 80: y++;           break; //fleche bas
  86.                 case 75: x--;           break; //fleche gauche
  87.  
  88.                 case 'a': color = rand() % 256; break; //changer la couleur du joueur en appuant sur a
  89.  
  90.                 case 27: fin = 1; //quitter : ECHAP
  91.  
  92.             }
  93.             //controle des bords
  94.             if (x < 0)
  95.                 x = 0;
  96.             if (x > TX - 1)
  97.                 x = TX - 1;
  98.  
  99.             if (y < 0)
  100.                 y = 0;
  101.             if (y > TY - 1)
  102.                 y = TY - 1;
  103.  
  104.             //afficher le joueur
  105.             textcolor(color);
  106.             gotoxy(x, y);
  107.             putchar(lettre);
  108.  
  109.  
  110.         }
  111.        
  112.  
  113.     }
  114.  
  115.  
  116.     system("PAUSE");
  117.     return 0;
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement